Ejemplo n.º 1
0
            public JsonBinaryReader(
                ReadOnlyMemory <byte> buffer,
                IReadOnlyJsonStringDictionary jsonStringDictionary = null)
            {
                if (buffer.Length < 2)
                {
                    throw new ArgumentException($"{nameof(buffer)} must have at least two byte.");
                }

                if (buffer.Span[0] != (byte)JsonSerializationFormat.Binary)
                {
                    throw new ArgumentNullException("buffer must be binary encoded.");
                }

                // offset for the 0x80 (128) binary serialization type marker.
                buffer = buffer.Slice(1);

                // Only navigate the outer most json value and trim off trailing bytes
                int jsonValueLength = JsonBinaryEncoding.GetValueLength(buffer.Span);

                if (buffer.Length < jsonValueLength)
                {
                    throw new ArgumentException("buffer is shorter than the length prefix.");
                }

                buffer = buffer.Slice(0, jsonValueLength);

                this.jsonBinaryBuffer       = new JsonBinaryMemoryReader(buffer);
                this.arrayAndObjectEndStack = new Stack <int>();
                this.jsonStringDictionary   = jsonStringDictionary;
            }
            public JsonBinaryReader(
                ReadOnlyMemory<byte> buffer,
                IReadOnlyJsonStringDictionary jsonStringDictionary = null)
            {
                if (buffer.IsEmpty)
                {
                    throw new ArgumentException($"{nameof(buffer)} must not be empty.");
                }

                // Only navigate the outer most json value and trim off trailing bytes
                int jsonValueLength = JsonBinaryEncoding.GetValueLength(buffer.Span);
                if (buffer.Length < jsonValueLength)
                {
                    throw new ArgumentException("buffer is shorter than the length prefix.");
                }

                buffer = buffer.Slice(0, jsonValueLength);

                this.jsonBinaryBuffer = new JsonBinaryMemoryReader(buffer);
                this.arrayAndObjectEndStack = new Stack<int>();
                this.jsonStringDictionary = jsonStringDictionary;
            }
            internal JsonBinaryReader(
                ReadOnlyMemory <byte> rootBuffer,
                int?indexToStartFrom = null,
                IReadOnlyJsonStringDictionary jsonStringDictionary = null)
            {
                if (rootBuffer.IsEmpty)
                {
                    throw new ArgumentException($"{nameof(rootBuffer)} must not be empty.");
                }

                this.rootBuffer = rootBuffer;

                ReadOnlyMemory <byte> readerBuffer = this.rootBuffer;

                if (indexToStartFrom.HasValue)
                {
                    readerBuffer = readerBuffer.Slice(start: indexToStartFrom.Value);
                }
                else
                {
                    // Skip the 0x80
                    readerBuffer = readerBuffer.Slice(start: 1);
                }

                // Only navigate the outer most json value and trim off trailing bytes
                int jsonValueLength = JsonBinaryEncoding.GetValueLength(readerBuffer.Span);

                if (readerBuffer.Length < jsonValueLength)
                {
                    throw new ArgumentException("buffer is shorter than the length prefix.");
                }

                readerBuffer = readerBuffer.Slice(0, jsonValueLength);

                // offset for the 0x80 binary type marker
                this.jsonBinaryBuffer       = new JsonBinaryMemoryReader(readerBuffer);
                this.arrayAndObjectEndStack = new Stack <int>();
                this.jsonStringDictionary   = jsonStringDictionary;
            }