Beispiel #1
0
        public static string SerializeToString <T>(T value)
        {
            if (value == null || value is Delegate)
            {
                return(null);
            }
            if (typeof(T) == typeof(object))
            {
                return(SerializeToString(value, value.GetType()));
            }
            if (typeof(T).IsAbstract() || typeof(T).IsInterface())
            {
                JsState.IsWritingDynamic = true;
                var result = SerializeToString(value, value.GetType());
                JsState.IsWritingDynamic = false;
                return(result);
            }

            using (var sb = StringBuilderWriter.Create())
            {
                if (typeof(T) == typeof(string))
                {
                    JsonUtils.WriteString(sb.Writer, value as string);
                }
                else
                {
                    JsonWriter <T> .WriteRootObject(sb.Writer, value);
                }
                return(sb.ToString());
            }
        }
        public static string SerializeToString(object value, Type type)
        {
            if (value == null)
            {
                return(null);
            }
            if (type == typeof(string))
            {
                return(value as string);
            }

            using (var sb = StringBuilderWriter.Create())
            {
                JsvWriter.GetWriteFn(type)(sb.Writer, value);

                return(sb.ToString());
            }
        }