APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.utils

public Class JsonMatcher

extends JsonSkimmer
Class Inheritance
Imports
java.io.InputStream, .Reader, com.badlogic.gdx.files.FileHandle, com.badlogic.gdx.utils.JsonValue.ValueType, .JsonWriter.OutputType

Efficient JSON parser that extracts values matching specified patterns using a single pass. It conveniently collects values for processing in batches or at once. Values not collected have minimal overhead, ideal for processing a subset of large JSON.

Match

Match objects, arrays, or field names (not field values) in the JSON:

Capture

Surround any match with parenthesis to capture that value.

Process

Use @ after a match to control when processors receive captured values.

Arrays

Arrays are captured as-is: Use [] in a capture to collect into an array rather than as a single value.

Examples

{users:[{name:nate},{name:iva}]}
Process each user name: users@/(name)
  1. The JSON root is matched implicitly.
  2. users matches the users:[ array. The field name and value are matched together.
  3. @ processes captured values at the end of each array item.
  4. (name) captures the "name" field and value.
  5. Result: Processors are called with nate and again with iva.

{config:{port:8081}}
Get the first port from "config" found at any depth: **/config/(port)
Result: Processors are called with: 8081

{services:[{status:up},{status:down},{status:failed}]}
Get all service statuses in an array: services/*/(status[])
Result: Processors are called with: [up, down, failed]

{items:[{id:123,type:cookies},{id:456,type:cake}]}
Process each "id" and "type" pair: items@/(id,type)
Result: Since there are multiple captures, processors are called with an object: {id=123,type=cookies} and {id=456,type=cake}

Escaping

Use special characters /,*@()[]',\ by surrounding them with single quotes.

Keys

Capture keys using ().

Behavior notes

JsonValues

Accessing values

Use process() or a processor to handle captures one by one. Use parseValue() to obtain captures directly:
JsonValue values = matcher.parseValue(json);
String error = value.getString("message", null);
if (error != null) fail(error);
return value.getString("access_token");
If an anonymous class processes values, var provides access to the fields (Java 10+):
var matcher = new JsonMatcher("(message,access_token)") {
	String error, accessToken;

	protected void process (JsonValue value) {
		error = value.getString("message", null);
		accessToken = value.getString("access_token");
	}
};
matcher.parse(json);
if (matcher.error != null) fail(matcher.error);
return matcher.accessToken;
Author
Nathan Sweet

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
pack-priv static class
public static interface

Field Summary

Modifier and TypeField and Description
pack-priv JsonMatcher.Pattern[]
pack-priv static final int
pack-priv static final int
pack-priv int
pack-priv char[]
private static final boolean
pack-priv int
pack-priv int
pack-priv static final int
pack-priv static final int
private static final int
pack-priv JsonMatcher.Pattern[]
pack-priv final IntArray
pack-priv JsonMatcher.Pattern[]
pack-priv static final int
pack-priv JsonMatcher.Processor
pack-priv JsonMatcher.Pattern
private boolean
pack-priv static final int
pack-priv boolean
pack-priv int
Inherited from com.badlogic.gdx.utils.JsonSkimmer:
bufferjson_en_arrayjson_en_mainjson_en_objectjson_errorjson_first_finaljson_startnameStringstackstopvalue

Constructor Summary

AccessConstructor and Description
public
public
JsonMatcher(String... patterns)

Adds all of the specified patterns.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The new pattern's index.
addPattern
(String pattern)

Adds a pattern for value extraction.

public int

Returns:

The new pattern's index.
addPattern
(String pattern, JsonMatcher.Processor processor)

Adds a pattern for value extraction.

private void
captureAllStart(JsonMatcher.Pattern pattern, int flags, JsonSkimmer.JsonToken name, boolean object)

private void
private void
private void
captureRoot()

If there are no patterns, set a temporary pattern to capture the root.

private void
public void
clear()

Clears any captured values for the current pattern.

public void
clear(int patternIndex)

Clears any captured values for the specified pattern.

public void
clearAll()

Clears any captured values for all patterns.

private void
public int
depth()

0 when not processing.

public void
end()

Causes parsing to stop as soon as possible, processing any values already collected.

pack-priv JsonMatcher.Match
newMatch(String name, boolean brackets, boolean at, boolean processEach, boolean valueCapture, boolean keyCapture, boolean star, boolean starStar)

pack-priv JsonMatcher.Node
newNode(JsonMatcher.Match[] matches, boolean processEach, JsonMatcher.Node backtrack, JsonMatcher.Node prev)

pack-priv JsonMatcher.Pattern
public String
parent()

Returns the last segment of the JSON path.

public String
parent(int up)

Returns the segment of the JSON path up the specified segments from the end, starting at 0, or "" if there aren't enough segments.

public void
parse(char[] data, int offset, int length)

Overrides com.badlogic.gdx.utils.JsonSkimmer.parse.

protected void
parseEnd()

Called just after parsing finishes.

protected void
parseStart()

Called just before parsing begins.

public JsonValue
parseValue(char[] data, int offset, int length)

Parses and returns captures for patterns without @.

public JsonValue
parseValue(String json)

Parses and returns captures for patterns without @.

public JsonValue
parseValue(Reader reader)

Parses and returns captures for patterns without @.

public JsonValue
parseValue(InputStream input)

Parses and returns captures for patterns without @.

public JsonValue
parseValue(FileHandle file)

Parses and returns captures for patterns without @.

public String
path()

Returns the current JSON path using "/" as separator.

public int
pattern()

Returns the index of the pattern currently being processed.

protected void
pop()

Overrides com.badlogic.gdx.utils.JsonSkimmer.pop.

Called when the end of an object or array is encountered in the JSON.

private void
process(JsonMatcher.Pattern pattern, boolean clear)

protected void
process(JsonValue value)

Called for all pattern matches, after any processors have been invoked.

protected void
push(JsonSkimmer.JsonToken name, boolean object)

Overrides com.badlogic.gdx.utils.JsonSkimmer.push.

Called when an object or array is encountered in the JSON.

public void
reject()

Rejects the current pattern.

public void
reject(int patternIndex)

Rejects a specific pattern.

public void
rejectAll()

Prevents further matching of any pattern at the current level or deeper.

public void
setProcessor(JsonMatcher.Processor processor)

This processor is invoked for all pattern matches, after per pattern processors and before process(JsonValue).

public void
stop()

Overrides com.badlogic.gdx.utils.JsonSkimmer.stop.

Causes parsing to stop as soon as possible, without any further processing.

protected void
value(JsonSkimmer.JsonToken name, JsonSkimmer.JsonToken value)

Overrides com.badlogic.gdx.utils.JsonSkimmer.value.

Called when a value is encountered in the JSON.

private JsonValue
Inherited from com.badlogic.gdx.utils.JsonSkimmer:
isStoppedparseparseparseparse

Field Detail

allback to summary
pack-priv JsonMatcher.Pattern[] all
arrayback to summary
pack-priv static final int array
captureback to summary
pack-priv static final int capture
capturedback to summary
pack-priv int captured
charsback to summary
pack-priv char[] chars
debugback to summary
private static final boolean debug
depthback to summary
pack-priv int depth
endCapturesback to summary
pack-priv int endCaptures
keysback to summary
pack-priv static final int keys
matchback to summary
pack-priv static final int match
noneback to summary
private static final int none
originalback to summary
pack-priv JsonMatcher.Pattern[] original
pathback to summary
pack-priv final IntArray path
patternsback to summary
pack-priv JsonMatcher.Pattern[] patterns
processback to summary
pack-priv static final int process
processorback to summary
pack-priv JsonMatcher.Processor processor
processPatternback to summary
pack-priv JsonMatcher.Pattern processPattern
rejectedback to summary
private boolean rejected
singleback to summary
pack-priv static final int single
stoppableback to summary
pack-priv boolean stoppable
totalback to summary
pack-priv int total

Constructor Detail

JsonMatcherback to summary
public JsonMatcher()
JsonMatcherback to summary
public JsonMatcher(String... patterns)

Adds all of the specified patterns.

Method Detail

addPatternback to summary
public int addPattern(String pattern)

Adds a pattern for value extraction.

Returns:int

The new pattern's index.

addPatternback to summary
public int addPattern(String pattern, JsonMatcher.Processor processor)

Adds a pattern for value extraction. The processor is invoked only for this pattern's matches.

Returns:int

The new pattern's index.

captureAllStartback to summary
private void captureAllStart(JsonMatcher.Pattern pattern, int flags, JsonSkimmer.JsonToken name, boolean object)
captureAllValueback to summary
private void captureAllValue(JsonMatcher.Pattern pattern, JsonSkimmer.JsonToken name, JsonValue value)
capturedback to summary
private void captured(JsonMatcher.Pattern pattern)
captureRootback to summary
private void captureRoot()

If there are no patterns, set a temporary pattern to capture the root.

captureValueback to summary
private void captureValue(JsonMatcher.Pattern pattern, int flags, JsonSkimmer.JsonToken name, JsonValue value)
clearback to summary
public void clear()

Clears any captured values for the current pattern. Only valid during processing.

clearback to summary
public void clear(int patternIndex)

Clears any captured values for the specified pattern. Only valid during processing.

clearAllback to summary
public void clearAll()

Clears any captured values for all patterns. Only valid during processing.

debugback to summary
private void debug(JsonMatcher.Pattern pattern, String text)
depthback to summary
public int depth()

0 when not processing.

endback to summary
public void end()

Causes parsing to stop as soon as possible, processing any values already collected.

newMatchback to summary
pack-priv JsonMatcher.Match newMatch(String name, boolean brackets, boolean at, boolean processEach, boolean valueCapture, boolean keyCapture, boolean star, boolean starStar)
See Also
PatternParser
newNodeback to summary
pack-priv JsonMatcher.Node newNode(JsonMatcher.Match[] matches, boolean processEach, JsonMatcher.Node backtrack, JsonMatcher.Node prev)
See Also
PatternParser
newPatternback to summary
pack-priv JsonMatcher.Pattern newPattern(JsonMatcher.Node root, JsonMatcher.Processor processor)
See Also
PatternParser
parentback to summary
public String parent()

Returns the last segment of the JSON path. "" when not processing.

See Also
path()
parentback to summary
public String parent(int up)

Returns the segment of the JSON path up the specified segments from the end, starting at 0, or "" if there aren't enough segments. "" when not processing.

See Also
path()
parseback to summary
public void parse(char[] data, int offset, int length)

Overrides com.badlogic.gdx.utils.JsonSkimmer.parse.

Annotations
@Override
parseEndback to summary
protected void parseEnd()

Called just after parsing finishes.

parseStartback to summary
protected void parseStart()

Called just before parsing begins.

parseValueback to summary
public JsonValue parseValue(char[] data, int offset, int length)

Parses and returns captures for patterns without @. Returns an array for multiple patterns without @.

parseValueback to summary
public JsonValue parseValue(String json)

Parses and returns captures for patterns without @. Returns an array for multiple patterns without @.

parseValueback to summary
public JsonValue parseValue(Reader reader)

Parses and returns captures for patterns without @. Returns an array for multiple patterns without @.

parseValueback to summary
public JsonValue parseValue(InputStream input)

Parses and returns captures for patterns without @. Returns an array for multiple patterns without @.

parseValueback to summary
public JsonValue parseValue(FileHandle file)

Parses and returns captures for patterns without @. Returns an array for multiple patterns without @.

pathback to summary
public String path()

Returns the current JSON path using "/" as separator. Anonymous objects use "{}", arrays "[]". "" when not processing.

patternback to summary
public int pattern()

Returns the index of the pattern currently being processed. -1 when not processing.

popback to summary
protected void pop()

Overrides com.badlogic.gdx.utils.JsonSkimmer.pop.

Doc from com.badlogic.gdx.utils.JsonSkimmer.pop.

Called when the end of an object or array is encountered in the JSON.

Annotations
@Override
processback to summary
private void process(JsonMatcher.Pattern pattern, boolean clear)
processback to summary
protected void process(JsonValue value)

Called for all pattern matches, after any processors have been invoked.

pushback to summary
protected void push(JsonSkimmer.JsonToken name, boolean object)

Overrides com.badlogic.gdx.utils.JsonSkimmer.push.

Doc from com.badlogic.gdx.utils.JsonSkimmer.push.

Called when an object or array is encountered in the JSON.

Annotations
@Override
rejectback to summary
public void reject()

Rejects the current pattern. Only valid during processing.

See Also
rejectAll()
rejectback to summary
public void reject(int patternIndex)

Rejects a specific pattern. Only valid during processing.

See Also
rejectAll()
rejectAllback to summary
public void rejectAll()

Prevents further matching of any pattern at the current level or deeper. Matching resumes when parsing returns to a higher level. Only valid during processing.

setProcessorback to summary
public void setProcessor(JsonMatcher.Processor processor)

This processor is invoked for all pattern matches, after per pattern processors and before process(JsonValue).

stopback to summary
public void stop()

Overrides com.badlogic.gdx.utils.JsonSkimmer.stop.

Causes parsing to stop as soon as possible, without any further processing.

Annotations
@Override
valueback to summary
protected void value(JsonSkimmer.JsonToken name, JsonSkimmer.JsonToken value)

Overrides com.badlogic.gdx.utils.JsonSkimmer.value.

Doc from com.badlogic.gdx.utils.JsonSkimmer.value.

Called when a value is encountered in the JSON.

Annotations
@Override
valueStartback to summary
private JsonValue valueStart()
com.badlogic.gdx.utils back to summary

pack-priv Class JsonMatcher.Match

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv int
pack-priv final String
pack-priv final boolean
pack-priv final boolean

Constructor Summary

AccessConstructor and Description
pack-priv
Match(String name, int flags, boolean star, boolean starStar)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

anyback to summary
pack-priv final boolean any
flagsback to summary
pack-priv int flags
nameback to summary
pack-priv final String name
starback to summary
pack-priv final boolean star
starStarback to summary
pack-priv final boolean starStar

Constructor Detail

Matchback to summary
pack-priv Match(String name, int flags, boolean star, boolean starStar)

Method Detail

toStringback to summary
pack-priv void toString(StringBuilder buffer)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

com.badlogic.gdx.utils back to summary

pack-priv Class JsonMatcher.Node

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv JsonMatcher.Node
pack-priv int
pack-priv final JsonMatcher.Match[]
pack-priv JsonMatcher.Node
pack-priv boolean
pack-priv int
pack-priv JsonMatcher.Node
pack-priv final boolean
pack-priv boolean
pack-priv boolean

Constructor Summary

AccessConstructor and Description
pack-priv
Node(JsonMatcher.Match[] matches, boolean processEach, JsonMatcher.Node backtrack)

Method Summary

Modifier and TypeMethod and Description
pack-priv int
private void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

backtrackback to summary
pack-priv JsonMatcher.Node backtrack
Annotations
@Null
deadback to summary
pack-priv int dead
matchesback to summary
pack-priv final JsonMatcher.Match[] matches
nextback to summary
pack-priv JsonMatcher.Node next
Annotations
@Null
nextStarStarback to summary
pack-priv boolean nextStarStar
popback to summary
pack-priv int pop
prevback to summary
pack-priv JsonMatcher.Node prev
Annotations
@Null
processEachback to summary
pack-priv final boolean processEach
processPopback to summary
pack-priv boolean processPop
starStarback to summary
pack-priv boolean starStar

Constructor Detail

Nodeback to summary
pack-priv Node(JsonMatcher.Match[] matches, boolean processEach, JsonMatcher.Node backtrack)

Method Detail

matchback to summary
pack-priv int match(JsonSkimmer.JsonToken name)
toStringback to summary
private void toString(StringBuilder buffer)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

com.badlogic.gdx.utils back to summary

pack-priv Class JsonMatcher.Pattern

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv JsonValue
pack-priv boolean
pack-priv int
pack-priv boolean
pack-priv JsonMatcher.Node
pack-priv final JsonMatcher.Processor
pack-priv final JsonMatcher.Node
pack-priv final Array<JsonValue>
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv
Pattern(JsonMatcher.Node root, JsonMatcher.Processor processor, int total, boolean at)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
pack-priv void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

atback to summary
pack-priv boolean at
captureback to summary
pack-priv JsonValue capture
captureAllback to summary
pack-priv boolean captureAll
capturedback to summary
pack-priv int captured
captureRootback to summary
pack-priv boolean captureRoot
currentback to summary
pack-priv JsonMatcher.Node current
processorback to summary
pack-priv final JsonMatcher.Processor processor
rootback to summary
pack-priv final JsonMatcher.Node root
stackback to summary
pack-priv final Array<JsonValue> stack
totalback to summary
pack-priv int total

Constructor Detail

Patternback to summary
pack-priv Pattern(JsonMatcher.Node root, JsonMatcher.Processor processor, int total, boolean at)

Method Detail

clearCaptureback to summary
pack-priv void clearCapture()
resetback to summary
pack-priv void reset()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

com.badlogic.gdx.utils back to summary

public Interface JsonMatcher.Processor


Method Summary

Modifier and TypeMethod and Description
public void
process(JsonValue
This value is reused once this method returns, except when called at the end of parsing.
value
)

Called with captured values.

Method Detail

processback to summary
public void process(JsonValue value)

Called with captured values.

Parameters
value:JsonValue

This value is reused once this method returns, except when called at the end of parsing.