public static void ExtensionDataDictionaryHandlesPreserveReferences() { Employee bob = new Employee { Name = "Bob" }; EmployeeExtensionData angela = new EmployeeExtensionData { Name = "Angela", Manager = bob }; bob.Subordinates = new List <Employee> { angela }; var extensionData = new Dictionary <string, object> { ["extString"] = "string value", ["extNumber"] = 100, ["extObject"] = bob, ["extArray"] = bob.Subordinates }; angela.ExtensionData = extensionData; string expected = JsonConvert.SerializeObject(angela, s_newtonsoftSerializerSettingsPreserve); string actual = JsonSerializer.Serialize(angela, s_serializerOptionsPreserve); Assert.Equal(expected, actual); }
public static void JsonObjectNonCollectionTest() { // $values Not Valid string json = @"{ ""$id"": ""1"", ""$values"": ""test"" }"; JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <EmployeeExtensionData>(json, s_deserializerOptionsPreserve)); Assert.Equal("$.$values", ex.Path); ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <Dictionary <string, string> >(json, s_deserializerOptionsPreserve)); Assert.Equal("$.$values", ex.Path); // $.* Not valid (i.e: $test) json = @"{ ""$id"": ""1"", ""$test"": ""test"" }"; ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <EmployeeExtensionData>(json, s_deserializerOptionsPreserve)); Assert.Equal("$.$test", ex.Path); ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <Dictionary <string, string> >(json, s_deserializerOptionsPreserve)); Assert.Equal("$.$test", ex.Path); json = @"{ ""$id"": ""1"", ""\u0024test"": ""test"" }"; // \u0024.* Valid (i.e: \u0024test) EmployeeExtensionData employee = JsonSerializer.Deserialize <EmployeeExtensionData>(json, s_deserializerOptionsPreserve); Assert.Equal("test", ((JsonElement)employee.ExtensionData["$test"]).GetString()); Dictionary <string, string> dictionary = JsonSerializer.Deserialize <Dictionary <string, string> >(json, s_deserializerOptionsPreserve); Assert.Equal("test", dictionary["$test"]); }