Exemplo n.º 1
0
        public void SerializeObject_Then_DeserializeObjectAsObject_SampleWithStringEnumConverter_ShouldMatchJsonNet()
        {
            var obj             = CreateObject();
            var expectedOptions = new Newtonsoft.Json.JsonSerializerSettings {
                Converters = new[] { new Newtonsoft.Json.Converters.StringEnumConverter() }
            };
            var expectedJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj, expectedOptions);
            var expected     = Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson, typeof(object), expectedOptions);

            var actualOptions = new JsonSerializerSettings {
                Converters = new[] { new Converters.StringEnumConverter() }
            };
            var actualJson = JsonConvert.SerializeObject(obj, actualOptions);
            var actual     = JsonConvert.DeserializeObject(actualJson, typeof(object), actualOptions);

            JsonDeepEqualAssert.Equal(expected, actual, new JsonDeepEqualDiffOptions {
                DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK"
            });

            actual = JsonConvert.DeserializeObject <object>(expectedJson, actualOptions);
            JsonDeepEqualAssert.Equal(expected, actual, new JsonDeepEqualDiffOptions {
                DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK"
            });

            expected = Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson, expectedOptions);
            actual   = JsonConvert.DeserializeObject(actualJson, actualOptions);
            JsonDeepEqualAssert.Equal(expected, actual, new JsonDeepEqualDiffOptions {
                DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK"
            });
        }
Exemplo n.º 2
0
        public void SerializeObject_Then_DeserializeObject_TestEnum_ShouldMatchJsonNet(TestEnum input)
        {
            var expectedJson = Newtonsoft.Json.JsonConvert.SerializeObject(input);
            var expected     = Newtonsoft.Json.JsonConvert.DeserializeObject <TestEnum>(expectedJson);

            var actualJson = JsonConvert.SerializeObject(input);
            var actual     = JsonConvert.DeserializeObject <TestEnum>(actualJson);

            JsonDeepEqualAssert.Equal(expected, actual);
        }
Exemplo n.º 3
0
        public void DeserializeObjectAsObject_JsonWithComments_ShouldMatchJsonNet()
        {
            const string json = @"/* Comment */{ /* Comment */
""a"":1,
// Comment
""b"": // Comment
    2,
/* Comment */
""c"": /* Comment */ 3,
""d"": [ // Comment
4/* Comment */,/* Comment */ 5 /* Comment */ ]/* Comment */
/* Comment */}";

            var expected = Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(object));
            var actual   = JsonConvert.DeserializeObject(json, typeof(object));

            JsonDeepEqualAssert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        public void SerializeObject_Then_DeserializeObjectAsObject_Sample_ShouldMatchJsonNet()
        {
            var obj          = CreateObject();
            var expectedJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            var expected     = Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson, typeof(object));

            var actualJson = JsonConvert.SerializeObject(obj);
            var actual     = JsonConvert.DeserializeObject(actualJson, typeof(object));

            JsonDeepEqualAssert.Equal(expected, actual, new JsonDeepEqualDiffOptions {
                DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK"
            });

            actual = JsonConvert.DeserializeObject <object>(expectedJson);
            JsonDeepEqualAssert.Equal(expected, actual, new JsonDeepEqualDiffOptions {
                DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK"
            });

            expected = Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson);
            actual   = JsonConvert.DeserializeObject(actualJson);
            JsonDeepEqualAssert.Equal(expected, actual, new JsonDeepEqualDiffOptions {
                DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffK"
            });
        }
Exemplo n.º 5
0
        public void SerializeObject_Then_DeserializeObjectAsDictionary_Sample_ShouldMatchJsonNet(FloatParseHandling floatParseHandling, DateParseHandling dateParseHandling)
        {
            var obj              = CreateObject();
            var expectedJson     = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            var expectedSettings = new Newtonsoft.Json.JsonSerializerSettings
            {
                FloatParseHandling = Enum.Parse <Newtonsoft.Json.FloatParseHandling>(floatParseHandling.ToString()),
                DateParseHandling  = Enum.Parse <Newtonsoft.Json.DateParseHandling>(dateParseHandling.ToString()),
            };
            var expected = Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson, typeof(Dictionary <string, object>), expectedSettings);

            var actualJson     = JsonConvert.SerializeObject(obj);
            var actualSettings = new JsonSerializerSettings
            {
                FloatParseHandling = floatParseHandling,
                DateParseHandling  = dateParseHandling,
            };
            var actual = JsonConvert.DeserializeObject(actualJson, typeof(Dictionary <string, object>), actualSettings);

            JsonDeepEqualAssert.Equal(expected, actual);

            actual = JsonConvert.DeserializeObject <Dictionary <string, object> >(actualJson, actualSettings);
            JsonDeepEqualAssert.Equal(expected, actual);
        }
Exemplo n.º 6
0
        public void Read_ObjectWithComments_ShouldMatch(JsonCommentHandling commentHandling)
        {
            const string json = @"/* Comment */{ /* Comment */
""a"":1,
// Comment
""b"": // Comment
    2,
/* Comment */
""c"": /* Comment */ 3,
""d"": [ // Comment
4/* Comment */,/* Comment */ 5 /* Comment */ ]/* Comment */
/* Comment */}";

            var jsonReaderOptions = new JsonReaderOptions
            {
                CommentHandling = commentHandling,
            };
            var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json).AsSpan(), jsonReaderOptions);

            reader.Read();

            var converter = new ObjectConverter();
            var actual    = converter.Read(ref reader, typeof(object), new JsonSerializerOptions());

            var expected = new Dictionary <string, object>
            {
                ["a"] = 1L,
                ["b"] = 2L,
                ["c"] = 3L,
                ["d"] = new List <object> {
                    4L, 5L
                },
            };

            JsonDeepEqualAssert.Equal(expected, actual);
        }