public void JsonSerializer_Dictionary_DateTimeOffset()
        {
            var dict = new Dictionary <string, object>();

            dict["Timestamp"] = DateTimeOffset.UtcNow;
            var bts    = Utf8JsonSerializer.Serialize(dict, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);
            var newdt  = Utf8JsonSerializer.Deserialize <Dictionary <string, object> >(bts, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);
            var newdt2 = JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(newdt, "Timestamp");

            Assert.Equal(dict["Timestamp"], newdt2);
        }
        public void JsonSerializer_DateTimeOffset()
        {
            var timestamp = DateTimeOffset.UtcNow;
            var bts       = Utf8JsonSerializer.Serialize(timestamp, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);
            var newdt     = Utf8JsonSerializer.Deserialize <DateTimeOffset>(bts, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);

            Assert.Equal(timestamp, newdt);
            var newdt1 = JsonConvert.DeserializeObject <DateTimeOffset>(System.Text.Encoding.UTF8.GetString(bts));

            Assert.Equal(timestamp, newdt1);
            var newdt2 = JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(System.Text.Encoding.UTF8.GetString(bts));

            Assert.Equal(timestamp, newdt2);
        }
        public void JsonObjectTypeDeserializer_Test()
        {
            var dict = new Dictionary <string, object>(StringComparer.Ordinal)
            {
                ["name"]           = "apple",
                ["Age"]            = 18,
                ["Id"]             = Guid.NewGuid(),
                ["createDateTime"] = DateTime.Now,
                ["combGuid"]       = CombGuid.NewComb(),
                ["dtOffset"]       = DateTimeOffset.Now,
                ["double"]         = 10.56d,
                ["float"]          = 20.06f,
                ["decimal"]        = 8.000056M,
                ["bytes"]          = new byte[] { 1 },
                ["emptybytes"]     = new byte[0],
                ["Worten1"]        = 0,
                ["Worten"]         = "second",
            };

            var json    = JsonConvertX.SerializeObject(dict, JsonConvertX.DefaultSettings);
            var newDict = JsonConvertX.DeserializeObject <Dictionary <string, object> >(json, JsonConvertX.DefaultSettings);

            Assert.Equal(dict["name"], JsonObjectTypeDeserializer.Deserialize <string>(newDict, "name"));
            Assert.Equal(dict["Age"], JsonObjectTypeDeserializer.Deserialize <int>(newDict, "Age"));
            Assert.Equal(dict["Id"], JsonObjectTypeDeserializer.Deserialize <Guid>(newDict, "Id"));
            // camelcase key test
            Assert.Equal(dict["createDateTime"], JsonObjectTypeDeserializer.Deserialize <DateTime>(newDict, "CreateDateTime"));
            Assert.Equal(dict["createDateTime"], JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(newDict, "CreateDateTime").DateTime);
            Assert.Equal(dict["combGuid"], JsonObjectTypeDeserializer.Deserialize <CombGuid>(newDict, "combGuid"));
            Assert.Equal(dict["dtOffset"], JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(newDict, "dtOffset"));
            Assert.Equal(dict["double"], JsonObjectTypeDeserializer.Deserialize <double>(newDict, "double"));
            Assert.Equal(dict["float"], JsonObjectTypeDeserializer.Deserialize <float>(newDict, "float"));
            Assert.Equal(dict["decimal"], JsonObjectTypeDeserializer.Deserialize <decimal>(newDict, "decimal"));
            Assert.Equal(dict["bytes"], JsonObjectTypeDeserializer.Deserialize <byte[]>(newDict, "bytes"));
            Assert.Equal(dict["emptybytes"], JsonObjectTypeDeserializer.Deserialize <byte[]>(newDict, "emptybytes"));
            Assert.Equal(Antworten.First, JsonObjectTypeDeserializer.Deserialize <Antworten>(newDict, "Worten1"));
            Assert.Equal(Antworten.Second, JsonObjectTypeDeserializer.Deserialize <Antworten>(newDict, "Worten"));

            Assert.Equal(dict["name"], newDict.Deserialize <string>("name"));
            Assert.Equal(dict["Age"], newDict.Deserialize <int>("Age"));
            Assert.Equal(dict["Id"], newDict.Deserialize <Guid>("Id"));
            // camelcase key test
            Assert.Equal(dict["createDateTime"], newDict.Deserialize <DateTime>("CreateDateTime"));
            Assert.Equal(dict["combGuid"], newDict.Deserialize <CombGuid>("combGuid"));
            Assert.Equal(dict["dtOffset"], newDict.Deserialize <DateTimeOffset>("dtOffset"));
            Assert.Equal(dict["double"], newDict.Deserialize <double>("double"));
            Assert.Equal(dict["float"], newDict.Deserialize <float>("float"));
            Assert.Equal(dict["decimal"], newDict.Deserialize <decimal>("decimal"));
            Assert.Equal(dict["bytes"], newDict.Deserialize <byte[]>("bytes"));
            Assert.Equal(dict["emptybytes"], newDict.Deserialize <byte[]>("emptybytes"));

            Assert.Equal(dict["name"], dict.Deserialize <string>("name"));
            Assert.Equal(dict["Age"], dict.Deserialize <int>("Age"));
            Assert.Equal(dict["Id"], dict.Deserialize <Guid>("Id"));
            // camelcase key test
            Assert.Equal(dict["createDateTime"], dict.Deserialize <DateTime>("CreateDateTime"));
            Assert.Equal(dict["combGuid"], dict.Deserialize <CombGuid>("combGuid"));
            Assert.Equal(dict["dtOffset"], dict.Deserialize <DateTimeOffset>("dtOffset"));
            Assert.Equal(dict["double"], dict.Deserialize <double>("double"));
            Assert.Equal(dict["float"], dict.Deserialize <float>("float"));
            Assert.Equal(dict["decimal"], dict.Deserialize <decimal>("decimal"));
            Assert.Equal(dict["bytes"], dict.Deserialize <byte[]>("bytes"));
            Assert.Equal(dict["emptybytes"], dict.Deserialize <byte[]>("emptybytes"));

            var combGuid = CombGuid.NewComb();
            var jsonStr  = combGuid.ToString();

            Assert.Equal(combGuid, JsonObjectTypeDeserializer.Deserialize <CombGuid>(jsonStr));
            Assert.Equal(combGuid, JsonObjectTypeDeserializer.Deserialize <CombGuid>(combGuid));
            Assert.Equal((Guid)combGuid, JsonObjectTypeDeserializer.Deserialize <Guid>(combGuid));
            Assert.Equal(combGuid.ToString(), JsonObjectTypeDeserializer.Deserialize <string>(combGuid));
            Assert.Equal(combGuid.ToByteArray(), JsonObjectTypeDeserializer.Deserialize <byte[]>(combGuid));
            Assert.Equal(combGuid.DateTime, JsonObjectTypeDeserializer.Deserialize <DateTime>(combGuid));
            Assert.Equal(DateTime.UtcNow.Date, JsonObjectTypeDeserializer.Deserialize <DateTime>(combGuid).Date);
            Assert.Equal(new DateTimeOffset(combGuid.DateTime), JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(combGuid));
        }
Esempio n. 4
0
 public object Deserialize(object value, Type objectType, bool allowNull = false)
 {
     return(JsonObjectTypeDeserializer.Deserialize(value, objectType, allowNull));
 }
Esempio n. 5
0
 public TValue Deserialize <TValue>(object value, TValue defaultValue)
 {
     return(JsonObjectTypeDeserializer.Deserialize <TValue>(value, defaultValue));
 }
Esempio n. 6
0
 public TValue Deserialize <TValue>(object value)
 {
     return(JsonObjectTypeDeserializer.Deserialize <TValue>(value, false));
 }
Esempio n. 7
0
 public TValue Deserialize <TValue>(IDictionary <string, object> dictionary, string key, TValue defaultValue)
 {
     return(JsonObjectTypeDeserializer.Deserialize <TValue>(dictionary, key, defaultValue));
 }
Esempio n. 8
0
 public TValue Deserialize <TValue>(IDictionary <string, object> dictionary)
 {
     return(JsonObjectTypeDeserializer.Deserialize <TValue>(dictionary));
 }