Utf8Memory IJsonTextReaderPrivateImplementation.GetBufferedJsonToken()
            {
                ReadOnlyMemory <byte> bufferedRawJson = this.jsonTextBuffer.GetBufferedRawJsonToken(
                    this.token.Start,
                    this.token.End);

                return(Utf8Memory.UnsafeCreateNoValidation(bufferedRawJson));
            }
Пример #2
0
            /// <inheritdoc />
            public override string GetStringValue(IJsonNavigatorNode stringNode)
            {
                ReadOnlyMemory <byte> buffer = JsonBinaryNavigator.GetNodeOfType(
                    JsonNodeType.String,
                    stringNode);

                return(JsonBinaryEncoding.GetStringValue(
                           Utf8Memory.UnsafeCreateNoValidation(buffer),
                           this.jsonStringDictionary));
            }
Пример #3
0
        public static UtfAllString Create(string utf16String)
        {
            if (utf16String == null)
            {
                throw new ArgumentNullException(nameof(utf16String));
            }

            Utf8Memory utf8String = Utf8Memory.UnsafeCreateNoValidation(Encoding.UTF8.GetBytes(utf16String));

            return(new UtfAllString(utf8String, utf16String));
        }
        public static UtfAllString Create(Utf8Memory utf8String)
        {
            string utf16String = utf8String.ToString();

            string utf16EscapedString = JsonConvert.ToString(utf16String);

            utf16EscapedString = utf16EscapedString.Substring(1, utf16EscapedString.Length - 2);

            Utf8Memory utf8EscapedString = Utf8Memory.UnsafeCreateNoValidation(Encoding.UTF8.GetBytes(utf16EscapedString));

            return(new UtfAllString(utf8String, utf16String, utf8EscapedString, utf16EscapedString));
        }
            /// <inheritdoc />
            public override string GetStringValue()
            {
                if (!(
                    (this.JsonObjectState.CurrentTokenType == JsonTokenType.String) ||
                    (this.JsonObjectState.CurrentTokenType == JsonTokenType.FieldName)))
                {
                    throw new JsonInvalidTokenException();
                }

                return JsonBinaryEncoding.GetStringValue(
                    Utf8Memory.UnsafeCreateNoValidation(this.jsonBinaryBuffer.GetBufferedRawJsonToken(this.currentTokenPosition)),
                    this.jsonStringDictionary);
            }
            /// <inheritdoc />
            public override UtfAnyString GetStringValue()
            {
                if (this.TryGetBufferedStringValue(out Utf8Memory memory))
                {
                    return(Utf8String.UnsafeFromUtf8BytesNoValidation(memory.Memory));
                }

                ReadOnlyMemory <byte> stringToken = this.jsonTextBuffer.GetBufferedRawJsonToken(
                    this.token.Start,
                    this.token.End);

                return(JsonTextParser.GetStringValue(Utf8Memory.UnsafeCreateNoValidation(stringToken)));
            }
            /// <inheritdoc />
            public override bool TryGetBufferedStringValue(out Utf8Memory value)
            {
                if (this.token.JsonTextTokenType.HasFlag(JsonTextTokenType.EscapedFlag))
                {
                    value = default;
                    return(false);
                }

                value = Utf8Memory.UnsafeCreateNoValidation(
                    this.jsonTextBuffer.GetBufferedRawJsonToken(
                        this.token.Start,
                        this.token.End));
                return(true);
            }
Пример #8
0
                /// <summary>
                /// Parses out a JSON string AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON string AST node</returns>
                private static StringNode ParseStringNode(IJsonReader jsonTextReader)
                {
                    if (!jsonTextReader.TryGetBufferedRawJsonToken(out ReadOnlyMemory <byte> bufferedRawJsonToken))
                    {
                        throw new InvalidOperationException("Failed to get the buffered raw json token.");
                    }

                    StringNode stringNode = StringNode.Create(Utf8Memory.UnsafeCreateNoValidation(bufferedRawJsonToken));

                    // consume the string from the reader
                    jsonTextReader.Read();

                    return(stringNode);
                }
Пример #9
0
                /// <summary>
                /// Parses out a JSON property AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON property AST node</returns>
                private static ObjectProperty ParsePropertyNode(IJsonReader jsonTextReader)
                {
                    if (!jsonTextReader.TryGetBufferedRawJsonToken(out ReadOnlyMemory <byte> bufferedRawJsonToken))
                    {
                        throw new InvalidOperationException("Failed to get the buffered raw json token.");
                    }

                    FieldNameNode fieldName = FieldNameNode.Create(Utf8Memory.UnsafeCreateNoValidation(bufferedRawJsonToken));

                    // Consume the fieldname from the jsonreader
                    jsonTextReader.Read();

                    JsonTextNavigatorNode value = Parser.ParseNode(jsonTextReader);

                    return(new ObjectProperty(fieldName, value));
                }
Пример #10
0
        public static UtfAllString Create(string utf16String)
        {
            if (utf16String == null)
            {
                throw new ArgumentNullException(nameof(utf16String));
            }

            Utf8Memory utf8String = Utf8Memory.UnsafeCreateNoValidation(Encoding.UTF8.GetBytes(utf16String));

            string utf16EscapedString = JsonConvert.ToString(utf16String);

            utf16EscapedString = utf16EscapedString.Substring(1, utf16EscapedString.Length - 2);

            Utf8Memory utf8EscapedString = Utf8Memory.UnsafeCreateNoValidation(Encoding.UTF8.GetBytes(utf16EscapedString));

            return(new UtfAllString(utf8String, utf16String, utf8EscapedString, utf16EscapedString));
        }
Пример #11
0
                /// <summary>
                /// Parses out a JSON property AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON property AST node</returns>
                private static ObjectProperty ParsePropertyNode(IJsonTextReaderPrivateImplementation jsonTextReader)
                {
                    ReadOnlyMemory <byte> bufferedRawJsonToken = jsonTextReader.GetBufferedJsonToken().Memory;
                    FieldNameNode         fieldName            = FieldNameNode.Create(Utf8Memory.UnsafeCreateNoValidation(bufferedRawJsonToken));

                    // Consume the fieldname from the jsonreader
                    jsonTextReader.Read();

                    JsonTextNavigatorNode value = Parser.ParseNode(jsonTextReader);

                    return(new ObjectProperty(fieldName, value));
                }
Пример #12
0
            private void WriteToInternal(BinaryNavigatorNode binaryNavigatorNode, IJsonWriter jsonWriter)
            {
                ReadOnlyMemory <byte> buffer   = binaryNavigatorNode.Buffer;
                JsonNodeType          nodeType = binaryNavigatorNode.JsonNodeType;

                switch (nodeType)
                {
                case JsonNodeType.Null:
                    jsonWriter.WriteNullValue();
                    break;

                case JsonNodeType.False:
                    jsonWriter.WriteBoolValue(false);
                    break;

                case JsonNodeType.True:
                    jsonWriter.WriteBoolValue(true);
                    break;

                case JsonNodeType.Number64:
                {
                    Number64 value = JsonBinaryEncoding.GetNumberValue(buffer.Span);
                    jsonWriter.WriteNumber64Value(value);
                }
                break;

                case JsonNodeType.String:
                case JsonNodeType.FieldName:
                    bool fieldName = binaryNavigatorNode.JsonNodeType == JsonNodeType.FieldName;

                    Utf8Memory utf8Buffer = Utf8Memory.UnsafeCreateNoValidation(buffer);
                    if (JsonBinaryEncoding.TryGetBufferedStringValue(
                            utf8Buffer,
                            this.jsonStringDictionary,
                            out Utf8Memory bufferedStringValue))
                    {
                        if (fieldName)
                        {
                            jsonWriter.WriteFieldName(bufferedStringValue.Span);
                        }
                        else
                        {
                            jsonWriter.WriteStringValue(bufferedStringValue.Span);
                        }
                    }
                    else
                    {
                        string value = JsonBinaryEncoding.GetStringValue(
                            utf8Buffer,
                            this.jsonStringDictionary);
                        if (fieldName)
                        {
                            jsonWriter.WriteFieldName(value);
                        }
                        else
                        {
                            jsonWriter.WriteStringValue(value);
                        }
                    }
                    break;

                case JsonNodeType.Array:
                {
                    jsonWriter.WriteArrayStart();

                    foreach (BinaryNavigatorNode arrayItem in this.GetArrayItemsInternal(buffer))
                    {
                        this.WriteToInternal(arrayItem, jsonWriter);
                    }

                    jsonWriter.WriteArrayEnd();
                }
                break;

                case JsonNodeType.Object:
                {
                    jsonWriter.WriteObjectStart();

                    foreach (ObjectPropertyInternal objectProperty in this.GetObjectPropertiesInternal(buffer))
                    {
                        this.WriteToInternal(objectProperty.NameNode, jsonWriter);
                        this.WriteToInternal(objectProperty.ValueNode, jsonWriter);
                    }

                    jsonWriter.WriteObjectEnd();
                }
                break;

                case JsonNodeType.Int8:
                {
                    sbyte value = JsonBinaryEncoding.GetInt8Value(buffer.Span);
                    jsonWriter.WriteInt8Value(value);
                }
                break;

                case JsonNodeType.Int16:
                {
                    short value = JsonBinaryEncoding.GetInt16Value(buffer.Span);
                    jsonWriter.WriteInt16Value(value);
                }
                break;

                case JsonNodeType.Int32:
                {
                    int value = JsonBinaryEncoding.GetInt32Value(buffer.Span);
                    jsonWriter.WriteInt32Value(value);
                }
                break;

                case JsonNodeType.Int64:
                {
                    long value = JsonBinaryEncoding.GetInt64Value(buffer.Span);
                    jsonWriter.WriteInt64Value(value);
                }
                break;

                case JsonNodeType.UInt32:
                {
                    uint value = JsonBinaryEncoding.GetUInt32Value(buffer.Span);
                    jsonWriter.WriteUInt32Value(value);
                }
                break;

                case JsonNodeType.Float32:
                {
                    float value = JsonBinaryEncoding.GetFloat32Value(buffer.Span);
                    jsonWriter.WriteFloat32Value(value);
                }
                break;

                case JsonNodeType.Float64:
                {
                    double value = JsonBinaryEncoding.GetFloat64Value(buffer.Span);
                    jsonWriter.WriteFloat64Value(value);
                }
                break;

                case JsonNodeType.Binary:
                {
                    ReadOnlyMemory <byte> value = JsonBinaryEncoding.GetBinaryValue(buffer);
                    jsonWriter.WriteBinaryValue(value.Span);
                }
                break;

                case JsonNodeType.Guid:
                {
                    Guid value = JsonBinaryEncoding.GetGuidValue(buffer.Span);
                    jsonWriter.WriteGuidValue(value);
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException($"Unknown {nameof(JsonNodeType)}: {nodeType}.");
                }
            }
Пример #13
0
 public static Utf8Memory Create(string value)
 {
     return(Utf8Memory.UnsafeCreateNoValidation(Encoding.UTF8.GetBytes(value)));
 }
Пример #14
0
            private void ForceRewriteRawJsonValue(
                ReadOnlyMemory <byte> rawJsonValue,
                bool isFieldName,
                IReadOnlyJsonStringDictionary jsonStringDictionary)
            {
                byte typeMarker = rawJsonValue.Span[0];

                if (!JsonBinaryEncoding.TypeMarker.IsSystemString(typeMarker) &&
                    !JsonBinaryEncoding.TypeMarker.IsEmptyArray(typeMarker) &&
                    !JsonBinaryEncoding.TypeMarker.IsEmptyObject(typeMarker))
                {
                    JsonNodeType nodeType = JsonBinaryEncoding.NodeTypes.GetNodeType(typeMarker);
                    switch (nodeType)
                    {
                    case JsonNodeType.String:
                    {
                        if (!JsonBinaryEncoding.TryGetBufferedStringValue(
                                Utf8Memory.UnsafeCreateNoValidation(rawJsonValue),
                                jsonStringDictionary,
                                out Utf8Memory bufferedStringValue))
                        {
                            throw new InvalidOperationException("Excepted to get the buffered string value.");
                        }

                        if (isFieldName)
                        {
                            this.WriteFieldName(bufferedStringValue.Span);
                        }
                        else
                        {
                            this.WriteStringValue(bufferedStringValue.Span);
                        }

                        return;
                    }

                    case JsonNodeType.Array:
                    {
                        this.WriteArrayStart();

                        foreach (ReadOnlyMemory <byte> arrayItem in JsonBinaryEncoding.Enumerator.GetArrayItems(rawJsonValue))
                        {
                            this.ForceRewriteRawJsonValue(arrayItem, isFieldName, jsonStringDictionary);
                        }

                        this.WriteArrayEnd();

                        return;
                    }

                    case JsonNodeType.Object:
                    {
                        this.WriteObjectStart();

                        foreach (JsonBinaryEncoding.Enumerator.ObjectProperty property in JsonBinaryEncoding.Enumerator.GetObjectProperties(rawJsonValue))
                        {
                            this.ForceRewriteRawJsonValue(property.Name, isFieldName: true, jsonStringDictionary);
                            this.ForceRewriteRawJsonValue(property.Value, isFieldName: false, jsonStringDictionary);
                        }

                        this.WriteObjectEnd();

                        return;
                    }
                    }
                }

                // Other that whether or not this is a field name, the type of the value does not matter here
                this.JsonObjectState.RegisterToken(isFieldName ? JsonTokenType.FieldName : JsonTokenType.String);
                this.binaryWriter.Write(rawJsonValue.Span);
                if (!isFieldName)
                {
                    this.bufferedContexts.Peek().Count++;
                }
            }