public static bool TryGetBufferedStringValue( Utf8Memory stringToken, IReadOnlyJsonStringDictionary jsonStringDictionary, out Utf8Memory value) { if (stringToken.IsEmpty) { value = default; return(false); } if (JsonBinaryEncoding.TryGetBufferedLengthPrefixedString( stringToken, out value)) { return(true); } if (JsonBinaryEncoding.TryGetEncodedStringValue( stringToken.Span, jsonStringDictionary, out UtfAllString encodedStringValue)) { value = encodedStringValue.Utf8EscapedString; return(true); } value = default; return(false); }
/// <summary> /// Gets the string value from the binary reader. /// </summary> /// <param name="stringToken">The buffer that has the string.</param> /// <param name="jsonStringDictionary">The JSON string dictionary.</param> /// <returns>A string value from the binary reader.</returns> public static string GetStringValue( Utf8Memory stringToken, IReadOnlyJsonStringDictionary jsonStringDictionary) { if (stringToken.IsEmpty) { throw new JsonInvalidTokenException(); } if (JsonBinaryEncoding.TryGetBufferedLengthPrefixedString( stringToken, out Utf8Memory lengthPrefixedString)) { return(lengthPrefixedString.ToString()); } if (JsonBinaryEncoding.TryGetEncodedStringValue( stringToken.Span, jsonStringDictionary, out UtfAllString encodedStringValue)) { return(encodedStringValue.Utf16String); } throw new JsonInvalidTokenException(); }
/// <inheritdoc /> public override bool TryGetObjectProperty(IJsonNavigatorNode node, string propertyName, out ObjectProperty objectProperty) { if (node is LazyNode lazyNode) { node = lazyNode.Value; } if (!(node is ObjectNode objectNode)) { throw new ArgumentException($"{node} was not of type: {nameof(ObjectNode)}."); } Utf8Memory propertyNameAsUtf8 = Utf8Memory.Create(propertyName); foreach (ObjectProperty property in objectNode.Properties) { if (!this.TryGetBufferedStringValue(property.NameNode, out Utf8Memory candidate)) { throw new InvalidOperationException("Failed to get property name buffered value."); } if (propertyNameAsUtf8.Span == candidate.Span) { objectProperty = property; return(true); } } objectProperty = default; return(false); }
Utf8Memory IJsonTextReaderPrivateImplementation.GetBufferedJsonToken() { ReadOnlyMemory <byte> bufferedRawJson = this.jsonTextBuffer.GetBufferedRawJsonToken( this.token.Start, this.token.End); return(Utf8Memory.UnsafeCreateNoValidation(bufferedRawJson)); }
public static Utf8Memory Create(ReadOnlyMemory <byte> utf8Bytes) { if (!Utf8Memory.TryCreate(utf8Bytes, out Utf8Memory utf8Memory)) { throw new ArgumentException($"{nameof(utf8Bytes)} did not contain a valid UTF-8 byte sequence."); } return(utf8Memory); }
/// <inheritdoc /> public override string GetStringValue(IJsonNavigatorNode stringNode) { ReadOnlyMemory <byte> buffer = JsonBinaryNavigator.GetNodeOfType( JsonNodeType.String, stringNode); return(JsonBinaryEncoding.GetStringValue( Utf8Memory.UnsafeCreateNoValidation(buffer), this.jsonStringDictionary)); }
/// <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(IJsonTextReaderPrivateImplementation jsonTextReader) { Utf8Memory bufferedStringValue = jsonTextReader.GetBufferedJsonToken(); StringNode stringNode = StringNode.Create(bufferedStringValue); // consume the string from the reader jsonTextReader.Read(); return(stringNode); }
public static StringNode Create(Utf8Memory bufferedToken) { if (bufferedToken.Length == 0) { return(StringNode.Empty); } // In the future we can have a flyweight dictionary for system strings. return(new StringNode(bufferedToken)); }
public static bool TryCreate(ReadOnlyMemory <byte> utf8Bytes, out Utf8Memory utf8Memory) { if (!Utf8Span.TryParseUtf8Bytes(utf8Bytes.Span, out _)) { utf8Memory = default; return(false); } utf8Memory = new Utf8Memory(utf8Bytes); return(true); }
private UtfAllString( Utf8Memory utf8String, string utf16String, Utf8Memory utf8EscapedString, string utf16EscapedString) { this.Utf8String = utf8String; this.Utf16String = utf16String; this.Utf8EscapedString = utf8EscapedString; this.Utf16EscapedString = utf16EscapedString; }
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 bool TryGetBufferedStringValue( IJsonNavigatorNode stringNode, out Utf8Memory value) { ReadOnlyMemory <byte> buffer = JsonBinaryNavigator.GetNodeOfType( JsonNodeType.String, stringNode); return(JsonBinaryEncoding.TryGetBufferedStringValue( this.rootBuffer, buffer, out value)); }
/// <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 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 bool TryGetBufferedStringValue(out Utf8Memory bufferedUtf8StringValue) { if (!( (this.JsonObjectState.CurrentTokenType == JsonTokenType.String) || (this.JsonObjectState.CurrentTokenType == JsonTokenType.FieldName))) { throw new JsonInvalidTokenException(); } return(JsonBinaryEncoding.TryGetBufferedStringValue( this.rootBuffer, this.jsonBinaryBuffer.GetBufferedRawJsonToken(this.currentTokenPosition), out bufferedUtf8StringValue)); }
/// <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); }
/// <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); }
/// <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)); }
/// <summary> /// Gets the string value from the binary reader. /// </summary> /// <param name="stringToken">The buffer that has the string.</param> /// <param name="jsonStringDictionary">The JSON string dictionary.</param> /// <returns>A string value from the binary reader.</returns> public static string GetStringValue( Utf8Memory stringToken, IReadOnlyJsonStringDictionary jsonStringDictionary) { if (stringToken.IsEmpty) { throw new JsonInvalidTokenException(); } if (!JsonBinaryEncoding.TryGetBufferedStringValue(stringToken, jsonStringDictionary, out Utf8Memory bufferedUtf8StringValue)) { throw new JsonInvalidTokenException(); } return(bufferedUtf8StringValue.ToString()); }
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)); }
/// <inheritdoc /> public override bool TryGetBufferedStringValue( IJsonNavigatorNode navigatorNode, out Utf8Memory value) { if (navigatorNode == null) { throw new ArgumentNullException(nameof(navigatorNode)); } if (!(navigatorNode is StringNodeBase stringNode)) { throw new ArgumentException($"{nameof(navigatorNode)} must actually be a number node."); } // For text we materialize the strings into UTF-16, so we can't get the buffered UTF-8 string. value = default; return(false); }
/// <inheritdoc /> public override bool TryGetBufferedStringValue( IJsonNavigatorNode node, out Utf8Memory value) { if (!(node is StringNodeBase stringNodeBase)) { throw new ArgumentException($"{node} was not of type: {nameof(StringNodeBase)}."); } // TODO: consider cacheing whether or not the string is escaped in the node itself. if (stringNodeBase.BufferedValue.Span.Contains(ReverseSoldius.Span)) { // encountered escaped string that can't be returned value = default; return(false); } // Just trim off the quotes value = stringNodeBase.BufferedValue.Slice(start: 1, length: stringNodeBase.BufferedValue.Length - 2); return(true); }
/// <inheritdoc /> public abstract bool TryGetBufferedStringValue(out Utf8Memory value);
protected StringNodeBase( Utf8Memory bufferedValue) { this.BufferedValue = bufferedValue; }
public static Utf8Memory Create(string value) { return(Utf8Memory.UnsafeCreateNoValidation(Encoding.UTF8.GetBytes(value))); }
private StringNode(Utf8Memory bufferedValue) : base(bufferedValue) { }
private FieldNameNode(Utf8Memory bufferedValue) : base(bufferedValue) { }
/// <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)); }
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}."); } }