private static unsafe void ParseJson(ReadableBuffer buffer) { Utf8String json; if (buffer.IsSingleSpan) { json = new Utf8String(buffer.First.Span); } else { var length = buffer.Length; byte *b = stackalloc byte[length]; buffer.CopyTo(new Span <byte>(b, length)); json = new Utf8String(new ReadOnlySpan <byte>(b, length)); } //Console.WriteLine(utf8Str.Length); var reader = new JsonReader(json); while (reader.Read()) { var tokenType = reader.TokenType; switch (tokenType) { case JsonReader.JsonTokenType.ObjectStart: case JsonReader.JsonTokenType.ObjectEnd: case JsonReader.JsonTokenType.ArrayStart: case JsonReader.JsonTokenType.ArrayEnd: //Console.WriteLine(tokenType); break; case JsonReader.JsonTokenType.Property: var name = reader.GetName(); //Console.WriteLine(name); var value = reader.GetValue(); //Console.WriteLine(value); break; case JsonReader.JsonTokenType.Value: value = reader.GetValue(); //Console.WriteLine(value); break; default: throw new ArgumentOutOfRangeException(); } } }
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 JsonReader(new Utf8String(utf8)); while (reader.Read()) { switch (reader.TokenType) { case JsonReader.JsonTokenType.Property: var name = reader.GetName(); var type = reader.GetJsonValueType(); var value = reader.GetValue(); var current = stack.Peek(); var property = new JsonProperty(current, name); switch (type) { case JsonReader.JsonValueType.String: current._properties[property] = new JsonValue(value); break; case JsonReader.JsonValueType.Object: // 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 JsonReader.JsonValueType.True: current._properties[property] = new JsonValue(type); break; case JsonReader.JsonValueType.False: current._properties[property] = new JsonValue(type); break; case JsonReader.JsonValueType.Null: current._properties[property] = new JsonValue(type); break; case JsonReader.JsonValueType.Number: current._properties[property] = new JsonValue(value, type); break; case JsonReader.JsonValueType.Array: throw new NotImplementedException("array support not implemented yet."); default: throw new NotSupportedException(); } break; case JsonReader.JsonTokenType.ObjectStart: break; case JsonReader.JsonTokenType.ObjectEnd: if (stack.Count != 1) { stack.Pop(); } break; case JsonReader.JsonTokenType.ArrayStart: throw new NotImplementedException("array support not implemented yet."); case JsonReader.JsonTokenType.ArrayEnd: case JsonReader.JsonTokenType.Value: break; default: throw new NotSupportedException(); } } return(stack.Peek()); }
uint ReadCount(ReadOnlySpan<byte> json) { uint count; var reader = new JsonReader(new Utf8String(json)); while (reader.Read()) { switch (reader.TokenType) { case JsonReader.JsonTokenType.Property: var name = reader.GetName(); var value = reader.GetValue(); Console.WriteLine("Property {0} = {1}", name, value); if (name == "Count") { if (!InvariantParser.TryParse(value, out count)) { return 1; } return count; } break; } } return 1; }