public static string ReadString(this IJsonReader reader, bool nextToken = true) { if (reader == null) { throw new ArgumentNullException("reader"); } var stringValue = default(string); switch (reader.Token) { case JsonToken.Null: break; case JsonToken.Member: case JsonToken.StringLiteral: case JsonToken.Number: case JsonToken.DateTime: case JsonToken.Boolean: stringValue = Convert.ToString(reader.RawValue, reader.Context.Format); break; default: throw JsonSerializationException.UnexpectedToken(reader, JsonToken.StringLiteral, JsonToken.Number, JsonToken.DateTime, JsonToken.Boolean); } if (nextToken) { reader.NextToken(); } return(stringValue); }
public static DateTime?ReadDateTimeOrNull(this IJsonReader reader, bool nextToken = true) { if (reader == null) { throw new ArgumentNullException("reader"); } var value = default(DateTime?); switch (reader.Token) { case JsonToken.Null: value = null; break; case JsonToken.Member: case JsonToken.StringLiteral: case JsonToken.Number: case JsonToken.DateTime: value = reader.Value.AsDateTime; break; default: throw JsonSerializationException.UnexpectedToken(reader, JsonToken.StringLiteral, JsonToken.Number, JsonToken.DateTime); } if (nextToken) { reader.NextToken(); } return(value); }
public static DateTime ReadDateTime(this IJsonReader reader, bool nextToken = true) { if (reader == null) { throw new ArgumentNullException("reader"); } var value = default(DateTime); if (reader.Token == JsonToken.Member || reader.Token == JsonToken.StringLiteral || reader.Token == JsonToken.Number || reader.Token == JsonToken.DateTime) { value = reader.Value.AsDateTime; } else { throw JsonSerializationException.UnexpectedToken(reader, JsonToken.StringLiteral, JsonToken.Number, JsonToken.DateTime); } if (nextToken) { reader.NextToken(); } return(value); }
public static void ReadNull(this IJsonReader reader, bool nextToken = true) { if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.Token != JsonToken.Null) { throw JsonSerializationException.UnexpectedToken(reader, JsonToken.Null); } if (nextToken) { reader.NextToken(); } }
public static void ReadObjectBegin(this IJsonReader reader, bool nextToken = true) { if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.Token != JsonToken.BeginObject) { throw JsonSerializationException.UnexpectedToken(reader, JsonToken.BeginObject); } if (reader.IsEndOfStream()) { throw JsonSerializationException.UnexpectedToken(reader, JsonToken.EndOfObject); } if (nextToken) { reader.NextToken(); } }
public static string ReadMember(this IJsonReader reader, bool nextToken = true) { if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.Token != JsonToken.Member) { throw JsonSerializationException.UnexpectedToken(reader, JsonToken.Member); } var memberName = (string)reader.RawValue; if (nextToken) { reader.NextToken(); } return(memberName); }