Beispiel #1
0
        public string SerializeToString(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 = JsonSerializer.SerializeToString(value, value.GetType());
                if (typeof(T).IsAbstract || typeof(T).IsInterface)
                {
                    JsState.IsWritingDynamic = false;
                }
                return(result);
            }

            var writer = StringWriterThreadStatic.Allocate();

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

            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
Beispiel #2
0
        public static string SerializeToCsv <T>(IEnumerable <T> records)
        {
            var writer = StringWriterThreadStatic.Allocate();

            writer.WriteCsv(records);
            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
Beispiel #3
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();

            if (typeof(T) == typeof(string))
            {
                JsonUtils.WriteString(writer, value as string);
            }
            else
            {
                JsonWriter <T> .WriteRootObject(writer, value);
            }
            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
Beispiel #4
0
        public static string SerializeToString <T>(T value)
        {
            var writer = StringWriterThreadStatic.Allocate();

            GetWriteFn(value.GetType())(writer, value);
            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
Beispiel #5
0
        public static string SerializeToString(object value, Type type)
        {
            if (value == null)
            {
                return(null);
            }
            if (type == typeof(string))
            {
                return(value as string);
            }

            var writer = StringWriterThreadStatic.Allocate();

            JsvWriter.GetWriteFn(type)(writer, value);
            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }
Beispiel #6
0
        public static string SerializeToString <T>(T value)
        {
            if (value == null)
            {
                return(null);
            }
            if (typeof(T) == typeof(string))
            {
                return(value as string);
            }

            var writer = StringWriterThreadStatic.Allocate();

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

            return(StringWriterThreadStatic.ReturnAndFree(writer));
        }