Beispiel #1
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> .WriteObject(writer, value);

            writer.Flush();
        }
Beispiel #2
0
        public static string SerializeToString <T>(T value)
        {
            if (value == null)
            {
                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(4096);

            using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
            {
                JsvWriter <T> .WriteObject(writer, value);
            }
            return(sb.ToString());
        }
Beispiel #3
0
        public static void SerializeToWriter <T>(T value, TextWriter writer)
        {
            if (value == null)
            {
                return;
            }
            if (typeof(T) == typeof(string))
            {
                writer.Write(value);
                return;
            }
            if (typeof(T) == typeof(object))
            {
                if (typeof(T).IsAbstract || typeof(T).IsInterface)
                {
                    JsState.IsWritingDynamic = true;
                }
                SerializeToWriter(value, value.GetType(), writer);
                if (typeof(T).IsAbstract || typeof(T).IsInterface)
                {
                    JsState.IsWritingDynamic = false;
                }
                return;
            }

            JsvWriter <T> .WriteObject(writer, value);
        }
Beispiel #4
0
 public static void SerializeToStream <T>(T value, Stream stream)
 {
     using (var writer = new StreamWriter(stream, UTF8EncodingWithoutBom))
     {
         JsvWriter <T> .WriteObject(writer, value);
     }
 }
        public static void SerializeToWriter <T>(T value, TextWriter writer)
        {
            if (value == null)
            {
                return;
            }
            if (typeof(T) == typeof(string))
            {
                writer.Write(value);
                return;
            }
            if (typeof(T) == typeof(object))
            {
                if (typeof(T).IsAbstract() || typeof(T).IsInterface())
                {
                    JsState.IsWritingDynamic = true;
                }
                SerializeToWriter(value, value.GetType(), writer);
                if (typeof(T).IsAbstract() || typeof(T).IsInterface())
                {
                    JsState.IsWritingDynamic = false;
                }
                return;
            }

            try
            {
                SerializationVisitorTracker.TrackSerialization(writer);
                JsvWriter <T> .WriteObject(writer, value);
            }
            finally
            {
                SerializationVisitorTracker.UnTrackSerialization(writer);
            }
        }
        public void SerializeToWriter(T value, TextWriter writer)
        {
            if (value == null)
            {
                return;
            }
            if (typeof(T) == typeof(string))
            {
                writer.Write(value);
                return;
            }

            JsvWriter <T> .WriteObject(writer, value);
        }
        public string SerializeToString(T value)
        {
            if (value == null)
            {
                return(null);
            }
            if (typeof(T) == typeof(string))
            {
                return(value as string);
            }

            var writer = StringWriterThreadStatic.Allocate();

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

            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
Beispiel #8
0
        public string SerializeToString(T value)
        {
            if (value == null)
            {
                return(null);
            }
            if (typeof(T) == typeof(string))
            {
                return(value as string);
            }

            var sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                JsvWriter <T> .WriteObject(writer, value);
            }
            return(sb.ToString());
        }
Beispiel #9
0
        public static string SerializeToString <T>(T value)
        {
            if (value == null)
            {
                return(null);
            }
            if (typeof(T) == typeof(string))
            {
                return(value as string);
            }

            var sb = new StringBuilder(4096);

            using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
            {
                JsvWriter <T> .WriteObject(writer, value);
            }
            return(sb.ToString());
        }