Пример #1
0
        public BaseJsonSerializer()
        {
            offset = 0;

            hGlobal = HGlobalCache <char> .OccupancyInstance();

            Expand(255);
        }
Пример #2
0
        public static void SerializeObject <T>(T value, HGlobalCache <char> hGCache)
        {
            var jsonSerializer = new JsonSerializer <JsonSerializeModes.SimpleMode>(hGCache, DefaultMaxDepth);

            ValueInterface <T> .WriteValue(jsonSerializer, value);

            jsonSerializer.Flush();
        }
Пример #3
0
        public BaseJsonSerializer()
        {
            offset = 0;

            hGlobal = HGlobalCache.ThreadInstance;

            Expand(255);
        }
Пример #4
0
        public object Deserialize(TextReader textReader, Type type)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = hGCache.Buffer(textReader);

                return(Deserialize(hGCache.GetPointer(), length, type));
            }
        }
        public static object DeserializeObject(TextReader textReader, Type type, JsonFormatterOptions options)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = hGCache.Buffer(textReader);

                return(DeserializeObject(hGCache.GetPointer(), length, type, options));
            }
        }
        public static T DeserializeObject <T>(TextReader textReader)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = hGCache.Buffer(textReader);

                return(DeserializeObject <T>(hGCache.GetPointer(), length));
            }
        }
Пример #7
0
        public void Serialize <T>(T value, HGlobalCache <char> hGCache)
        {
            var options = Options;

            if ((options & ReferenceOptions) != 0)
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.ReferenceMode>(options, hGCache, MaxDepth)
                {
                    jsonFormatter = this,
                    References    = new JsonReferenceWriter()
                };

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = IndentedChars;
                    jsonSerializer.LineBreakChars = LineBreakChars;
                    jsonSerializer.MiddleChars    = MiddleChars;
                }

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
            else if ((options & ComplexOptions) != 0)
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.ComplexMode>(options, hGCache, MaxDepth)
                {
                    jsonFormatter = this
                };

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = IndentedChars;
                    jsonSerializer.LineBreakChars = LineBreakChars;
                    jsonSerializer.MiddleChars    = MiddleChars;
                }

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
            else
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.SimpleMode>(options, hGCache, MaxDepth)
                {
                    jsonFormatter = this
                };

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
        }
Пример #8
0
        public static async Task <T> DeserializeObjectAsync <T>(TextReader textReader)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = await hGCache.BufferAsync(textReader);

                unsafe
                {
                    return(DeserializeObject <T>(hGCache.GetPointer(), length));
                }
            }
        }
Пример #9
0
        public async Task <object> DeserializeAsync(TextReader textReader, Type type)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = await hGCache.BufferAsync(textReader);

                unsafe
                {
                    return(Deserialize(hGCache.GetPointer(), length, type));
                }
            }
        }
Пример #10
0
        public static async Task <object> DeserializeObjectAsync(TextReader textReader, Type type, JsonFormatterOptions options)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = await hGCache.BufferAsync(textReader);

                unsafe
                {
                    return(DeserializeObject(hGCache.GetPointer(), length, type, options));
                }
            }
        }
Пример #11
0
        public static void SerializeObject <T>(T value, HGlobalCache <char> hGCache, JsonFormatterOptions options)
        {
            if ((options & ReferenceOptions) != 0)
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.ReferenceMode>(options, hGCache, DefaultMaxDepth)
                {
                    References = new JsonReferenceWriter()
                };

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = DefaultIndentedChars;
                    jsonSerializer.LineBreakChars = DefaultLineCharsBreak;
                    jsonSerializer.MiddleChars    = DefaultMiddleChars;
                }

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
            else if ((options & ComplexOptions) != 0)
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.ComplexMode>(options, hGCache, DefaultMaxDepth);

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = DefaultIndentedChars;
                    jsonSerializer.LineBreakChars = DefaultLineCharsBreak;
                    jsonSerializer.MiddleChars    = DefaultMiddleChars;
                }

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
            else
            {
                var jsonSerializer = new JsonSerializer <DefaultSerializeMode>(options, hGCache, DefaultMaxDepth);

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
        }
Пример #12
0
        /// <summary>
        /// 将 Stream 的内容缓存到 HGlobalCache 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">Stream</param>
        /// <param name="encoding">编码</param>
        /// <returns>返回缓冲的长度</returns>
        public static void ReadFrom(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            hGBytes.ReadFrom(stream);

            var maxCharsCount = encoding.GetMaxCharCount(hGBytes.Count);

            if (maxCharsCount >= hGCache.Capacity)
            {
                hGCache.Expand(maxCharsCount);
            }

            hGCache.Count = encoding.GetChars(
                hGBytes.GetPointer(),
                hGBytes.Count,
                hGCache.GetPointer(),
                hGCache.Capacity);

            BytesPool.Return(hGBytes);
        }
Пример #13
0
        /// <summary>
        /// 将 HGlobalCache 中的内容写入到流中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">流</param>
        /// <param name="encoding">编码</param>
        public static void WriteTo(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            var maxBytesCount = encoding.GetMaxByteCount(hGCache.Count);

            if (maxBytesCount > hGBytes.Capacity)
            {
                hGBytes.Expand(maxBytesCount - hGBytes.Capacity);
            }

            hGBytes.Count = encoding.GetBytes(
                hGCache.GetPointer(),
                hGCache.Count,
                hGBytes.GetPointer(),
                hGBytes.Capacity);

            hGBytes.WriteTo(stream);

            BytesPool.Return(hGBytes);
        }
Пример #14
0
        static void SerializeObject <T, TMode>(T value, HGlobalCache <char> hGCache, TextWriter textWriter, JsonFormatterOptions options) where TMode : struct
        {
            var jsonSerializer = new JsonSerializer <TMode>(hGCache, DefaultMaxDepth, textWriter, options);

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode))
            {
                jsonSerializer.References = new JsonReferenceWriter();
            }

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode) || typeof(TMode) == typeof(JsonSerializeModes.ComplexMode))
            {
                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = DefaultIndentedChars;
                    jsonSerializer.LineBreakChars = DefaultLineCharsBreak;
                    jsonSerializer.MiddleChars    = DefaultMiddleChars;
                }
            }

            ValueInterface <T> .WriteValue(jsonSerializer, value);

            jsonSerializer.Flush();
        }
Пример #15
0
        /// <summary>
        /// 异步将 Stream 的内容缓存到 HGlobalCache 中。
        /// </summary>
        /// <param name="hGCache">HGlobalCache</param>
        /// <param name="stream">Stream</param>
        /// <param name="encoding">编码</param>
        /// <returns>返回缓冲的长度</returns>
        public static async Task ReadFromAsync(this HGlobalCache <char> hGCache, Stream stream, Encoding encoding)
        {
            var hGBytes = BytesPool.Rent();

            await hGBytes.ReadFromAsync(stream);

            var maxCharsCount = encoding.GetMaxCharCount(hGBytes.Count);

            if (maxCharsCount > hGCache.Capacity)
            {
                hGCache.Expand(maxCharsCount - hGCache.Capacity);
            }

            unsafe
            {
                hGCache.Count = encoding.GetChars(
                    hGBytes.GetPointer(),
                    hGBytes.Count,
                    hGCache.GetPointer(),
                    hGCache.Capacity);
            }

            BytesPool.Return(hGBytes);
        }
Пример #16
0
        void Serialize <T, TMode>(T value, HGlobalCache <char> hGCache, JsonFormatterOptions options) where TMode : struct
        {
            var jsonSerializer = new JsonSerializer <TMode>(this, hGCache, MaxDepth, options);

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode))
            {
                jsonSerializer.References = new JsonReferenceWriter();
            }

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode) || typeof(TMode) == typeof(JsonSerializeModes.ComplexMode))
            {
                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = IndentedChars;
                    jsonSerializer.LineBreakChars = LineBreakChars;
                    jsonSerializer.MiddleChars    = MiddleChars;
                }
            }

            ValueInterface <T> .WriteValue(jsonSerializer, value);


            jsonSerializer.Flush();
        }
Пример #17
0
 public JsonSerializer(HGlobalCache <char> hGCache, int maxDepth, TextWriter textWriter)
     : this(hGCache, maxDepth)
 {
     TextWriter = textWriter;
 }
Пример #18
0
 public static T DeserializeObject <T>(HGlobalCache <char> hGCache, JsonFormatterOptions options)
 {
     return(DeserializeObject <T>(hGCache.GetPointer(), hGCache.Count, options));
 }
Пример #19
0
 public static object DeserializeObject(HGlobalCache <char> hGCache, Type type, JsonFormatterOptions options)
 {
     return(DeserializeObject(hGCache.GetPointer(), hGCache.Count, type, options));
 }
Пример #20
0
 public static IJsonReader CreateJsonReader(HGlobalCache <char> hGCache)
 {
     return(CreateJsonReader(hGCache.GetPointer(), hGCache.Count));
 }
Пример #21
0
 /// <summary>
 /// 将 JSON 字符串反序列化到指定的数据写入器中。
 /// </summary>
 /// <param name="hGCache">JSON 字符串缓存</param>
 /// <param name="dataWriter">数据写入器</param>
 public void DeserializeTo(HGlobalCache <char> hGCache, IDataWriter dataWriter)
 {
     DeserializeTo(hGCache.GetPointer(), hGCache.Count, dataWriter);
 }
Пример #22
0
 public JsonSerializer(HGlobalCache <char> hGCache, int maxDepth)
 {
     HGCache  = hGCache;
     MaxDepth = maxDepth;
 }
Пример #23
0
 public object Deserialize(HGlobalCache <char> hGCache, Type type)
 {
     return(Deserialize(hGCache.GetPointer(), hGCache.Count, type));
 }
Пример #24
0
 public T Deserialize <T>(HGlobalCache <char> hGCache)
 {
     return(Deserialize <T>(hGCache.GetPointer(), hGCache.Count));
 }
Пример #25
0
 public JsonSerializer(HGlobalCache <char> hGCache, int maxDepth, TextWriter textWriter, JsonFormatterOptions options)
     : this(hGCache, maxDepth)
 {
     TextWriter = textWriter;
     Options    = options;
 }
Пример #26
0
 public JsonSerializer(JsonFormatter jsonFormatter, HGlobalCache <char> hGCache, int maxDepth, TextWriter textWriter, JsonFormatterOptions options)
     : this(jsonFormatter, hGCache, maxDepth, options)
 {
     TextWriter = textWriter;
 }
Пример #27
0
 public JsonSerializer(JsonFormatter jsonFormatter, HGlobalCache <char> hGCache, int maxDepth, JsonFormatterOptions options)
     : this(hGCache, maxDepth)
 {
     JsonFormatter = jsonFormatter;
     Options       = options;
 }