public static async Task <RavenJToken> ReadFromAsync(JsonTextReaderAsync reader) { if (reader.TokenType == JsonToken.None) { if (!await reader.ReadAsync()) { throw new Exception("Error reading RavenJToken from JsonReader."); } } switch (reader.TokenType) { case JsonToken.StartObject: return(await RavenJObject.LoadAsync(reader)); case JsonToken.StartArray: return(await RavenJArray.LoadAsync(reader)); case JsonToken.String: case JsonToken.Integer: case JsonToken.Float: case JsonToken.Date: case JsonToken.Boolean: case JsonToken.Bytes: case JsonToken.Null: case JsonToken.Undefined: return(new RavenJValue(reader.Value)); } throw new Exception("Error reading RavenJToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); }
public static async Task <RavenJToken> LoadAsync(JsonTextReaderAsync reader) { if (reader.TokenType == JsonToken.None) { if (!await reader.ReadAsync()) { throw new Exception("Error reading RavenJArray from JsonReader."); } } if (reader.TokenType != JsonToken.StartArray) { throw new Exception("Error reading RavenJArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } if (await reader.ReadAsync() == false) { throw new Exception("Unexpected end of json array"); } var ar = new RavenJArray(); RavenJToken val = null; do { switch (reader.TokenType) { case JsonToken.Comment: // ignore comments break; case JsonToken.EndArray: return(ar); case JsonToken.StartObject: val = await RavenJObject.LoadAsync(reader); ar.Items.Add(val); break; case JsonToken.StartArray: val = await RavenJArray.LoadAsync(reader); ar.Items.Add(val); break; default: val = RavenJValue.Load(reader); ar.Items.Add(val); break; } } while (await reader.ReadAsync()); throw new Exception("Error reading RavenJArray from JsonReader."); }
public static async Task <RavenJToken> LoadAsync(JsonTextReaderAsync reader) { if (reader.TokenType == JsonToken.None) { if (!await reader.ReadAsync()) { throw new Exception("Error reading RavenJObject from JsonReader."); } } if (reader.TokenType != JsonToken.StartObject) { throw new Exception( "Error reading RavenJObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } if (await reader.ReadAsync() == false) { throw new Exception("Unexpected end of json object"); } string propName = null; var o = new RavenJObject(); do { switch (reader.TokenType) { case JsonToken.Comment: // ignore comments break; case JsonToken.PropertyName: propName = reader.Value.ToString(); break; case JsonToken.EndObject: return(o); case JsonToken.StartObject: if (!string.IsNullOrEmpty(propName)) { var val = await RavenJObject.LoadAsync(reader); o[propName] = val; // TODO: Assert when o.Properties.ContainsKey and its value != val propName = null; } else { throw new InvalidOperationException("The JsonReader should not be on a token of type {0}." .FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } break; case JsonToken.StartArray: if (!string.IsNullOrEmpty(propName)) { var val = await RavenJArray.LoadAsync(reader); o[propName] = val; // TODO: Assert when o.Properties.ContainsKey and its value != val propName = null; } else { throw new InvalidOperationException("The JsonReader should not be on a token of type {0}." .FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } break; default: if (!string.IsNullOrEmpty(propName)) { var val = RavenJValue.Load(reader); o[propName] = val; // TODO: Assert when o.Properties.ContainsKey and its value != val propName = null; } else { throw new InvalidOperationException("The JsonReader should not be on a token of type {0}." .FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } break; } } while (await reader.ReadAsync()); throw new Exception("Error reading RavenJObject from JsonReader."); }