Example #1
0
        /// <summary>
        /// encodes <paramref name="obj"/> to a json string
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string ToJson(object obj, JsonSettings options = null)
        {
            // Invoke methods tagged with [BeforeEncode] attribute.
            if (obj != null)
            {
                var type = obj.GetType();
                if (!(type.IsEnum || type.IsPrimitive || type.IsArray))
                {
                    foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                    {
                        if (method.IsDefined(JsonConstants.beforeEncodeAttrType) && method.GetParameters().Length == 0)
                        {
                            method.Invoke(obj, null);
                        }
                    }
                }
            }

            return(JsonEncoder.ToJson(obj, options ?? new JsonSettings()));
        }
Example #2
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));
 }
Example #3
0
 /// <summary>
 /// decodes <paramref name="jsonString"/> into an object of type T
 /// </summary>
 /// <returns>The json.</returns>
 /// <param name="jsonString">Json string.</param>
 /// <param name="settings">Settings.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static T FromJson <T>(string jsonString, JsonSettings settings = null)
 {
     return((T)FromJson(jsonString, typeof(T), settings));
 }
Example #4
0
 JsonDecoder(string jsonString, JsonSettings settings = null)
 {
     _json     = new StringReader(jsonString);
     _settings = settings;
 }
Example #5
0
 JsonEncoder(JsonSettings settings)
 {
     _settings = settings;
     indent    = 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));
     }
 }