Exemple #1
0
        public static string ToJson(object obj, JsonSettings settings)
        {
            var encoder = new JsonEncoder(settings);

            encoder.EncodeValue(obj, false);

            var json = _builder.ToString();

            _builder.Length = 0;
            return(json);
        }
Exemple #2
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()));
        }