public static void EnsureObjectStart(ref Utf8Json.Reader reader) { if (reader.TokenType != JsonTokenType.StartObject) { throw new InvalidDataException($"Unexpected JSON Token Type '{GetTokenString(reader.TokenType)}'. Expected a JSON Object."); } }
public static byte[] JsonLabReaderLoop(int inpuDataLength, out int length, ref Utf8Json.Reader json) { byte[] outputArray = new byte[inpuDataLength]; Span <byte> destination = outputArray; while (json.Read()) { JsonTokenType tokenType = json.TokenType; ReadOnlySpan <byte> valueSpan = json.Value; switch (tokenType) { case JsonTokenType.PropertyName: valueSpan.CopyTo(destination); destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.Number: case JsonTokenType.String: case JsonTokenType.Comment: valueSpan.CopyTo(destination); destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.True: // Special casing True/False so that the casing matches with Json.NET destination[0] = (byte)'T'; destination[1] = (byte)'r'; destination[2] = (byte)'u'; destination[3] = (byte)'e'; destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.False: destination[0] = (byte)'F'; destination[1] = (byte)'a'; destination[2] = (byte)'l'; destination[3] = (byte)'s'; destination[4] = (byte)'e'; destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.Null: // Special casing Null so that it matches what JSON.NET does break; default: break; } } length = outputArray.Length - destination.Length; return(outputArray); }
public static bool TryParseRequestMessageHeapable(ref ReadOnlySequence <byte> buffer) { if (!TryParseMessage(ref buffer, out var payload)) { return(false); } var reader = new Utf8Json(); Utf8Json.Reader json = reader.GetReader(payload); CheckRead(ref json); EnsureObjectStart(ref json); string protocol = null; int? protocolVersion = null; var completed = false; while (!completed && CheckRead(ref json)) { switch (json.TokenType) { case JsonTokenType.PropertyName: ReadOnlySpan <byte> memberName = json.Value; if (memberName.SequenceEqual(ProtocolPropertyNameUtf8)) { protocol = ReadAsString(ref json, ProtocolPropertyName); } else if (memberName.SequenceEqual(ProtocolVersionPropertyNameUtf8)) { protocolVersion = ReadAsInt32(ref json, ProtocolVersionPropertyName); } else { json.Skip(); } break; case JsonTokenType.EndObject: completed = true; break; default: throw new InvalidDataException($"Unexpected token '{json.TokenType}' when reading handshake request JSON."); } } if (protocol == null) { throw new InvalidDataException($"Missing required property '{ProtocolPropertyName}'."); } if (protocolVersion == null) { throw new InvalidDataException($"Missing required property '{ProtocolVersionPropertyName}'."); } json.Dispose(); return(true); }
public static bool CheckRead(ref Utf8Json.Reader reader) { if (!reader.Read()) { throw new InvalidDataException("Unexpected end when reading JSON."); } return(true); }
public void HeapableReaderSystemTextJsonLabSpanEmptyLoop() { var reader = new Utf8Json(); Utf8Json.Reader json = reader.GetReader(_dataUtf8); while (json.Read()) { ; } }
public static object JsonLabReturnObjectHelperHeapable(byte[] data, JsonReaderOptions options = JsonReaderOptions.Default) { var reader = new Utf8Json() { Options = options }; Utf8Json.Reader json = reader.GetReader(data); return(JsonLabReaderLoop(ref json)); }
public static byte[] HeapableJsonLabReturnBytesHelper(byte[] data, out int length, JsonReaderOptions options = JsonReaderOptions.Default) { var json = new Utf8Json() { Options = options }; Utf8Json.Reader reader = json.GetReader(data); return(JsonLabReaderLoop(data.Length, out length, ref reader)); }
public static List <object> JsonLabReaderListLoop(ref Utf8Json.Reader json) { List <object> arrayList = new List <object>(); object value = null; while (json.Read()) { JsonTokenType tokenType = json.TokenType; ReadOnlySpan <byte> valueSpan = json.Value; switch (tokenType) { case JsonTokenType.True: case JsonTokenType.False: value = valueSpan[0] == 't'; arrayList.Add(value); break; case JsonTokenType.Number: value = json.GetValueAsNumber(); arrayList.Add(value); break; case JsonTokenType.String: value = json.GetValueAsString(); arrayList.Add(value); break; case JsonTokenType.Null: value = null; arrayList.Add(value); break; case JsonTokenType.StartObject: value = JsonLabReaderDictionaryLoop(ref json); arrayList.Add(value); break; case JsonTokenType.StartArray: value = JsonLabReaderListLoop(ref json); arrayList.Add(value); break; case JsonTokenType.EndArray: return(arrayList); case JsonTokenType.None: case JsonTokenType.Comment: default: break; } } return(arrayList); }
public static byte[] HeapableJsonLabSequenceReturnBytesHelper(byte[] data, out int length, JsonReaderOptions options = JsonReaderOptions.Default) { ReadOnlySequence <byte> sequence = CreateSegments(data); var json = new Utf8Json() { Options = options }; Utf8Json.Reader reader = json.GetReader(sequence); byte[] result = JsonLabReaderLoop(data.Length, out length, ref reader); reader.Dispose(); // TODO: Should we reset the value and valuetype once we are done? //Assert.True(reader.Value.IsEmpty); //Assert.Equal(JsonValueType.Unknown, reader.ValueType); return(result); }
public static object JsonLabReaderLoop(ref Utf8Json.Reader json) { object root = null; while (json.Read()) { JsonTokenType tokenType = json.TokenType; ReadOnlySpan <byte> valueSpan = json.Value; switch (tokenType) { case JsonTokenType.True: case JsonTokenType.False: root = valueSpan[0] == 't'; break; case JsonTokenType.Number: root = json.GetValueAsNumber(); break; case JsonTokenType.String: root = json.GetValueAsString(); break; case JsonTokenType.Null: break; case JsonTokenType.StartObject: root = JsonLabReaderDictionaryLoop(ref json); break; case JsonTokenType.StartArray: root = JsonLabReaderListLoop(ref json); break; case JsonTokenType.EndObject: case JsonTokenType.EndArray: break; case JsonTokenType.None: case JsonTokenType.Comment: default: break; } } return(root); }
public static int?ReadAsInt32(ref Utf8Json.Reader reader, string propertyName) { reader.Read(); if (reader.TokenType != JsonTokenType.Number) { throw new InvalidDataException($"Expected '{propertyName}' to be of type Integer."); } if (reader.Value.IsEmpty) { return(null); } if (!Utf8Parser.TryParse(reader.Value, out int value, out _)) { throw new InvalidDataException($"Expected '{propertyName}' to be of type Integer."); } return(value); }
public static void HeapableJsonLabEmptyLoopHelper(byte[] data) { var reader = new Utf8Json(); Utf8Json.Reader json = reader.GetReader(data); while (json.Read()) { JsonTokenType tokenType = json.TokenType; switch (tokenType) { case JsonTokenType.StartObject: case JsonTokenType.EndObject: break; case JsonTokenType.StartArray: case JsonTokenType.EndArray: break; case JsonTokenType.PropertyName: break; case JsonTokenType.String: break; case JsonTokenType.Number: break; case JsonTokenType.True: break; case JsonTokenType.False: break; case JsonTokenType.Null: break; default: throw new ArgumentException(); } } }
public static void SetKeyValues(ref Utf8Json.Reader json, Dictionary <string, object> dictionary, ref string key, ref object value) { while (json.Read()) { switch (json.TokenType) { case JsonTokenType.PropertyName: key = Encoding.UTF8.GetString(json.Value); dictionary.Add(key, null); break; case JsonTokenType.String: value = Encoding.UTF8.GetString(json.Value); if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.True: case JsonTokenType.False: value = json.Value[0] == (byte)'t'; if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; default: break; } } }
public static unsafe string ReadAsString(ref Utf8Json.Reader reader, string propertyName) { reader.Read(); if (reader.TokenType != JsonTokenType.String) { throw new InvalidDataException($"Expected '{propertyName}' to be of type String."); } if (reader.Value.IsEmpty) { return(""); } #if NETCOREAPP2_2 return(Encoding.UTF8.GetString(reader.Value)); #else fixed(byte *bytes = &MemoryMarshal.GetReference(reader.Value)) { return(Encoding.UTF8.GetString(bytes, reader.Value.Length)); } #endif }
private static void GetJsonReaderException(ref Utf8Json.Reader json, ExceptionResource resource, byte nextByte, ReadOnlySpan <byte> bytes) { string message = GetResourceString(ref json, resource, (char)nextByte, Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Length)); throw new JsonReaderException(message, json._utf8Json._state._lineNumber, json._utf8Json._state._position); }
private static string GetResourceString(ref Utf8Json.Reader json, ExceptionResource resource, char character, string characters) { Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource), "The enum value is not defined, please check the ExceptionResource Enum."); string formatString = ExceptionStrings.ResourceManager.GetString(resource.ToString()); string message = formatString; switch (resource) { case ExceptionResource.ArrayDepthTooLarge: message = string.Format(formatString, json.CurrentDepth, json._utf8Json.MaxDepth); break; case ExceptionResource.ArrayEndWithinObject: if (json.CurrentDepth <= 0) { formatString = ExceptionStrings.ResourceManager.GetString(ExceptionResource.DepthMustBePositive.ToString()); message = string.Format(formatString, json.CurrentDepth); } else { message = string.Format(formatString); } break; case ExceptionResource.EndOfStringNotFound: break; case ExceptionResource.ExpectedDigitNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedDigitNotFoundEndOfData: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedEndAfterSingleJson: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedEndOfDigitNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedNextDigitComponentNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedNextDigitEValueNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedSeparaterAfterPropertyNameNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedStartOfPropertyNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedStartOfPropertyOrValueNotFound: break; case ExceptionResource.ExpectedStartOfValueNotFound: message = string.Format(formatString, character); break; case ExceptionResource.ExpectedValueAfterPropertyNameNotFound: break; case ExceptionResource.FoundInvalidCharacter: message = string.Format(formatString, character); break; case ExceptionResource.InvalidEndOfJson: message = string.Format(formatString, json.TokenType); break; case ExceptionResource.ObjectDepthTooLarge: message = string.Format(formatString, json.CurrentDepth, json._utf8Json.MaxDepth); break; case ExceptionResource.ObjectEndWithinArray: if (json.CurrentDepth <= 0) { formatString = ExceptionStrings.ResourceManager.GetString(ExceptionResource.DepthMustBePositive.ToString()); message = string.Format(formatString, json.CurrentDepth); } else { message = string.Format(formatString); } break; case ExceptionResource.Default: break; case ExceptionResource.ExpectedFalse: message = string.Format(formatString, characters); break; case ExceptionResource.ExpectedNull: message = string.Format(formatString, characters); break; case ExceptionResource.ExpectedTrue: message = string.Format(formatString, characters); break; // This case is covered between ArrayEndWithinObject and ObjectEndWithinArray /*case ExceptionResource.DepthMustBePositive: * break;*/ case ExceptionResource.InvalidCharacterWithinString: message = string.Format(formatString, character); break; case ExceptionResource.EndOfCommentNotFound: break; } return(message); }
public static void ThrowJsonReaderException(ref Utf8Json.Reader json, ExceptionResource resource = ExceptionResource.Default, byte nextByte = default, ReadOnlySpan <byte> bytes = default) { GetJsonReaderException(ref json, resource, nextByte, bytes); }
public static Dictionary <string, object> JsonLabReaderDictionaryLoop(ref Utf8Json.Reader json) { Dictionary <string, object> dictionary = new Dictionary <string, object>(); string key = ""; object value = null; while (json.Read()) { JsonTokenType tokenType = json.TokenType; ReadOnlySpan <byte> valueSpan = json.Value; switch (tokenType) { case JsonTokenType.PropertyName: key = json.GetValueAsString(); dictionary.Add(key, null); break; case JsonTokenType.True: case JsonTokenType.False: value = valueSpan[0] == 't'; if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.Number: value = json.GetValueAsNumber(); if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.String: value = json.GetValueAsString(); if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.Null: value = null; if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.StartObject: value = JsonLabReaderDictionaryLoop(ref json); if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.StartArray: value = JsonLabReaderListLoop(ref json); if (dictionary.TryGetValue(key, out _)) { dictionary[key] = value; } else { dictionary.Add(key, value); } break; case JsonTokenType.EndObject: return(dictionary); case JsonTokenType.None: case JsonTokenType.Comment: default: break; } } return(dictionary); }