Exemple #1
0
        public void KeyFormatterFeature_filter_key_should_be_correct()
        {
            JsonSerializerOption option1 = new JsonSerializerOption
            {
                GlobalKeyFormat = (key, parentType, handler) =>
                {
                    if (parentType == typeof(R01_User))
                    {
                        return(key.Substring(4));
                    }

                    return(key);
                }
            };

            var ru   = new R01_User();
            var json = JsonSerializer.ToJson(ru, option1);

            Assert.IsTrue(JsonValidator.IsValid(json));
            Assert.AreEqual("{\"Name\":null,\"Age\":0}", json);

            JsonDeserializeOption option2 = new JsonDeserializeOption
            {
                GlobalKeyFormat = (key, parentType) =>
                {
                    if (parentType == typeof(R01_User))
                    {
                        return("R01_" + key);
                    }

                    return(key);
                }
            };

            var p = JsonSerializer.ToObject <R01_User>(json, option2);

            Assert.AreEqual(ru.R01_Age, p.R01_Age);
            Assert.AreEqual(ru.R01_Name, p.R01_Name);
        }
 public static T FromKoobooJson <T>(this string json, JsonDeserializeOption option)
 {
     return(KoobooJsonHelper.Deserialize <T>(json, option));
 }
Exemple #3
0
 public static T Deserialize <T>(string json, JsonDeserializeOption option)
 {
     return(string.IsNullOrWhiteSpace(json) ? default : JsonSerializer.ToObject <T>(json, option));
 }
Exemple #4
0
 public static object Deserialize(string json, Type type, JsonDeserializeOption option)
 {
     return(string.IsNullOrWhiteSpace(json) ? default : JsonSerializer.ToObject(json, type, option));
 }