/// <summary> /// Parses a JSON-text as defined in http://tools.ietf.org/html/rfc4627, returning an /// IDictionary<string, object> or an IList<object> depending on whether /// the value was an array or dictionary. Nested objects also match these types. /// </summary> public static object Parse(string input) { object output; input = input.Trim(); JsonStringParser parser = new JsonStringParser(input); if ((parser.ParseObject(out output) || parser.ParseArray(out output)) && parser.CurrentIndex == input.Length) { return output; } throw new ArgumentException("Input JSON was invalid."); }
public static void ParseStreamLine(JsonStringParser parser, string line, IStreamHandler handler) { try { var element = parser.Parse(line); ParseStreamLine(element, handler); } catch (Exception ex) { handler.OnException(new StreamParseException( "JSON parse failed.", line, ex)); } }
private static async Task <string> HttpGetFromWebApi(string url, string resultJsonStringParameterName, SetBearerTokenAction setBearerTokenAction) { string resultJsonParameterValue = ""; using (var client = new HttpClient()) { setBearerTokenAction.Invoke(client); var response = await client.GetAsync(url); var resultJson = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { resultJsonParameterValue = JsonStringParser.GetValue(resultJsonStringParameterName, resultJson); } else { throw new PinAuthenticationException(response.StatusCode.ToString()); //TODO: map from status code to the error //TODO: make and use service exception } } return(resultJsonParameterValue); }
public void UserStreamParserPerformanceTest() { var workingset = Environment.WorkingSet; var parser = new JsonStringParser(); // pre-work for (var i = 0; i < 100; i++) { foreach (var elem in TweetSamples.GetStreamSampleElements()) { parser.Parse(elem); } } var source = new CancellationTokenSource(); var handler = new PseudoStreamHandler(); var received = 0; source.CancelAfter(TimeSpan.FromSeconds(10)); foreach (var content in TweetSamples.GetStreamSamples()) { if (source.IsCancellationRequested) { break; } received++; UserStreamParser.ParseStreamLine(parser, content, handler); } var wsa = Environment.WorkingSet; TestContext.WriteLine("received: {0}", received); TestContext.WriteLine("handler: statuses: {0} / events: {1}", handler.ReceivedStatuses, handler.ReceivedEvents); TestContext.WriteLine("cache: {0} / {1}", parser.CacheCount(), parser.ALQCount()); TestContext.WriteLine("workingset delta: {0}", wsa - workingset); }
/// <inheritdoc /> public JsonItem ParseNext() { JsonString propertyName = null; string propertyNameAsString; IDictionary <string, JsonItem> propertyBag = new Dictionary <string, JsonItem>(); this.state = JsonObjectParseState.Start; while (this.state != JsonObjectParseState.End) { JsonItem item = null; switch (this.state) { case JsonObjectParseState.Start: if (this.Consume('{')) { this.state = JsonObjectParseState.PropertyName; new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); } else { return(this.MakeError('{')); } break; case JsonObjectParseState.PropertyName: if (this.Peek('"')) { item = new JsonStringParser(this.Buffer).ParseNext(); if (item.IsString) { propertyName = (JsonString)item; } else { return(item); } new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.KeyValueSperator; } else if (this.Consume('}')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.End; } else { return(this.MakeError('"')); } break; case JsonObjectParseState.KeyValueSperator: if (this.Consume(':')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.Value; } else { return(this.MakeError(':')); } break; case JsonObjectParseState.Value: item = new JsonValueParser(this.Buffer).ParseNext(); if (item.IsError) { return(item); } propertyName.TryGetValue(out propertyNameAsString); propertyBag.Add(propertyNameAsString, item); new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.PropertySetSeperator; break; case JsonObjectParseState.PropertySetSeperator: if (this.Consume(',')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.PropertyName; } else if (this.Consume('}')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.End; } else { return(this.MakeError(',')); } break; } } return(new JsonObject(propertyBag)); }
/// <inheritdoc /> public JsonItem ParseNext() { JsonString propertyName = null; string propertyNameAsString; IDictionary<string, JsonItem> propertyBag = new Dictionary<string, JsonItem>(); this.state = JsonObjectParseState.Start; while (this.state != JsonObjectParseState.End) { JsonItem item = null; switch (this.state) { case JsonObjectParseState.Start: if (this.Consume('{')) { this.state = JsonObjectParseState.PropertyName; new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); } else { return this.MakeError('{'); } break; case JsonObjectParseState.PropertyName: if (this.Peek('"')) { item = new JsonStringParser(this.Buffer).ParseNext(); if (item.IsString) { propertyName = (JsonString)item; } else { return item; } new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.KeyValueSperator; } else if (this.Consume('}')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.End; } else { return this.MakeError('"'); } break; case JsonObjectParseState.KeyValueSperator: if (this.Consume(':')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.Value; } else { return this.MakeError(':'); } break; case JsonObjectParseState.Value: item = new JsonValueParser(this.Buffer).ParseNext(); if (item.IsError) { return item; } propertyName.TryGetValue(out propertyNameAsString); propertyBag.Add(propertyNameAsString, item); new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.PropertySetSeperator; break; case JsonObjectParseState.PropertySetSeperator: if (this.Consume(',')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.PropertyName; } else if (this.Consume('}')) { new JsonWhiteSpaceParser(JsonParseType.Object, this.Buffer).ParseNext(); this.state = JsonObjectParseState.End; } else { return this.MakeError(','); } break; } } return new JsonObject(propertyBag); }
private string parseJsonObject(string jsonParameterName, string jsonString) => JsonStringParser.GetValue(jsonParameterName, jsonString);