예제 #1
0
 public static void SerializeToWriter <T>(T value, TextWriter writer)
 {
     if (value == null)
     {
         return;
     }
     if (typeof(T) == typeof(string))
     {
         writer.Write(value);
     }
     else if (typeof(T) == typeof(object))
     {
         SerializeToWriter(value, value.GetType(), writer);
     }
     else if (typeof(T).IsAbstract || typeof(T).IsInterface)
     {
         JsState.IsWritingDynamic = false;
         SerializeToWriter(value, value.GetType(), writer);
         JsState.IsWritingDynamic = true;
     }
     else
     {
         JsvWriter <T> .WriteRootObject(writer, value);
     }
 }
예제 #2
0
        public static string SerializeToString <T>(T value)
        {
            if (value == null || value is Delegate)
            {
                return(null);
            }
            if (typeof(T) == typeof(string))
            {
                return(value as string);
            }
            if (typeof(T) == typeof(object) || typeof(T).IsAbstract() || typeof(T).IsInterface())
            {
                if (typeof(T).IsAbstract() || typeof(T).IsInterface())
                {
                    JsState.IsWritingDynamic = true;
                }
                var result = SerializeToString(value, value.GetType());
                if (typeof(T).IsAbstract() || typeof(T).IsInterface())
                {
                    JsState.IsWritingDynamic = false;
                }
                return(result);
            }

            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
            {
                JsvWriter <T> .WriteRootObject(writer, value);
            }
            return(sb.ToString());
        }
예제 #3
0
        public static void SerializeToStream <T>(T value, Stream stream)
        {
            if (value == null)
            {
                return;
            }
            if (typeof(T) == typeof(object))
            {
                if (typeof(T).IsAbstract() || typeof(T).IsInterface())
                {
                    JsState.IsWritingDynamic = true;
                }
                SerializeToStream(value, value.GetType(), stream);
                if (typeof(T).IsAbstract() || typeof(T).IsInterface())
                {
                    JsState.IsWritingDynamic = false;
                }
                return;
            }

            var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);

            JsvWriter <T> .WriteRootObject(writer, value);

            writer.Flush();
        }
        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())
            {
                JsvWriter <T> .WriteRootObject(sb.Writer, value);

                return(sb.ToString());
            }
        }
예제 #5
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);
            }

            var writer = StringWriterThreadStatic.Allocate();

            JsvWriter <T> .WriteRootObject(writer, value);

            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
        public static void SerializeToStream <T>(T value, Stream stream)
        {
            if (value == null)
            {
                return;
            }
            if (typeof(T) == typeof(object))
            {
                SerializeToStream(value, value.GetType(), stream);
            }
            else if (typeof(T).IsAbstract || typeof(T).IsInterface)
            {
                JsState.IsWritingDynamic = false;
                SerializeToStream(value, value.GetType(), stream);
                JsState.IsWritingDynamic = true;
            }
            else
            {
                var writer = new StreamWriter(stream, PclExport.Instance.GetUseEncoding(false));
                JsvWriter <T> .WriteRootObject(writer, value);

                writer.Flush();
            }
        }