/// <summary> /// Reads the next token ensuring that it matches the specified /// token. If not, an exception is thrown. /// </summary> public string ReadToken(JsonTokenClass token) { int depth = Depth; if (!token.IsTerminator) { MoveToContent(); } // // We allow an exception to the simple case of validating // the token and returning its value. If the reader is still at // the start (depth is zero) and we're being asked to check // for the null token or a scalar-type token then we allow that // to be appear within a one-length array. This is done because // valid JSON text must begin with an array or object. Our // JsonWriterBase automatically wraps a scalar value in an // array if not done explicitly. This exception here allow // that case to pass as being logically valid, as if the // token appeared entirely on its own between BOF and EOF. // string text; if (depth == 0 && TokenClass == JsonTokenClass.Array && (token.IsScalar || token == JsonTokenClass.Null)) { Read(/* array */); text = ReadToken(token); ReadToken(JsonTokenClass.EndArray); } else { if (TokenClass != token) { throw new JsonException(string.Format("Found {0} where {1} was expected.", TokenClass, token)); } text = Text; Read(); } return(text); }
/// <summary> /// Buffers the value at which the reader is positioned. /// </summary> /// <returns>Returns a <see cref="JsonBuffer" /> object that holds /// the buffered value.</returns> public JsonBuffer BufferValue() { if (EOF) { return(JsonBuffer.Empty); } JsonTokenClass tokenClass = TokenClass; if (tokenClass.IsTerminator || tokenClass == JsonTokenClass.Member) { Read(); } int start = _index; Skip(); return(_buffer.Slice(start, _index)); }
public static JsonBuffer From(JsonToken token) { JsonTokenClass clazz = token.Class; if (clazz == JsonTokenClass.Null) { return(_null); } if (!clazz.IsScalar) { throw new ArgumentException("Token must represent a JSON scalar value or null.", "token"); } if (clazz == JsonTokenClass.Boolean) { return(token.Equals(JsonToken.True()) ? _true : _false); } JsonBufferStorage storage = new JsonBufferStorage(1); storage.Write(token); return(storage.ToBuffer()); }
private JsonToken(JsonTokenClass clazz, string text) { _class = clazz; _text = text; }
private JsonToken(JsonTokenClass clazz) : this(clazz, null) { }