Skip to content

io microsphere json JSONTokener

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

JSONTokener

Type: Class | Module: microsphere-java-core | Package: io.microsphere.json

Source: microsphere-java-core/src/main/java/io/microsphere/json/JSONTokener.java

Overview

Parses a JSON (RFC 4627) encoded string into the corresponding object. Most clients of this class will use only need the #JSONTokener(String) constructor and #nextValue method.

Example Usage

`String json = "{"
        + "  \"query\": \"Pizza\", "
        + "  \"locations\": [ 94043, 90210 ] "
        + "`";

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
}

For best interoperability and performance use JSON that complies with RFC 4627, such as that generated by JSONStringer. For legacy reasons this parser is lenient, so a successful parse does not indicate that the input string was valid JSON. All the following syntax errors will be ignored:

  • End of line comments starting with // or # and ending with a newline character.
  • C-style comments starting with /* and ending with *``/. Such comments may not be nested.
  • Strings that are unquoted or 'single quoted'.
  • Hexadecimal integers prefixed with 0x or 0X.
  • Octal integers prefixed with 0.
  • Array elements separated by ;.
  • Unnecessary array separators. These are interpreted as if null was the omitted value.
  • Key-value pairs separated by = or =>.
  • Key-value pairs separated by ;.

Each tokener may be used to parse a single JSON string. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

Declaration

public class JSONTokener

Version Information

  • Introduced in: 0.1.10-SNAPSHOT (current)
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

String json = "{"
        + "  \"query\": \"Pizza\", "
        + "  \"locations\": [ 94043, 90210 ] "
        + "}";

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.json.JSONTokener;

API Reference

Public Methods

Method Description
nextValue The input JSON.
nextString Advances the position until after the next newline character. If the line is
readLiteral Unescapes the character identified by the character or characters that immediately
readObject Returns the string up to but not including any of the given characters or a newline
readArray Reads a sequence of values and the trailing closing brace ']' of an array. The
more
next
hasNext
next
nextClean
next
nextTo
nextTo
skipPast
skipTo
back
dehexchar

Method Details

nextValue

public Object nextValue()

The input JSON. / private final String in;

/** The index of the next character to be returned by #next. When the input is exhausted, this equals the input's length. / private int pos;

/**

nextString

public String nextString(char quote)

Advances the position until after the next newline character. If the line is terminated by "\r\n", the '\n' must be consumed as whitespace by the caller. / void skipToEndOfLine() { for (; hasNext(); this.pos++) { char c = currentChar(); if (c == '\r' || c == '\n') { this.pos++; break; } } }

/** Returns the string up to but not including quote, unescaping any character escape sequences encountered along the way. The opening quote should have already been read. This consumes the closing quote, but does not include it in the returned string.

readLiteral

public Object readLiteral()

Unescapes the character identified by the character or characters that immediately follow a backslash. The backslash '' should have already been read. This supports both unicode escapes "u000A" and two-character escapes "\n".

readObject

public JSONObject readObject()

Returns the string up to but not including any of the given characters or a newline character. This does not consume the excluded character.

readArray

public JSONArray readArray()

Reads a sequence of values and the trailing closing brace ']' of an array. The opening brace '[' should have already been read. Note that "[]" yields an empty array, but "[,]" returns a two-element array equivalent to "[null,null]".


This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally