예제 #1
0
        public static string ToNson(object obj, NsonSettings settings)
        {
            NsonEncoder encoder = new NsonEncoder(settings);

            encoder.EncodeValue(obj);
            return(encoder.NsonString());
        }
예제 #2
0
        /// <summary>
        /// encodes <paramref name="obj"/> to a nson string
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string ToNson(object obj, NsonSettings options = null)
        {
            // Invoke methods tagged with [BeforeEncode] attribute.
            if (obj != null)
            {
                Type type = obj.GetType();
                if (!(type.IsEnum || type.IsPrimitive || type.IsArray))
                {
                    foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                    {
                        if (method.IsDefined(NsonConstants.beforeEncodeAttrType) && method.GetParameters().Length == 0)
                        {
                            method.Invoke(obj, null);
                        }
                    }
                }
            }

            return(NsonEncoder.ToNson(obj, options ?? new NsonSettings()));
        }