Exemplo n.º 1
0
        public async Task WriteGenericQueueOfQueue()
        {
            Queue <Queue> input = new Queue <Queue>();

            input.Enqueue(new Queue(new List <int>()
            {
                1, 2
            }));
            input.Enqueue(new Queue(new List <int>()
            {
                3, 4
            }));

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericQueueWrapper <QueueWrapper> input2 = new GenericQueueWrapper <QueueWrapper>();

            input2.Enqueue(new QueueWrapper(new List <object>()
            {
                1, 2
            }));
            input2.Enqueue(new QueueWrapper(new List <object>()
            {
                3, 4
            }));

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal("[[1,2],[3,4]]", json);
        }
Exemplo n.º 2
0
        public async Task WriteGenericStackOfStack()
        {
            Stack <Stack> input = new Stack <Stack>();

            input.Push(new Stack(new List <int>()
            {
                1, 2
            }));
            input.Push(new Stack(new List <int>()
            {
                3, 4
            }));

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[4,3],[2,1]]", json);

            GenericStackWrapper <StackWrapper> input2 = new GenericStackWrapper <StackWrapper>();

            input2.Push(new StackWrapper(new List <object> {
                1, 2
            }));
            input2.Push(new StackWrapper(new List <object> {
                3, 4
            }));

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal("[[4,3],[2,1]]", json);
        }
Exemplo n.º 3
0
        public async Task WriteGenericICollectionOfICollection()
        {
            ICollection <ICollection> input = new List <ICollection>
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericICollectionWrapper <WrapperForICollection> input2 = new GenericICollectionWrapper <WrapperForICollection>
            {
                new WrapperForICollection(new List <object> {
                    1, 2
                }),
                new WrapperForICollection(new List <object> {
                    3, 4
                }),
            };

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal("[[1,2],[3,4]]", json);
        }
Exemplo n.º 4
0
        public async Task WriteIEnumerableOfIEnumerable()
        {
            IEnumerable input = new List <List <int> >
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            WrapperForIEnumerable input2 = new WrapperForIEnumerable(new List <object>
            {
                new List <object>()
                {
                    1, 2
                },
                new List <object>()
                {
                    3, 4
                },
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal("[[1,2],[3,4]]", json);
        }
Exemplo n.º 5
0
        public async Task WriteArrayListOfArrayList()
        {
            ArrayList input = new ArrayList
            {
                new ArrayList(new List <int>()
                {
                    1, 2
                }),
                new ArrayList(new List <int>()
                {
                    3, 4
                })
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            ArrayListWrapper input2 = new ArrayListWrapper(new List <object>
            {
                new ArrayListWrapper(new List <object>()
                {
                    1, 2
                }),
                new ArrayListWrapper(new List <object>()
                {
                    3, 4
                })
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal("[[1,2],[3,4]]", json);
        }
        public async Task WriteStackTOfStackT()
        {
            Stack <Stack <int> > input = new Stack <Stack <int> >(new List <Stack <int> >
            {
                new Stack <int>(new List <int>()
                {
                    1, 2
                }),
                new Stack <int>(new List <int>()
                {
                    3, 4
                })
            });

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[4,3],[2,1]]", json);

            GenericStackWrapper <StringStackWrapper> input2 = new GenericStackWrapper <StringStackWrapper>(new List <StringStackWrapper>
            {
                new StringStackWrapper(new List <string>()
                {
                    "1", "2"
                }),
                new StringStackWrapper(new List <string>()
                {
                    "3", "4"
                })
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal(@"[[""4"",""3""],[""2"",""1""]]", json);
        }
Exemplo n.º 7
0
        public async Task NodesAsPocoProperties()
        {
            const string Expected = "{\"MyString\":null,\"Node\":42,\"Array\":[43],\"Value\":44,\"IntValue\":45,\"Object\":{\"Property\":46}}";

            var poco = new Poco
            {
                Node     = 42,
                Array    = new JsonArray(43),
                Value    = (JsonValue)44,
                IntValue = (JsonValue)45,
                Object   = new JsonObject
                {
                    ["Property"] = 46
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(poco);

            Assert.Equal(Expected, json);

            poco = await JsonSerializerWrapperForString.DeserializeWrapper <Poco>(json);

            Assert.Equal(42, (int)poco.Node);
            Assert.Equal(43, (int)poco.Array[0]);
            Assert.Equal(44, (int)poco.Value);
            Assert.Equal(45, (int)poco.IntValue);
            Assert.Equal(46, (int)poco.Object["Property"]);
        }
        public async Task WriteGenericIEnumerableOfGenericIEnumerable()
        {
            IEnumerable <IEnumerable <int> > input = new List <List <int> >
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericIEnumerableWrapper <StringIEnumerableWrapper> input2 = new GenericIEnumerableWrapper <StringIEnumerableWrapper>(new List <StringIEnumerableWrapper>
            {
                new StringIEnumerableWrapper(new List <string> {
                    "1", "2"
                }),
                new StringIEnumerableWrapper(new List <string> {
                    "3", "4"
                }),
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal(@"[[""1"",""2""],[""3"",""4""]]", json);
        }
Exemplo n.º 9
0
        public async Task ArrayLoop()
        {
            ListWithGenericCycle root = new ListWithGenericCycle();

            root.Add(root);

            string expected = JsonConvert.SerializeObject(root, s_newtonsoftSerializerSettingsPreserve);
            string actual   = await JsonSerializerWrapperForString.SerializeWrapper(root, s_serializerOptionsPreserve);

            Assert.Equal(expected, actual);

            ListWithGenericCycle rootCopy = await JsonSerializerWrapperForString.DeserializeWrapper <ListWithGenericCycle>(actual, s_serializerOptionsPreserve);

            Assert.Same(rootCopy, rootCopy[0]);

            // Duplicate reference
            root = new ListWithGenericCycle();
            root.Add(root);
            root.Add(root);
            root.Add(root);

            expected = JsonConvert.SerializeObject(root, s_newtonsoftSerializerSettingsPreserve);
            actual   = await JsonSerializerWrapperForString.SerializeWrapper(root, s_serializerOptionsPreserve);

            Assert.Equal(expected, actual);

            rootCopy = await JsonSerializerWrapperForString.DeserializeWrapper <ListWithGenericCycle>(actual, s_serializerOptionsPreserve);

            Assert.Same(rootCopy, rootCopy[0]);
            Assert.Same(rootCopy, rootCopy[1]);
            Assert.Same(rootCopy, rootCopy[2]);
        }
Exemplo n.º 10
0
        public async Task WriteIReadOnlyListTOfIReadOnlyListT()
        {
            IReadOnlyList <IReadOnlyList <int> > input = new List <List <int> >
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericIReadOnlyListWrapper <StringIReadOnlyListWrapper> input2 = new GenericIReadOnlyListWrapper <StringIReadOnlyListWrapper>(new List <StringIReadOnlyListWrapper>
            {
                new StringIReadOnlyListWrapper(new List <string> {
                    "1", "2"
                }),
                new StringIReadOnlyListWrapper(new List <string> {
                    "3", "4"
                })
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal(@"[[""1"",""2""],[""3"",""4""]]", json);
        }
Exemplo n.º 11
0
        public async Task WriteIListOfIList()
        {
            IList input = new List <IList>
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            WrapperForIList input2 = new WrapperForIList
            {
                new List <object>()
                {
                    1, 2
                },
                new List <object>()
                {
                    3, 4
                },
            };

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal("[[1,2],[3,4]]", json);
        }
Exemplo n.º 12
0
        public async Task WriteGenericIReadOnlyCollectionOfGenericIReadOnlyCollection()
        {
            IReadOnlyCollection <IReadOnlyCollection <int> > input = new List <List <int> >
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericIReadOnlyCollectionWrapper <WrapperForIReadOnlyCollectionOfT <string> > input2 =
                new GenericIReadOnlyCollectionWrapper <WrapperForIReadOnlyCollectionOfT <string> >(new List <WrapperForIReadOnlyCollectionOfT <string> >
            {
                new WrapperForIReadOnlyCollectionOfT <string>(new List <string> {
                    "1", "2"
                }),
                new WrapperForIReadOnlyCollectionOfT <string>(new List <string> {
                    "3", "4"
                })
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal(@"[[""1"",""2""],[""3"",""4""]]", json);
        }
Exemplo n.º 13
0
        public async Task WriteGenericIListOfGenericIList()
        {
            IList <IList <int> > input = new List <IList <int> >
            {
                new List <int>()
                {
                    1, 2
                },
                new List <int>()
                {
                    3, 4
                }
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericIListWrapper <StringIListWrapper> input2 = new GenericIListWrapper <StringIListWrapper>
            {
                new StringIListWrapper()
                {
                    "1", "2"
                },
                new StringIListWrapper()
                {
                    "3", "4"
                }
            };

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal(@"[[""1"",""2""],[""3"",""4""]]", json);
        }
Exemplo n.º 14
0
        public async Task WriteClassWithNullKeyValuePairValues_NullWrittenAsEmptyObject()
        {
            var value = new SimpleClassWithKeyValuePairs()
            {
                KvpWStrVal      = new KeyValuePair <string, string>("key", null),
                KvpWObjVal      = new KeyValuePair <string, object>("key", null),
                KvpWClassVal    = new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null),
                KvpWStrKvpVal   = new KeyValuePair <string, KeyValuePair <string, string> >("key", new KeyValuePair <string, string>("key", null)),
                KvpWObjKvpVal   = new KeyValuePair <string, KeyValuePair <string, object> >("key", new KeyValuePair <string, object>("key", null)),
                KvpWClassKvpVal = new KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> >("key", new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null)),
            };

            string result = await JsonSerializerWrapperForString.SerializeWrapper(value);

            // Roundtrip to ensure serialize was correct.
            value = await JsonSerializerWrapperForString.DeserializeWrapper <SimpleClassWithKeyValuePairs>(result);

            Assert.Equal("key", value.KvpWStrVal.Key);
            Assert.Equal("key", value.KvpWObjVal.Key);
            Assert.Equal("key", value.KvpWClassVal.Key);
            Assert.Equal("key", value.KvpWStrKvpVal.Key);
            Assert.Equal("key", value.KvpWObjKvpVal.Key);
            Assert.Equal("key", value.KvpWClassKvpVal.Key);
            Assert.Equal("key", value.KvpWStrKvpVal.Value.Key);
            Assert.Equal("key", value.KvpWObjKvpVal.Value.Key);
            Assert.Equal("key", value.KvpWClassKvpVal.Value.Key);

            Assert.Null(value.KvpWStrVal.Value);
            Assert.Null(value.KvpWObjVal.Value);
            Assert.Null(value.KvpWClassVal.Value);
            Assert.Null(value.KvpWStrKvpVal.Value.Value);
            Assert.Null(value.KvpWObjKvpVal.Value.Value);
            Assert.Null(value.KvpWClassKvpVal.Value.Value);
        }
Exemplo n.º 15
0
        public async Task WriteQueueTOfQueueT()
        {
            Queue <Queue <int> > input = new Queue <Queue <int> >(new List <Queue <int> >
            {
                new Queue <int>(new List <int>()
                {
                    1, 2
                }),
                new Queue <int>(new List <int>()
                {
                    3, 4
                })
            });

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal("[[1,2],[3,4]]", json);

            GenericQueueWrapper <StringQueueWrapper> input2 = new GenericQueueWrapper <StringQueueWrapper>(new List <StringQueueWrapper>
            {
                new StringQueueWrapper(new List <string>()
                {
                    "1", "2"
                }),
                new StringQueueWrapper(new List <string>()
                {
                    "3", "4"
                })
            });

            json = await JsonSerializerWrapperForString.SerializeWrapper(input2);

            Assert.Equal(@"[[""1"",""2""],[""3"",""4""]]", json);
        }
Exemplo n.º 16
0
        public async Task CustomReferenceResolver()
        {
            string json    = @"[
  {
    ""$id"": ""0b64ffdf-d155-44ad-9689-58d9adb137f3"",
    ""Name"": ""John Smith"",
    ""Spouse"": {
      ""$id"": ""ae3c399c-058d-431d-91b0-a36c266441b9"",
      ""Name"": ""Jane Smith"",
      ""Spouse"": {
        ""$ref"": ""0b64ffdf-d155-44ad-9689-58d9adb137f3""
      }
    }
  },
  {
    ""$ref"": ""ae3c399c-058d-431d-91b0-a36c266441b9""
  }
]";
            var    options = new JsonSerializerOptions
            {
                WriteIndented    = true,
                ReferenceHandler = new ReferenceHandler <GuidReferenceResolver>()
            };
            ImmutableArray <PersonReference> people = await JsonSerializerWrapperForString.DeserializeWrapper <ImmutableArray <PersonReference> >(json, options);

            Assert.Equal(2, people.Length);

            PersonReference john = people[0];
            PersonReference jane = people[1];

            Assert.Same(john, jane.Spouse);
            Assert.Same(jane, john.Spouse);

            Assert.Equal(json, await JsonSerializerWrapperForString.SerializeWrapper(people, options), ignoreLineEndingDifferences: true);
        }
Exemplo n.º 17
0
        public async Task 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   = await JsonSerializerWrapperForString.SerializeWrapper(angela, s_serializerOptionsPreserve);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 18
0
        public async Task WriteWrappingBraceResetsCorrectly()
        {
            List <int> list = new List <int> {
                10, 20, 30
            };
            ImmutableArray <int> immutableArr = list.ToImmutableArray();

            var root = new ClassWithListAndImmutableArray
            {
                PreservableList = list,
                // Do not write any curly braces for ImmutableArray since is a value type.
                NonProservableArray = immutableArr
            };
            await JsonSerializerWrapperForString.SerializeWrapper(root, s_serializerOptionsPreserve);

            ImmutableArray <List <int> > immutablArraytOfLists = new List <List <int> > {
                list
            }.ToImmutableArray();
            await JsonSerializerWrapperForString.SerializeWrapper(immutablArraytOfLists, s_serializerOptionsPreserve);

            List <ImmutableArray <int> > listOfImmutableArrays = new List <ImmutableArray <int> > {
                immutableArr
            };
            await JsonSerializerWrapperForString.SerializeWrapper(listOfImmutableArrays, s_serializerOptionsPreserve);

            List <object> mixedListOfLists = new List <object> {
                list, immutableArr, list, immutableArr
            };
            await JsonSerializerWrapperForString.SerializeWrapper(mixedListOfLists, s_serializerOptionsPreserve);
        }
Exemplo n.º 19
0
        public async Task JsonPropertyNameAttribute()
        {
            {
                OverridePropertyNameDesignTime_TestClass obj = await JsonSerializerWrapperForString.DeserializeWrapper <OverridePropertyNameDesignTime_TestClass>(@"{""Blah"":1}");

                Assert.Equal(1, obj.myInt);

                obj.myObject = 2;

                string json = await JsonSerializerWrapperForString.SerializeWrapper(obj);

                Assert.Contains(@"""Blah"":1", json);
                Assert.Contains(@"""BlahObject"":2", json);
            }

            // The JsonPropertyNameAttribute should be unaffected by JsonNamingPolicy and PropertyNameCaseInsensitive.
            {
                var options = new JsonSerializerOptions();
                options.PropertyNamingPolicy        = JsonNamingPolicy.CamelCase;
                options.PropertyNameCaseInsensitive = true;

                OverridePropertyNameDesignTime_TestClass obj = await JsonSerializerWrapperForString.DeserializeWrapper <OverridePropertyNameDesignTime_TestClass>(@"{""Blah"":1}", options);

                Assert.Equal(1, obj.myInt);

                string json = await JsonSerializerWrapperForString.SerializeWrapper(obj);

                Assert.Contains(@"""Blah"":1", json);
            }
        }
        public async Task CamelCaseSerialize_ApplyDictionaryKeyPolicy()
        {
            const string JsonCamel = @"{""keyDict"":{""keyString"":""text"",""keyNumber"":1000,""keyBool"":true},""keyList"":[1,2,3]}";
            var          options   = new JsonSerializerOptions
            {
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };

            var obj = new Dictionary <string, object>();

            obj["KeyDict"] = new Dictionary <string, object>()
            {
                { "KeyString", "text" },
                { "KeyNumber", 1000 },
                { "KeyBool", true }
            };
            obj["KeyList"] = new List <int>()
            {
                1, 2, 3
            };

            var json = await JsonSerializerWrapperForString.SerializeWrapper(obj, new JsonSerializerOptions()
            {
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            });

            Assert.Equal(JsonCamel, json);
        }
        public async Task CamelCaseSerialize()
        {
            var options = new JsonSerializerOptions()
            {
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase // e.g. Key1 -> key1.
            };

            Dictionary <string, int>[] obj = new Dictionary <string, int>[]
            {
                new Dictionary <string, int>()
                {
                    { "Key1", 1 }, { "Key2", 2 }
                },
                new Dictionary <string, int>()
                {
                    { "Key1", 3 }, { "Key2", 4 }
                },
            };

            const string Json      = @"[{""Key1"":1,""Key2"":2},{""Key1"":3,""Key2"":4}]";
            const string JsonCamel = @"[{""key1"":1,""key2"":2},{""key1"":3,""key2"":4}]";

            // Without key policy option, serialize keys as they are.
            string json = await JsonSerializerWrapperForString.SerializeWrapper <object>(obj);

            Assert.Equal(Json, json);

            // With key policy option, serialize keys with camel casing.
            json = await JsonSerializerWrapperForString.SerializeWrapper <object>(obj, options);

            Assert.Equal(JsonCamel, json);
        }
Exemplo n.º 22
0
        public async Task SpecialCharacters()
        {
            ClassWithSpecialCharacters obj = new()
            {
                Baseline = 1,
                Schema   = 2,
                SmtpId   = 3,
                Emojies  = 4,
                ꀀ        = 5,
                YiIt_2   = 6
            };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(obj);

            Assert.Equal(
                "{\"Baseline\":1," +
                "\"$schema\":2," +
                "\"smtp-id\":3," +
                "\"\\uD83D\\uDE00\\uD83D\\uDE01\":4," +
                "\"\\uA000\":5," +
                "\"\\uA000_2\":6}", json);

            obj = await JsonSerializerWrapperForString.DeserializeWrapper <ClassWithSpecialCharacters>(json);

            Assert.Equal(1, obj.Baseline);
            Assert.Equal(2, obj.Schema);
            Assert.Equal(3, obj.SmtpId);
            Assert.Equal(4, obj.Emojies);
            Assert.Equal(5, obj.ꀀ);
            Assert.Equal(6, obj.YiIt_2);
        }
Exemplo n.º 23
0
        public async Task JsonNameConflictOnCamelCasingFail()
        {
            {
                // Baseline comparison - no options set.
                IntPropertyNamesDifferentByCaseOnly_TestClass obj = await JsonSerializerWrapperForString.DeserializeWrapper <IntPropertyNamesDifferentByCaseOnly_TestClass>("{}");

                await JsonSerializerWrapperForString.SerializeWrapper(obj);
            }

            {
                var options = new JsonSerializerOptions();
                options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

                await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <IntPropertyNamesDifferentByCaseOnly_TestClass>("{}", options));

                await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.SerializeWrapper(new IntPropertyNamesDifferentByCaseOnly_TestClass(), options));
            }

            {
                // Baseline comparison - no options set.
                ObjectPropertyNamesDifferentByCaseOnly_TestClass obj = await JsonSerializerWrapperForString.DeserializeWrapper <ObjectPropertyNamesDifferentByCaseOnly_TestClass>("{}");

                await JsonSerializerWrapperForString.SerializeWrapper(obj);
            }

            {
                var options = new JsonSerializerOptions();
                options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

                await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.DeserializeWrapper <ObjectPropertyNamesDifferentByCaseOnly_TestClass>("{}", options));

                await Assert.ThrowsAsync <InvalidOperationException>(async() => await JsonSerializerWrapperForString.SerializeWrapper(new ObjectPropertyNamesDifferentByCaseOnly_TestClass(), options));
            }
        }
Exemplo n.º 24
0
        public async Task WriteKeyValuePairWithNullValues()
        {
            {
                KeyValuePair <string, string> kvp = new KeyValuePair <string, string>("key", null);
                Assert.Equal(@"{""Key"":""key"",""Value"":null}", await JsonSerializerWrapperForString.SerializeWrapper(kvp));
            }

            {
                KeyValuePair <string, object> kvp = new KeyValuePair <string, object>("key", null);
                Assert.Equal(@"{""Key"":""key"",""Value"":null}", await JsonSerializerWrapperForString.SerializeWrapper(kvp));
            }

            {
                KeyValuePair <string, SimpleClassWithKeyValuePairs> kvp = new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null);
                Assert.Equal(@"{""Key"":""key"",""Value"":null}", await JsonSerializerWrapperForString.SerializeWrapper(kvp));
            }

            {
                KeyValuePair <string, KeyValuePair <string, string> > kvp = new KeyValuePair <string, KeyValuePair <string, string> >("key", new KeyValuePair <string, string>("key", null));
                Assert.Equal(@"{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}", await JsonSerializerWrapperForString.SerializeWrapper(kvp));
            }

            {
                KeyValuePair <string, KeyValuePair <string, object> > kvp = new KeyValuePair <string, KeyValuePair <string, object> >("key", new KeyValuePair <string, object>("key", null));
                Assert.Equal(@"{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}", await JsonSerializerWrapperForString.SerializeWrapper(kvp));
            }

            {
                KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> > kvp = new KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> >("key", new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null));
                Assert.Equal(@"{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}", await JsonSerializerWrapperForString.SerializeWrapper(kvp));
            }
        }
        public async Task WriteSimpleClassWithObjectImmutableArray()
        {
            SimpleTestClassWithObjectImmutableArray obj = new SimpleTestClassWithObjectImmutableArray();

            obj.Initialize();

            Assert.Equal(SimpleTestClassWithObjectImmutableArray.s_json, await JsonSerializerWrapperForString.SerializeWrapper(obj));
        }
Exemplo n.º 26
0
        public virtual async Task ThrowByDefaultOnLoop()
        {
            Employee a = new Employee();

            a.Manager = a;

            await Assert.ThrowsAsync <JsonException>(() => JsonSerializerWrapperForString.SerializeWrapper(a));
        }
        public async Task Struct_MultiplePublicParameterizedCtors_NoPublicParameterlessCtor_NoAttribute_Supported_UseParameterlessCtor()
        {
            var obj = await JsonSerializerWrapperForString.DeserializeWrapper <MultiplePublicParameterizedCtor_Struct>(@"{""myInt"":1,""myString"":""1""}");

            Assert.Equal(0, obj.MyInt);
            Assert.Null(obj.MyString);
            Assert.Equal(@"{""MyInt"":0,""MyString"":null}", await JsonSerializerWrapperForString.SerializeWrapper(obj));
        }
Exemplo n.º 28
0
        public async Task WritePrimitiveKeyValuePair()
        {
            KeyValuePair <string, int> input = new KeyValuePair <string, int>("Key", 123);

            string json = await JsonSerializerWrapperForString.SerializeWrapper(input);

            Assert.Equal(@"{""Key"":""Key"",""Value"":123}", json);
        }
        public async Task PublicParameterlessCtor_MultiplePublicParameterizedCtors_WithAttribute_Supported()
        {
            var obj = await JsonSerializerWrapperForString.DeserializeWrapper <ParameterlessCtor_MultiplePublicParameterizedCtor_WithAttribute>(@"{""MyInt"":1,""MyString"":""1""}");

            Assert.Equal(1, obj.MyInt);
            Assert.Null(obj.MyString);
            Assert.Equal(@"{""MyInt"":1,""MyString"":null}", await JsonSerializerWrapperForString.SerializeWrapper(obj));
        }
Exemplo n.º 30
0
        public async Task BoxedStructReferencePreservation_SiblingPrimitiveValues()
        {
            object box   = 42;
            var    array = new object[] { box, box };

            string json = await JsonSerializerWrapperForString.SerializeWrapper(array, s_serializerOptionsPreserve);

            Assert.Equal(@"[42,42]", json);
        }