Exemplo n.º 1
0
        public void Serialize_Deserialize_Test()
        {
            var textJsonSerializer = new TextJsonSerializer(_serializerOptions);

            // Assert.Null(textJsonSerializer.Serialize(s1));
            Assert.Equal("null", textJsonSerializer.Serialize(null));

            var o1 = new TextJsonSerializerClass1()
            {
                Age  = 3,
                Id   = 1,
                Name = "HaHa"
            };

            var json1         = textJsonSerializer.Serialize(o1);
            var deserializeO1 = textJsonSerializer.Deserialize(json1, typeof(TextJsonSerializerClass1));

            Assert.Equal(typeof(TextJsonSerializerClass1), deserializeO1.GetType());

            var jsonSerializer = _provider.GetService <IJsonSerializer>();
            var json2          = jsonSerializer.Serialize(o1);

            Assert.Equal(json1, json2);

            var deserializeO2 = textJsonSerializer.Deserialize <TextJsonSerializerClass1>(json1);

            Assert.Equal(o1.Id, deserializeO2.Id);
            Assert.Equal(o1.Name, deserializeO2.Name);
            Assert.Equal(o1.Age, deserializeO2.Age);
        }
Exemplo n.º 2
0
 private static JsonContentResult JsonResult(object response, int statusCode = StatusCodes.Status200OK)
 {
     return(new JsonContentResult()
     {
         StatusCode = statusCode,
         Content = TextJsonSerializer.Serialize(response)
     });
 }
Exemplo n.º 3
0
        public void TestSerialize()
        {
            var jsonSerializer = new TextJsonSerializer();

            var r = jsonSerializer.Serialize(new ValueObject
            {
                A = 10,
                B = 20,
            });

            Assert.AreNotEqual(r, null);
        }
Exemplo n.º 4
0
        public void TestDeSerialize()
        {
            var jsonSerializer = new TextJsonSerializer();

            var r = jsonSerializer.Serialize(new ValueObject
            {
                A = 10,
                B = 20,
            });

            Assert.AreNotEqual(r, null);

            var s = jsonSerializer.Deserialize <ValueObject>(r);

            Assert.AreEqual(s.A, 10);
        }
Exemplo n.º 5
0
        public override void OnException(ExceptionContext context)
        {
            var exception = context.Exception;
            var error     = GetResponseError(exception, out var statusCode);

            context.Result = new JsonContentResult()
            {
                StatusCode = statusCode,
                Content    = TextJsonSerializer.Serialize(error)
            };

            var logMessage = string.IsNullOrEmpty(error.ExceptionMessage)
                ? error.Message
                : $"{error.Message}\nException message: {error.ExceptionMessage}";

            _logger.LogError(exception, logMessage);

            base.OnException(context);
        }
Exemplo n.º 6
0
        public void Generics_Serialize_Deserialize_Test()
        {
            var textJsonSerializer = new TextJsonSerializer(_serializerOptions);

            var o = new JsonTestResult <JsonTestResultItem>()
            {
                Success = true,
                Data    = new JsonTestResultItem()
                {
                    Id   = "1",
                    Name = "virtual"
                }
            };

            var json = textJsonSerializer.Serialize(o);

            var o2 = textJsonSerializer.Deserialize <JsonTestResult <JsonTestResultItem> >(json);

            Assert.Equal(o.Success, o2.Success);
            Assert.Equal(o.Data.Id, o2.Data.Id);
            Assert.Equal(o.Data.Name, o2.Data.Name);
        }