Exemplo n.º 1
0
        public static void ExtensionDataDictionarySerialize_DoesNotHonor()
        {
            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            EmptyClassWithExtensionProperty obj = JsonSerializer.Deserialize <EmptyClassWithExtensionProperty>(@"{""Key1"": 1}", options);

            // Ignore naming policy for extension data properties by default.
            Assert.False(obj.MyOverflow.ContainsKey("key1"));
            Assert.Equal(1, obj.MyOverflow["Key1"].GetInt32());
        }
Exemplo n.º 2
0
        public static void EmptyPropertyNameInExtensionData()
        {
            {
                string json = @"{"""":42}";
                EmptyClassWithExtensionProperty obj = JsonSerializer.Deserialize <EmptyClassWithExtensionProperty>(json);
                Assert.Equal(1, obj.MyOverflow.Count);
                Assert.Equal(42, obj.MyOverflow[""].GetInt32());
            }

            {
                // Verify that last-in wins.
                string json = @"{"""":42, """":43}";
                EmptyClassWithExtensionProperty obj = JsonSerializer.Deserialize <EmptyClassWithExtensionProperty>(json);
                Assert.Equal(1, obj.MyOverflow.Count);
                Assert.Equal(43, obj.MyOverflow[""].GetInt32());
            }
        }
Exemplo n.º 3
0
        public static void LongPropertyNames(int propertyLength, char ch)
        {
            // Although the CLR may limit member length to 1023 bytes, the serializer doesn't have a hard limit.

            string val  = new string(ch, propertyLength);
            string json = @"{""" + val + @""":1}";

            EmptyClassWithExtensionProperty obj = JsonSerializer.Deserialize <EmptyClassWithExtensionProperty>(json);

            Assert.True(obj.MyOverflow.ContainsKey(val));

            var options = new JsonSerializerOptions
            {
                // Avoid escaping '\u0467'.
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };

            string jsonRoundTripped = JsonSerializer.Serialize(obj, options);

            Assert.Equal(json, jsonRoundTripped);
        }