Skip to content

Commit

Permalink
Align DCJS with 4.8 implementation. (dotnet#57533)
Browse files Browse the repository at this point in the history
* Align DCJS with 4.8 version. This work addresses dotnet#55270.
  • Loading branch information
StephenMolloy authored Aug 21, 2021
1 parent 50576e3 commit 36697a5
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 597 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public JsonClassDataContract(ClassDataContract traditionalDataContract)
_helper = (base.Helper as JsonClassDataContractCriticalHelper)!;
}

[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
private JsonFormatClassReaderDelegate CreateJsonFormatReaderDelegate()
{
return new ReflectionJsonClassReader(TraditionalClassDataContract).ReflectionReadClass;
}

internal JsonFormatClassReaderDelegate JsonFormatReaderDelegate
{
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
Expand All @@ -41,7 +35,7 @@ internal JsonFormatClassReaderDelegate JsonFormatReaderDelegate
JsonFormatClassReaderDelegate tempDelegate;
if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
tempDelegate = CreateJsonFormatReaderDelegate();
tempDelegate = new ReflectionJsonClassReader(TraditionalClassDataContract).ReflectionReadClass;
}
else
{
Expand All @@ -57,12 +51,6 @@ internal JsonFormatClassReaderDelegate JsonFormatReaderDelegate
}
}

[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
private JsonFormatClassWriterDelegate CreateJsonFormatWriterDelegate()
{
return new ReflectionJsonFormatWriter().ReflectionWriteClass;
}

internal JsonFormatClassWriterDelegate JsonFormatWriterDelegate
{
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
Expand All @@ -77,7 +65,7 @@ internal JsonFormatClassWriterDelegate JsonFormatWriterDelegate
JsonFormatClassWriterDelegate tempDelegate;
if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
tempDelegate = CreateJsonFormatWriterDelegate();
tempDelegate = new ReflectionJsonFormatWriter().ReflectionWriteClass;
}
else
{
Expand Down Expand Up @@ -171,7 +159,7 @@ private void CopyMembersAndCheckDuplicateNames()
else
{
memberTable.Add(_traditionalClassDataContract.MemberNames[i].Value, null);
decodedMemberNames[i] = DataContractJsonSerializerImpl.ConvertXmlNameToJsonName(_traditionalClassDataContract.MemberNames[i]);
decodedMemberNames[i] = DataContractJsonSerializer.ConvertXmlNameToJsonName(_traditionalClassDataContract.MemberNames[i]);
}
}
_memberNames = decodedMemberNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public JsonCollectionDataContract(CollectionDataContract traditionalDataContract
_helper = (base.Helper as JsonCollectionDataContractCriticalHelper)!;
}

[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
private JsonFormatCollectionReaderDelegate CreateJsonFormatReaderDelegate()
{
return new ReflectionJsonCollectionReader().ReflectionReadCollection;
}

internal JsonFormatCollectionReaderDelegate JsonFormatReaderDelegate
{
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
Expand All @@ -41,7 +35,7 @@ internal JsonFormatCollectionReaderDelegate JsonFormatReaderDelegate
JsonFormatCollectionReaderDelegate tempDelegate;
if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
tempDelegate = CreateJsonFormatReaderDelegate();
tempDelegate = new ReflectionJsonCollectionReader().ReflectionReadCollection;
}
else
{
Expand All @@ -57,12 +51,6 @@ internal JsonFormatCollectionReaderDelegate JsonFormatReaderDelegate
}
}

[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
private JsonFormatGetOnlyCollectionReaderDelegate CreateJsonFormatGetOnlyReaderDelegate()
{
return new ReflectionJsonCollectionReader().ReflectionReadGetOnlyCollection;
}

internal JsonFormatGetOnlyCollectionReaderDelegate JsonFormatGetOnlyReaderDelegate
{
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
Expand All @@ -83,7 +71,7 @@ internal JsonFormatGetOnlyCollectionReaderDelegate JsonFormatGetOnlyReaderDelega
JsonFormatGetOnlyCollectionReaderDelegate tempDelegate;
if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
tempDelegate = CreateJsonFormatGetOnlyReaderDelegate();
tempDelegate = new ReflectionJsonCollectionReader().ReflectionReadGetOnlyCollection;
}
else
{
Expand All @@ -99,13 +87,6 @@ internal JsonFormatGetOnlyCollectionReaderDelegate JsonFormatGetOnlyReaderDelega
}
}

[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
private JsonFormatCollectionWriterDelegate CreateJsonFormatWriterDelegate()
{
return new ReflectionJsonFormatWriter().ReflectionWriteCollection;
}


internal JsonFormatCollectionWriterDelegate JsonFormatWriterDelegate
{
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
Expand All @@ -120,7 +101,7 @@ internal JsonFormatCollectionWriterDelegate JsonFormatWriterDelegate
JsonFormatCollectionWriterDelegate tempDelegate;
if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
tempDelegate = CreateJsonFormatWriterDelegate();
tempDelegate = new ReflectionJsonFormatWriter().ReflectionWriteCollection;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ private void InitForReading(Stream inputStream, Encoding? expectedEncoding)
{
try
{
//this.stream = new BufferedStream(inputStream);
_stream = inputStream;
_stream = new BufferedStream(inputStream);

SupportedEncoding expectedEnc = GetSupportedEncoding(expectedEncoding);
SupportedEncoding dataEnc = ReadEncoding();
Expand Down Expand Up @@ -472,8 +471,7 @@ private void InitForReading(Stream inputStream, Encoding? expectedEncoding)
private void InitForWriting(Stream outputStream, Encoding writeEncoding)
{
_encoding = writeEncoding;
//this.stream = new BufferedStream(outputStream);
_stream = outputStream;
_stream = new BufferedStream(outputStream);

// Set the encoding code
_encodingCode = GetSupportedEncoding(writeEncoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace System.Runtime.Serialization
{
public static class JsonFormatGeneratorStatics
{
private static MethodInfo? s_boxPointer;

private static PropertyInfo? s_collectionItemNameProperty;

private static ConstructorInfo? s_extensionDataObjectCtor;
Expand Down Expand Up @@ -59,6 +61,8 @@ public static class JsonFormatGeneratorStatics

private static PropertyInfo? s_typeHandleProperty;

private static MethodInfo? s_unboxPointer;

private static PropertyInfo? s_useSimpleDictionaryFormatReadProperty;

private static PropertyInfo? s_useSimpleDictionaryFormatWriteProperty;
Expand All @@ -81,6 +85,19 @@ public static class JsonFormatGeneratorStatics

private static MethodInfo? s_getJsonMemberNameMethod;

public static MethodInfo BoxPointer
{
get
{
if (s_boxPointer == null)
{
s_boxPointer = typeof(Pointer).GetMethod("Box");
Debug.Assert(s_boxPointer != null);
}
return s_boxPointer;
}
}

public static PropertyInfo CollectionItemNameProperty
{
get
Expand Down Expand Up @@ -349,6 +366,18 @@ public static PropertyInfo TypeHandleProperty
return s_typeHandleProperty;
}
}
public static MethodInfo UnboxPointer
{
get
{
if (s_unboxPointer == null)
{
s_unboxPointer = typeof(Pointer).GetMethod("Unbox");
Debug.Assert(s_unboxPointer != null);
}
return s_unboxPointer;
}
}
public static PropertyInfo UseSimpleDictionaryFormatReadProperty
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,14 @@ private void InternalDeserialize(LocalBuilder value, Type type, string name)
_ilg.Load(string.Empty);
_ilg.Call(XmlFormatGeneratorStatics.InternalDeserializeMethod);

_ilg.ConvertValue(Globals.TypeOfObject, type);
if (type.IsPointer)
{
_ilg.Call(JsonFormatGeneratorStatics.UnboxPointer);
}
else
{
_ilg.ConvertValue(Globals.TypeOfObject, type);
}
_ilg.Stloc(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ internal JsonFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionD
}
}
InitArgs(collectionContract.UnderlyingType);
if (collectionContract.IsReadOnlyContract)
{
ThrowIfCannotSerializeReadOnlyTypes(collectionContract);
}
WriteCollection(collectionContract);
return (JsonFormatCollectionWriterDelegate)_ilg.EndMethod();
}
Expand Down Expand Up @@ -162,6 +166,23 @@ private void InitArgs(Type objType)
_ilg.Stloc(_objectLocal);
}

private void ThrowIfCannotSerializeReadOnlyTypes(CollectionDataContract classContract)
{
ThrowIfCannotSerializeReadOnlyTypes(XmlFormatGeneratorStatics.CollectionSerializationExceptionMessageProperty);
}

private void ThrowIfCannotSerializeReadOnlyTypes(PropertyInfo serializationExceptionMessageProperty)
{
_ilg.Load(_contextArg);
_ilg.LoadMember(XmlFormatGeneratorStatics.SerializeReadOnlyTypesProperty);
_ilg.IfNot();
_ilg.Load(_dataContractArg);
_ilg.LoadMember(serializationExceptionMessageProperty);
_ilg.Load(null);
_ilg.Call(XmlFormatGeneratorStatics.ThrowInvalidDataContractExceptionMethod);
_ilg.EndIf();
}

private void InvokeOnSerializing(ClassDataContract classContract)
{
if (classContract.BaseContract != null)
Expand Down Expand Up @@ -243,7 +264,7 @@ private int WriteMembers(ClassDataContract classContract, LocalBuilder? extensio
_ilg.IfNotDefaultValue(memberValue);
}

bool requiresNameAttribute = DataContractJsonSerializerImpl.CheckIfXmlNameRequiresMapping(classContract.MemberNames![i]);
bool requiresNameAttribute = DataContractJsonSerializer.CheckIfXmlNameRequiresMapping(classContract.MemberNames![i]);
if (requiresNameAttribute || !TryWritePrimitive(memberType, memberValue, member.MemberInfo, arrayItemIndex: null, name: null, nameIndex: i + _childElementIndex))
{
// Note: DataContractSerializer has member-conflict logic here to deal with the schema export
Expand Down Expand Up @@ -554,7 +575,7 @@ private bool TryWritePrimitiveArray(Type type, Type itemType, LocalBuilder value
return false;

string? writeArrayMethod = null;
switch (itemType.GetTypeCode())
switch (Type.GetTypeCode(itemType))
{
case TypeCode.Boolean:
writeArrayMethod = "WriteJsonBooleanArray";
Expand Down Expand Up @@ -616,6 +637,15 @@ private void WriteObjectAttribute()
private void WriteValue(LocalBuilder memberValue)
{
Type memberType = memberValue.LocalType;
if (memberType.IsPointer)
{
_ilg.Load(memberValue);
_ilg.Load(memberType);
_ilg.Call(JsonFormatGeneratorStatics.BoxPointer);
memberType = typeof(System.Reflection.Pointer);
memberValue = _ilg.DeclareLocal(memberType, "memberValueRefPointer");
_ilg.Store(memberValue);
}
bool isNullableOfT = (memberType.IsGenericType &&
memberType.GetGenericTypeDefinition() == Globals.TypeOfNullable);
if (memberType.IsValueType && !isNullableOfT)
Expand Down Expand Up @@ -739,7 +769,7 @@ private void WriteStartElement(LocalBuilder? nameLocal, int nameIndex)
// namespace
_ilg.Load(null);

if (nameLocal != null && nameLocal.LocalType == Globals.TypeOfString)
if (nameLocal != null && nameLocal.LocalType == typeof(string))
{
_ilg.Call(JsonFormatGeneratorStatics.WriteStartElementStringMethod);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public JsonObjectDataContract(DataContract traditionalDataContract)
break;
case JsonGlobals.arrayString:
// Read as object array
return DataContractJsonSerializerImpl.ReadJsonValue(DataContract.GetDataContract(Globals.TypeOfObjectArray), jsonReader, context);
return DataContractJsonSerializer.ReadJsonValue(DataContract.GetDataContract(Globals.TypeOfObjectArray), jsonReader, context);
default:
throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.JsonUnexpectedAttributeValue, contentMode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected override int ReflectionWriteMembers(XmlWriterDelegator xmlWriter, obje
{
memberValue = ReflectionGetMemberValue(obj, member);
}
bool requiresNameAttribute = DataContractJsonSerializerImpl.CheckIfXmlNameRequiresMapping(classContract.MemberNames![i]);
bool requiresNameAttribute = DataContractJsonSerializer.CheckIfXmlNameRequiresMapping(classContract.MemberNames![i]);
PrimitiveDataContract? primitiveContract = member.MemberPrimitiveContract;
if (requiresNameAttribute || !ReflectionTryWritePrimitive(xmlWriter, context, memberType, memberValue, memberNames[i + childElementIndex] /*name*/, null/*ns*/, primitiveContract))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private bool IsReadingComplexText
}
}

protected override void Dispose(bool disposing)
public override void Close()
{
OnXmlDictionaryReaderClose? onClose = _onReaderClose;
_onReaderClose = null;
Expand All @@ -353,7 +353,8 @@ protected override void Dispose(bool disposing)
throw new InvalidOperationException(SR.GenericCallbackException, e);
}
}
base.Dispose(disposing);

base.Close();
}

public override void EndCanonicalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private static BinHexEncoding BinHexEncoding

private bool WrittenNameWithMapping => (_nameState & NameState.WrittenNameWithMapping) == NameState.WrittenNameWithMapping;

protected override void Dispose(bool disposing)
public override void Close()
{
if (!IsClosed)
{
Expand All @@ -219,7 +219,7 @@ protected override void Dispose(bool disposing)
}
}

base.Dispose(disposing);
base.Close();
}

public override void Flush()
Expand Down Expand Up @@ -262,21 +262,20 @@ public void SetOutput(Stream stream, Encoding encoding, bool ownsStream)
{
throw new ArgumentNullException(nameof(encoding));
}
Encoding? tempEncoding = encoding;
if (tempEncoding.WebName != Encoding.UTF8.WebName)
if (encoding.WebName != Encoding.UTF8.WebName)
{
stream = new JsonEncodingStreamWrapper(stream, tempEncoding, false);
stream = new JsonEncodingStreamWrapper(stream, encoding, false);
}
else
{
tempEncoding = null;
encoding = null!;
}
if (_nodeWriter == null)
{
_nodeWriter = new JsonNodeWriter();
}

_nodeWriter.SetOutput(stream, ownsStream, tempEncoding);
_nodeWriter.SetOutput(stream, ownsStream, encoding);
InitializeWriter();
}

Expand Down
Loading

0 comments on commit 36697a5

Please sign in to comment.