private async Task <JsonObject> ParseObjectAsync(IJsonReader jr, object parent, CancellationToken cancellationToken) { if (jr.TokenType != JsonToken.StartObject) { throw new Exception("Not an object"); } var obj = new JsonObject(Factory); if (!(await jr.ReadAsync(cancellationToken))) { throw new Exception("Failed to read object element."); } while (jr.TokenType != JsonToken.EndObject) { if (jr.TokenType != JsonToken.PropertyName) { throw new Exception("Failed to read object element."); } var name = (string)jr.Value; if (!(await jr.ReadAsync(cancellationToken))) { throw new Exception("Failed to read object element."); } obj[name] = await ParseAsync(jr, obj, cancellationToken); if (!(await jr.ReadAsync(cancellationToken))) { throw new Exception("Failed to read object element."); } } return(obj); }
/// <summary> /// Parses the content in the reader /// </summary> /// <param name="jr"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <IJsonStruct> ParseAsync(IJsonReader jr, CancellationToken cancellationToken = default(CancellationToken)) { if (!(await jr.ReadAsync(cancellationToken))) { return(null); } return(await ParseAsync(jr, Factory, cancellationToken)); }
private async Task <JsonArray> ParseArrayAsync(IJsonReader jr, object parent, CancellationToken cancellationToken) { if (jr.TokenType != JsonToken.StartArray) { throw new Exception("Not an array"); } var arr = new JsonArray(Factory); if (!(await jr.ReadAsync(cancellationToken))) { throw new Exception("Failed to read array element."); } while (jr.TokenType != JsonToken.EndArray) { arr.Add(await ParseAsync(jr, arr, cancellationToken)); if (!(await jr.ReadAsync(cancellationToken))) { throw new Exception("Failed to read array element."); } } return(arr); }