Пример #1
0
 /// <summary>
 /// overwrites the data on <paramref name="obj"/> with the data serialized from JSON. This will only
 /// work for custom objects, Dictionarys and Lists.
 /// </summary>
 /// <param name="jsonString">Json string.</param>
 /// <param name="obj">Object.</param>
 /// <param name="settings">Settings.</param>
 public static void FromJsonOverwrite(string jsonString, object obj, JsonSettings settings = null)
 {
     using (var instance = new JsonDecoder(jsonString, settings))
     {
         var type = obj.GetType();
         if (obj is IDictionary)
         {
             instance.DecodeDictionary(type, obj);
         }
         else if (obj is ICollection)
         {
             instance.DecodeList(type, obj);
         }
         else
         {
             try
             {
                 instance.DecodeObject(type, obj);
             }
             catch (Exception e)
             {
                 throw new DecodeException(
                           $"{nameof(FromJsonOverwrite)} only support Dictionary, List and custom objects. Use {nameof(FromJson)} for other types",
                           e);
             }
         }
     }
 }
Пример #2
0
 /// <summary>
 /// decodes <paramref name="jsonString"/> into standard system and generic types
 /// </summary>
 /// <returns>The json.</returns>
 /// <param name="jsonString">Json string.</param>
 /// <param name="settings">Settings.</param>
 public static object FromJson(string jsonString, JsonSettings settings = null)
 {
     using (var instance = new JsonDecoder(jsonString, settings))
     {
         return(instance.DecodeValueUntyped(instance.GetNextToken()));
     }
 }
Пример #3
0
 /// <summary>
 /// decods <paramref name="json"/> into a strongly typed object
 /// </summary>
 /// <param name="json"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static T FromJson <T>(string json, JsonSettings settings = null)
 {
     System.Diagnostics.Debug.Assert(json != null);
     return(JsonDecoder.FromJson <T>(json, settings));
 }
Пример #4
0
 /// <summary>
 /// overwrites any data on <paramref name="item"/> with the data found in the json string
 /// </summary>
 /// <param name="json">Json.</param>
 /// <param name="item">Item.</param>
 public static void FromJsonOverwrite(string json, object item)
 {
     System.Diagnostics.Debug.Assert(json != null);
     JsonDecoder.FromJsonOverwrite(json, item);
 }
Пример #5
0
 /// <summary>
 /// decodes <paramref name="jsonString"/> into an object of type <paramref name="type"/>
 /// </summary>
 /// <returns>The json.</returns>
 /// <param name="jsonString">Json string.</param>
 /// <param name="type">Type.</param>
 /// <param name="settings">Settings.</param>
 public static object FromJson(string jsonString, Type type, JsonSettings settings = null)
 {
     using (JsonDecoder instance = new JsonDecoder(jsonString, settings)) {
         return(instance.DecodeValue(instance.GetNextToken(), type));
     }
 }