Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 52031ef

Browse files
committed
Fix DCJS serialiation for type which has DataMember with special Name values
1 parent baed3e7 commit 52031ef

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerWriteContextComplexJson.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ private void VerifyType(DataContract dataContract, Type declaredType)
326326
scopedKnownTypes.Pop();
327327
}
328328
}
329+
330+
internal static void WriteJsonNameWithMapping(XmlWriterDelegator xmlWriter, XmlDictionaryString[] memberNames, int index)
331+
{
332+
xmlWriter.WriteStartElement("a", JsonGlobals.itemString, JsonGlobals.itemString);
333+
xmlWriter.WriteAttributeString(null, JsonGlobals.itemString, null, memberNames[index].Value);
334+
}
329335
#endif
330336

331337
internal static void VerifyObjectCompatibilityWithInterface(DataContract contract, object graph, Type declaredType)

src/System.Runtime.Serialization.Json/tests/DataContractJsonSerializer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,19 @@ public static void DCJS_UseSimpleDictionaryFormat()
12961296
Assert.StrictEqual(dict["key2"], deserialized["key2"]);
12971297
}
12981298

1299+
[Fact]
1300+
public static void DCJS_DataMemberNames()
1301+
{
1302+
var obj = new AppEnvironment()
1303+
{
1304+
ScreenDpi = 440,
1305+
ScreenOrientation = "horizontal"
1306+
};
1307+
var actual = SerializeAndDeserialize(obj, @"{""screen_dpi(x:y)"":440,""screen:orientation"":""horizontal""}");
1308+
Assert.StrictEqual(obj.ScreenDpi, actual.ScreenDpi);
1309+
Assert.StrictEqual(obj.ScreenOrientation, actual.ScreenOrientation);
1310+
}
1311+
12991312
private static T SerializeAndDeserialize<T>(T value, string baseline, DataContractJsonSerializerSettings settings = null, Func<DataContractJsonSerializer> serializerFactory = null)
13001313
{
13011314
DataContractJsonSerializer dcjs;

src/System.Runtime.Serialization.Xml/tests/SerializationTypes.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,3 +2119,13 @@ public TypeWithNonParameterlessConstructor(string value)
21192119
StringProperty = value;
21202120
}
21212121
}
2122+
2123+
[DataContract]
2124+
public class AppEnvironment
2125+
{
2126+
[DataMember(Name = "screen:orientation")]
2127+
public string ScreenOrientation { get; set; }
2128+
2129+
[DataMember(Name = "screen_dpi(x:y)")]
2130+
public int ScreenDpi { get; set; }
2131+
}

0 commit comments

Comments
 (0)