Top Description Constructors Methods
com.google.gson

public final Class JsonParser

extends Object
Class Inheritance
Imports
com.google.errorprone.annotations.InlineMe, com.google.gson.internal.Streams, com.google.gson.stream.JsonReader, .JsonToken, .MalformedJsonException, java.io.IOException, .Reader, .StringReader

A parser to parse JSON into a parse tree of JsonElements.

The JSON data is parsed in lenient mode.

Here's an example of parsing from a string:

String json = "{\"key\": \"value\"}";
JsonElement jsonElement = JsonParser.parseString(json);
JsonObject jsonObject = jsonElement.getAsJsonObject();

It can also parse from a reader:

try (Reader reader = new FileReader("my-data.json", StandardCharsets.UTF_8)) {
  JsonElement jsonElement = JsonParser.parseReader(reader);
  JsonObject jsonObject = jsonElement.getAsJsonObject();
}

If you want to parse from a JsonReader for more customized parsing requirements, the following example demonstrates how to achieve it:

String json = "{\"skipObj\": {\"skipKey\": \"skipValue\"}, \"obj\": {\"key\": \"value\"}}";
try (JsonReader jsonReader = new JsonReader(new StringReader(json))) {
  jsonReader.beginObject();
  while (jsonReader.hasNext()) {
    String fieldName = jsonReader.nextName();
    if (fieldName.equals("skipObj")) {
      jsonReader.skipValue();
    } else {
      JsonElement jsonElement = JsonParser.parseReader(jsonReader);
      JsonObject jsonObject = jsonElement.getAsJsonObject();
    }
  }
  jsonReader.endObject();
}
Authors
Inderjeet Singh, Joel Leitch
Since
1.3

Constructor Summary

AccessConstructor and Description
public
JsonParser()
Deprecated No need to instantiate this class, use the static methods instead.

Method Summary

Modifier and TypeMethod and Description
public JsonElement
parse(String json)
Deprecated Use JsonParser#parseString

public JsonElement
public JsonElement
public static JsonElement

Returns:

a parse tree of JsonElements corresponding to the specified JSON
parseReader
(Reader
JSON text
reader
)

Parses the complete JSON string provided by the reader into a parse tree.

public static JsonElement
parseReader(JsonReader reader)

Returns the next value from the JSON stream as a parse tree.

public static JsonElement

Returns:

a parse tree of JsonElements corresponding to the specified JSON
parseString
(String
JSON text
json
)

Parses the specified JSON string into a parse tree.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

JsonParserback to summary
public JsonParser()

Deprecated

No need to instantiate this class, use the static methods instead.

Annotations
@Deprecated

Method Detail

parseback to summary
public JsonElement parse(String json) throws JsonSyntaxException

Deprecated

Use JsonParser#parseString

Annotations
@Deprecated
@InlineMe
replacement:JsonParser.parseString(json)
imports:JsonParser
parseback to summary
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException
Annotations
@Deprecated
@InlineMe
replacement:JsonParser.parseReader(json)
imports:JsonParser
parseback to summary
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException
Annotations
@Deprecated
@InlineMe
replacement:JsonParser.parseReader(json)
imports:JsonParser
parseReaderback to summary
public static JsonElement parseReader(Reader reader) throws JsonIOException, JsonSyntaxException

Parses the complete JSON string provided by the reader into a parse tree. An exception is thrown if the JSON string has multiple top-level JSON elements, or if there is trailing data.

The JSON data is parsed in lenient mode.

Parameters
reader:Reader

JSON text

Returns:JsonElement

a parse tree of JsonElements corresponding to the specified JSON

Exceptions
JsonParseException:
if there is an IOException or if the specified text is not valid JSON
Since
2.8.6
parseReaderback to summary
public static JsonElement parseReader(JsonReader reader) throws JsonIOException, JsonSyntaxException

Returns the next value from the JSON stream as a parse tree. Unlike the other parse methods, no exception is thrown if the JSON data has multiple top-level JSON elements, or if there is trailing data.

If the strictness of the reader is Strictness#STRICT, that strictness will be used for parsing. Otherwise the strictness will be temporarily changed to Strictness#LENIENT and will be restored once this method returns.

Exceptions
JsonParseException:
if there is an IOException or if the specified text is not valid JSON
Since
2.8.6
parseStringback to summary
public static JsonElement parseString(String json) throws JsonSyntaxException

Parses the specified JSON string into a parse tree. An exception is thrown if the JSON string has multiple top-level JSON elements, or if there is trailing data.

The JSON string is parsed in lenient mode.

Parameters
json:String

JSON text

Returns:JsonElement

a parse tree of JsonElements corresponding to the specified JSON

Exceptions
JsonParseException:
if the specified text is not valid JSON
Since
2.8.6