public BlittableJsonReaderObject ReadObjectWithExternalProperties(DynamicJsonValue obj, string debugTag)
        {
            var state = new JsonParserState();

            using (var parser = new ObjectJsonParser(state, obj, this))
            {
                var writer = new BlittableJsonDocumentBuilder(this, BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, state);
                try
                {
                    writer.ReadObject();
                    if (writer.Read() == false)
                    {
                        throw new InvalidOperationException("Partial json content in object json parser shouldn't happen");
                    }
                    writer.FinalizeDocumentWithoutProperties(CachedProperties.Version);
                    _disposables.Add(writer);
                    return(writer.CreateReader(CachedProperties));
                }
                catch (Exception)
                {
                    writer.Dispose();
                    throw;
                }
            }
        }
        private unsafe bool AboutToReadPropertyNameInternal(ObjectJsonParser reader, JsonParserState state)
        {
            if (_state != State.None)
            {
                if (!AboutToReadWithStateUnlikely(reader, state))
                    return false;
            }

            _state = State.None;

            while (true)
            {
                if (reader.Read() == false)
                    return false;

                if (state.CurrentTokenType != JsonParserToken.String)
                    return true; // let the caller handle that

                if (_readingMetadataObject == false)
                {
                    if (state.StringSize == 9 && state.StringBuffer[0] == (byte)'@' && *(long*)(state.StringBuffer + 1) == 7022344802737087853)
                        _readingMetadataObject = true;

                    return true;
                }

                if (AboutToReadPropertyNameInMetadataUnlikely(reader, state, out bool aboutToReadPropertyName))
                    return aboutToReadPropertyName;
            }
        }
        private BlittableJsonReaderObject ReadObjectInternal(object builder, string documentId, BlittableJsonDocumentBuilder.UsageMode mode)
        {
            var state = new JsonParserState();

            using (var parser = new ObjectJsonParser(state, builder, this))
            {
                var writer = new BlittableJsonDocumentBuilder(this, mode, documentId, parser, state);
                try
                {
                    CachedProperties.NewDocument();
                    writer.ReadObject();
                    if (writer.Read() == false)
                    {
                        throw new InvalidOperationException("Partial content in object json parser shouldn't happen");
                    }
                    writer.FinalizeDocument();
                    _disposables.Add(writer);
                    return(writer.CreateReader());
                }
                catch (Exception)
                {
                    writer.Dispose();
                    throw;
                }
            }
        }
        public void Write(BlittableJsonTextWriter writer, DynamicJsonArray json)
        {
            var state = new JsonParserState();

            using (var parser = new ObjectJsonParser(state, json, this))
            {
                parser.Read();

                WriteArray(writer, state, parser);
            }
        }
        private void WriteInternal(BlittableJsonTextWriter writer, object json)
        {
            var state = new JsonParserState();

            using (var parser = new ObjectJsonParser(state, json, this))
            {
                parser.Read();

                WriteObject(writer, state, parser);
            }
        }
Пример #6
0
        public JsonOperationContext(int initialSize, int longLivedSize, SharedMultipleUseFlag lowMemoryFlag)
        {
            Debug.Assert(lowMemoryFlag != null);
            _disposeOnceRunner = new DisposeOnce <ExceptionRetry>(() =>
            {
#if MEM_GUARD_STACK
                ElectricFencedMemory.DecrementConext();
                ElectricFencedMemory.UnRegisterContextAllocation(this);
#endif

                Reset(true);

                _documentBuilder.Dispose();
                _arenaAllocator.Dispose();
                _arenaAllocatorForLongLivedValues?.Dispose();

                if (_managedBuffers != null)
                {
                    foreach (var managedPinnedBuffer in _managedBuffers)
                    {
                        managedPinnedBuffer.Dispose();
                    }

                    _managedBuffers = null;
                }

                if (_pinnedObjects != null)
                {
                    foreach (var pinnedObject in _pinnedObjects)
                    {
                        pinnedObject.Free();
                    }

                    _pinnedObjects = null;
                }
            });

            _initialSize    = initialSize;
            _longLivedSize  = longLivedSize;
            _arenaAllocator = new ArenaMemoryAllocator(lowMemoryFlag, initialSize);
            _arenaAllocatorForLongLivedValues = new ArenaMemoryAllocator(lowMemoryFlag, longLivedSize);
            CachedProperties  = new CachedProperties(this);
            _jsonParserState  = new JsonParserState();
            _objectJsonParser = new ObjectJsonParser(_jsonParserState, this);
            _documentBuilder  = new BlittableJsonDocumentBuilder(this, _jsonParserState, _objectJsonParser);
            LowMemoryFlag     = lowMemoryFlag;

#if MEM_GUARD_STACK
            ElectricFencedMemory.IncrementConext();
            ElectricFencedMemory.RegisterContextAllocation(this, Environment.StackTrace);
#endif
        }
Пример #7
0
        public JsonOperationContext(int initialSize, int longLivedSize)
        {
            _initialSize    = initialSize;
            _longLivedSize  = longLivedSize;
            _arenaAllocator = new ArenaMemoryAllocator(initialSize);
            _arenaAllocatorForLongLivedValues = new ArenaMemoryAllocator(longLivedSize);
            Encoding         = new UTF8Encoding();
            CachedProperties = new CachedProperties(this);

            _jsonParserState  = new JsonParserState();
            _objectJsonParser = new ObjectJsonParser(_jsonParserState, this);
            _documentBuilder  = new BlittableJsonDocumentBuilder(this, _jsonParserState, _objectJsonParser);
        }
Пример #8
0
        public JsonOperationContext(int initialSize, int longLivedSize, SharedMultipleUseFlag lowMemoryFlag)
        {
            Debug.Assert(lowMemoryFlag != null);

            _initialSize    = initialSize;
            _longLivedSize  = longLivedSize;
            _arenaAllocator = new ArenaMemoryAllocator(lowMemoryFlag, initialSize);
            _arenaAllocatorForLongLivedValues = new ArenaMemoryAllocator(lowMemoryFlag, longLivedSize);
            CachedProperties  = new CachedProperties(this);
            _jsonParserState  = new JsonParserState();
            _objectJsonParser = new ObjectJsonParser(_jsonParserState, this);
            _documentBuilder  = new BlittableJsonDocumentBuilder(this, _jsonParserState, _objectJsonParser);
            LowMemoryFlag     = lowMemoryFlag;

#if MEM_GUARD_STACK
            ElectricFencedMemory.IncrementConext();
            ElectricFencedMemory.RegisterContextAllocation(this, Environment.StackTrace);
#endif
        }
Пример #9
0
        private unsafe void WriteValue(BlittableJsonTextWriter writer, JsonParserState state, ObjectJsonParser parser)
        {
            switch (state.CurrentTokenType)
            {
            case JsonParserToken.Null:
                writer.WriteNull();
                break;

            case JsonParserToken.False:
                writer.WriteBool(false);
                break;

            case JsonParserToken.True:
                writer.WriteBool(true);
                break;

            case JsonParserToken.String:
                if (state.CompressedSize.HasValue)
                {
                    var lazyCompressedStringValue = new LazyCompressedStringValue(null, state.StringBuffer,
                                                                                  state.StringSize, state.CompressedSize.Value, this);
                    writer.WriteString(lazyCompressedStringValue);
                }
                else
                {
                    writer.WriteString(AllocateStringValue(null, state.StringBuffer, state.StringSize));
                }
                break;

            case JsonParserToken.Float:
                writer.WriteDouble(new LazyNumberValue(AllocateStringValue(null, state.StringBuffer, state.StringSize)));
                break;

            case JsonParserToken.Integer:
                writer.WriteInteger(state.Long);
                break;

            case JsonParserToken.StartObject:
                WriteObject(writer, state, parser);
                break;

            case JsonParserToken.StartArray:
                WriteArray(writer, state, parser);
                break;

            default:
                throw new ArgumentOutOfRangeException("Could not understand " + state.CurrentTokenType);
            }
        }
Пример #10
0
        public unsafe void WriteObject(BlittableJsonTextWriter writer, JsonParserState state, ObjectJsonParser parser)
        {
            if (state.CurrentTokenType != JsonParserToken.StartObject)
            {
                throw new InvalidOperationException("StartObject expected, but got " + state.CurrentTokenType);
            }

            writer.WriteStartObject();
            bool first = true;

            while (true)
            {
                if (parser.Read() == false)
                {
                    throw new InvalidOperationException("Object json parser can't return partial results");
                }
                if (state.CurrentTokenType == JsonParserToken.EndObject)
                {
                    break;
                }

                if (state.CurrentTokenType != JsonParserToken.String)
                {
                    throw new InvalidOperationException("Property expected, but got " + state.CurrentTokenType);
                }

                if (first == false)
                {
                    writer.WriteComma();
                }
                first = false;

                var lazyStringValue = AllocateStringValue(null, state.StringBuffer, state.StringSize);
                writer.WritePropertyName(lazyStringValue);

                if (parser.Read() == false)
                {
                    throw new InvalidOperationException("Object json parser can't return partial results");
                }

                WriteValue(writer, state, parser);
            }
            writer.WriteEndObject();
        }
Пример #11
0
        public void WriteArray(BlittableJsonTextWriter writer, JsonParserState state, ObjectJsonParser parser)
        {
            if (state.CurrentTokenType != JsonParserToken.StartArray)
            {
                throw new InvalidOperationException("StartArray expected, but got " + state.CurrentTokenType);
            }

            writer.WriteStartArray();
            bool first = true;

            while (true)
            {
                if (parser.Read() == false)
                {
                    throw new InvalidOperationException("Object json parser can't return partial results");
                }

                if (state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    break;
                }

                if (first == false)
                {
                    writer.WriteComma();
                }
                first = false;

                WriteValue(writer, state, parser);
            }
            writer.WriteEndArray();
        }