public void ReaderCorefxlab() { var json = new JsonLab.Utf8JsonReader(_data); while (json.Read()) { ; } }
private static void GetJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan <byte> bytes) { string message = GetResourceString(ref json, resource, (char)nextByte, Encoding.UTF8.GetString(bytes.ToArray())); throw new JsonReaderException(message, json._lineNumber, json._position); }
private static string GetResourceString(ref Utf8JsonReader 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.Depth, json.MaxDepth); break; case ExceptionResource.ArrayEndWithinObject: if (json.Depth <= 0) { formatString = ExceptionStrings.ResourceManager.GetString(ExceptionResource.DepthMustBePositive.ToString()); message = string.Format(formatString, json.Depth); } else { message = ExceptionStrings.ResourceManager.GetString(ExceptionResource.ArrayEndWithinObject.ToString()); } 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.Depth, json.MaxDepth); break; case ExceptionResource.ObjectEndWithinArray: if (json.Depth <= 0) { formatString = ExceptionStrings.ResourceManager.GetString(ExceptionResource.DepthMustBePositive.ToString()); message = string.Format(formatString, json.Depth); } else { message = ExceptionStrings.ResourceManager.GetString(ExceptionResource.ObjectEndWithinArray.ToString()); } 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 Utf8JsonReader json, ExceptionResource resource = ExceptionResource.Default, byte nextByte = default, ReadOnlySpan <byte> bytes = default) { GetJsonReaderException(ref json, resource, nextByte, bytes); }
public static JsonDynamicObject Parse(ReadOnlySpan <byte> utf8, int expectedNumberOfProperties = -1) { Stack <JsonDynamicObject> stack = new Stack <JsonDynamicObject>(); if (expectedNumberOfProperties == -1) { expectedNumberOfProperties = utf8.Length >> 3; } var properties = new Dictionary <JsonProperty, JsonValue>(expectedNumberOfProperties); stack.Push(new JsonDynamicObject(properties)); var reader = new Utf8JsonReader(utf8); while (reader.Read()) { switch (reader.TokenType) { case JsonTokenType.PropertyName: var name = new Utf8String(reader.Value); reader.Read(); // Move to the value token var type = reader.TokenType; var current = stack.Peek(); var property = new JsonProperty(current, name); switch (type) { case JsonTokenType.String: current._properties[property] = new JsonValue(new Utf8String(reader.Value)); break; case JsonTokenType.StartObject: // TODO: could this be lazy? Could this reuse the root JsonObject (which would store non-allocating JsonDom)? var newObj = new JsonDynamicObject(properties); current._properties[property] = new JsonValue(newObj); stack.Push(newObj); break; case JsonTokenType.True: current._properties[property] = new JsonValue(type); break; case JsonTokenType.False: current._properties[property] = new JsonValue(type); break; case JsonTokenType.Null: current._properties[property] = new JsonValue(type); break; case JsonTokenType.Number: current._properties[property] = new JsonValue(new Utf8String(reader.Value), type); break; case JsonTokenType.StartArray: throw new NotImplementedException("array support not implemented yet."); default: throw new NotSupportedException(); } break; case JsonTokenType.StartObject: break; case JsonTokenType.EndObject: if (stack.Count != 1) { stack.Pop(); } break; case JsonTokenType.StartArray: throw new NotImplementedException("array support not implemented yet."); case JsonTokenType.EndArray: case JsonTokenType.String: case JsonTokenType.True: case JsonTokenType.False: case JsonTokenType.Null: case JsonTokenType.Number: break; default: throw new NotSupportedException(); } } return(stack.Peek()); }