Ejemplo 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);
        }
Ejemplo n.º 2
0
        public void Ctor_Test()
        {
            var jsonSerializer = new TextJsonSerializer(_serializerOptions);

            Assert.False(jsonSerializer.Options.Converters.Any());
            Assert.Equal(JavaScriptEncoder.UnsafeRelaxedJsonEscaping, jsonSerializer.Options.Encoder);
        }
Ejemplo n.º 3
0
 private static JsonContentResult JsonResult(object response, int statusCode = StatusCodes.Status200OK)
 {
     return(new JsonContentResult()
     {
         StatusCode = statusCode,
         Content = TextJsonSerializer.Serialize(response)
     });
 }
Ejemplo n.º 4
0
        public void TestSerialize()
        {
            var jsonSerializer = new TextJsonSerializer();

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

            Assert.AreNotEqual(r, null);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public static SdfConfigManager UseTextJsonSerializer(this SdfConfigManager sdfConfig, Action <JsonSerializerOptions> action)
        {
            TextJsonSerializer    textJsonSerializer    = new TextJsonSerializer();
            JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();

            jsonSerializerOptions.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory());
            jsonSerializerOptions.Converters.Add(new LongJsonConverter());
            jsonSerializerOptions.Converters.Add(new LongAvailableNullJsonConverter());
            jsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
            jsonSerializerOptions.Converters.Add(new DatetimeAvailableNullJsonConverter());
            jsonSerializerOptions.Converters.Add(new SdfEnumConverterFactory());
            jsonSerializerOptions.Converters.Add(new DoubleAvailableNullJsonConverter());
            if (action != null)
            {
                action(jsonSerializerOptions);
            }
            textJsonSerializer.JsonSerializerOptions = jsonSerializerOptions;
            sdfConfig.Register.RegisterSingleton <ISerializer>(textJsonSerializer);
            return(sdfConfig);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public void Setup()
        {
            var jsonNetOptions = new JsonSerializerSettings
            {
                Formatting = Formatting.None,
                Converters = new JsonConverter[] {
                    new IsoDateTimeConverter {
                        DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
                    },
                    new LongToStringConverter(),
                },

                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                NullValueHandling     = NullValueHandling.Ignore
            };

            var serializeOptions = new JsonSerializerOptions
            {
                WriteIndented               = false,
                IgnoreNullValues            = false,
                PropertyNameCaseInsensitive = true,
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };

            serializeOptions.Converters.Add(new ImplicitInt64Converter());
            serializeOptions.Converters.Add(new ImplicitUInt64Converter());
            serializeOptions.Converters.Add(new ImplicitDateTimeConverter());
            serializeOptions.Converters.Add(new ImplicitDateTimeOffsetConverter());

            var deserializeOptions = new JsonSerializerOptions
            {
                WriteIndented               = false,
                IgnoreNullValues            = true,
                PropertyNameCaseInsensitive = true,
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };

            deserializeOptions.Converters.Add(new ImplicitInt16Converter());
            deserializeOptions.Converters.Add(new ImplicitUInt16Converter());
            deserializeOptions.Converters.Add(new ImplicitInt32Converter());
            deserializeOptions.Converters.Add(new ImplicitUInt32Converter());
            deserializeOptions.Converters.Add(new ImplicitInt64Converter());
            deserializeOptions.Converters.Add(new ImplicitUInt64Converter());
            deserializeOptions.Converters.Add(new ImplicitDecimalConverter());
            deserializeOptions.Converters.Add(new ImplicitDoubleConverter());
            deserializeOptions.Converters.Add(new ImplicitSingleConverter());
            deserializeOptions.Converters.Add(new ImplicitByteConverter());
            deserializeOptions.Converters.Add(new ImplicitSByteConverter());
            deserializeOptions.Converters.Add(new ImplicitDateTimeConverter());
            deserializeOptions.Converters.Add(new ImplicitDateTimeOffsetConverter());

            serializeToStringObject = new
            {
                int16    = 111.ToString(),
                uint16   = ushort.MaxValue.ToString(),
                int32    = int.MaxValue.ToString(),
                uint32   = uint.MaxValue.ToString(),
                int32N   = "".ToString(),
                int64    = 12321.ToString(),
                uint64   = ulong.MaxValue.ToString(),
                boolean  = true,
                decimalV = decimal.MaxValue.ToString(),
                doubleV  = "1.123445767",
                floatV   = "1.1111",
                byteV    = byte.MaxValue.ToString(),
                sbyteV   = sbyte.MaxValue.ToString(),
                charV    = 'c',
                date     = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                date1    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                arr      = new string[] { "a", "b" },
                TestEnum = TestEnum.Approved
            };

            jsonFromString = JsonConvert.SerializeObject(serializeToStringObject, jsonNetOptions);

            serializeToStringClass = (TestClass)JsonConvert.DeserializeObject(jsonFromString, typeof(TestClass), jsonNetOptions);

            jsonNetSerializer  = new JsonNetSerializer(jsonNetOptions);
            textJsonSerializer = new TextJsonSerializer(serializeOptions, deserializeOptions);
            jilSerializer      = new JilSerializer();

            dataContractSerializer = new DataContractSerializer();
#if !NET5_0
            binarySerializer = new BinarySerializer();
#endif
            protoBufSerializer = new ProtoBufSerializer();

            NetSerializer.Serializer instance = new NetSerializer.Serializer(new[] { typeof(TestClass) });
            netSerializer = new NETSerializer(instance);

            dataContractFromString = dataContractSerializer.SerializeToString(serializeToStringClass);
#if !NET5_0
            binaryFromString = binarySerializer.SerializeToString(serializeToStringClass);
#endif
            protoBufFromString      = protoBufSerializer.SerializeToString(serializeToStringClass);
            netSerializerFromString = netSerializer.SerializeToString(serializeToStringClass);

            jilFromString = jilSerializer.SerializeToString(serializeToStringClass);
        }