Class Result
An evaluator for a very small expression language to extract primitive types
from structured information. This implementation is layered over the
com.codename1.io.JSONParser and com.codename1.xml.XMLParser classes. This
expression language allows applications to extract information from
structured data returned by web services with minimal effort. You can read more about it here.
The expression language works a lot like a very small subset of XPath - the expression syntax uses the / character for sub-elements and square brackets for arrays.
Some sample expressions:
`Simple expression, get the title of the first photo element.
/photos/photo[1]/title
Globally find the first name of a person with a last name of 'Coolman'.
//person[lastname='Coolman']/firstName
Get the latitude value of the second last result element.
/results[last()-1]/geometry/bounds/northeast/lat
Get the names of players from Germany
/tournament/player[@nationality='Germany']/name
Get the purchase order numbers of any order with a lineitem worth over $5
//order/lineitem[price > 5]/../@ponum
etc`
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbooleanIndicates whether some other object is "equal to" this one.static ResultfromContent(Element content) Create an evaluator object from a parsed XML DOM.static ResultfromContent(InputStream content, String format) Create an evaluator object from a structured content document (XML, JSON, etc) input stream.static ResultfromContent(Reader content, String format) Create an evaluator object from a structured content document (XML, JSON, etc) input stream.static ResultfromContent(String content, String format) Create an evaluator object from a structured content document (XML, JSON, etc) as a string.static ResultfromContent(Map content) Create an evaluator object from parsed JSON content DOM.Get the object value from the requested path.getAsArray(String path) Get a List of values from the requested path.booleangetAsBoolean(String path) Get a boolean value from the requested path.boolean[]getAsBooleanArray(String path) Get an array of values from the requested path.byte[]getAsByteArray(String path) Get an array of byte values from the requested path.doublegetAsDouble(String path) Get a double value from the requested path.double[]getAsDoubleArray(String path) Get an array of values from the requested path.float[]getAsFloatArray(String path) Get an array of float values from the requested path.intgetAsInteger(String path) Get an integer value from the requested path.int[]getAsIntegerArray(String path) Get an array of values from the requested path.longGet a long value from the requested path.long[]getAsLongArray(String path) Get an array of values from the requested path.short[]getAsShortArray(String path) Get an array of short values from the requested path.getAsString(String path) Get a string value from the requested path.String[]getAsStringArray(String path) Get an array of string values from the requested path.intgetSizeOfArray(String path) Get the size of an array at the requested path.inthashCode()Returns a hashcode value for the object.voidmapNamespaceAlias(String namespaceURI, String alias) toString()Convert the object to a formatted structured content document.
-
Field Details
-
JSON
- See Also:
-
XML
- See Also:
-
SEPARATOR
public static final char SEPARATOR- See Also:
-
ARRAY_START
public static final char ARRAY_START- See Also:
-
ARRAY_END
public static final char ARRAY_END- See Also:
-
-
Method Details
-
fromContent
Create an evaluator object from a structured content document (XML, JSON, etc) as a string.
Parameters
-
content: structured content document as a string. -
format: @param format an identifier for the type of content passed (ie. xml, json, etc).
Returns
Result a result evaluator object
Throws
IllegalArgumentException: @throws IllegalArgumentException thrown if null content or format is passed.
- Throws:
IllegalArgumentException
-
-
fromContent
public static Result fromContent(InputStream content, String format) throws IllegalArgumentException, IOException Create an evaluator object from a structured content document (XML, JSON, etc) input stream. Normally you would use this method within a content request implementation, for example:
ConnectionRequest request = new ConnectionRequest() { protected void readResponse(InputStream input) throws IOException { Result evaluator = Result.fromContent(input, Result.JSON); // ... evaluate the result here } // ... etc };Parameters
-
content: structured content document as a string. -
format: @param format an identifier for the type of content passed (ie. xml, json, etc).
Returns
Result a result evaluator object
Throws
IllegalArgumentException: @throws IllegalArgumentException thrown if null content or format is passed.
- Throws:
IllegalArgumentExceptionIOException
-
-
fromContent
public static Result fromContent(Reader content, String format) throws IllegalArgumentException, IOException Create an evaluator object from a structured content document (XML, JSON, etc) input stream. Normally you would use this method within a content request implementation, for example:
ConnectionRequest request = new ConnectionRequest() { protected void readResponse(InputStream input) throws IOException { Result evaluator = Result.fromContent(input, Result.JSON); // ... evaluate the result here } // ... etc };Parameters
-
content: structured content document as a string. -
format: @param format an identifier for the type of content passed (ie. xml, json, etc).
Returns
Result a result evaluator object
Throws
IllegalArgumentException: @throws IllegalArgumentException thrown if null content or format is passed.
- Throws:
IllegalArgumentExceptionIOException
-
-
fromContent
Create an evaluator object from a parsed XML DOM.
Parameters
content: a parsed XML DOM.
Returns
Result a result evaluator object
Throws
IllegalArgumentException: thrown if null content is passed.
- Throws:
IllegalArgumentException
-
fromContent
Create an evaluator object from parsed JSON content DOM.
Parameters
content: JSON content input stream
Returns
Result a result evaluator object
- Throws:
IllegalArgumentException
-
hashCode
-
equals
-
toString
-
getAsBoolean
Get a boolean value from the requested path.
For example: JSON
{ "settings" : [ { "toggle" : "true", ... etc }Expression
boolean value = result.getAsBoolean("/settings[0]/toggle");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsInteger
Get an integer value from the requested path.
For example: JSON
{ "settings" { "connection" { "max_retries" : "20", ... etc } }Expression
int value = result.getAsInteger("//connection/max_retries");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalException: @throws IllegalException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsLong
Get a long value from the requested path.
For example: JSON
{ "settings" { "connection" { "timeout_milliseconds" : "100000", ... etc } }Expression
long value = result.getAsLong("/settings/connection/timeout_milliseconds");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsDouble
Get a double value from the requested path.
For example: JSON
{ "geometry" : { "bounds" : { "northeast" : { "lat" : 42.94959820, "lng" : -81.24873959999999 }, "southwest" : { "lat" : 42.94830, "lng" : -81.24901740000001 } }, "location" : { "lat" : 42.94886990, "lng" : -81.24876030 }, "location_type" : "RANGE_INTERPOLATED", "viewport" : { "northeast" : { "lat" : 42.95029808029150, "lng" : -81.24752951970851 }, "southwest" : { "lat" : 42.94760011970850, "lng" : -81.25022748029151 } } // etcExpression
double neBoundsLat = result.getAsDouble("//bounds/northeast/lat"); double neBoundsLong = result.getAsDouble("//bounds/northeast/lng"); double swBoundsLat = result.getAsDouble("//bounds/southwest/lat"); double swBoundsLong = result.getAsDouble("//bounds/southwest/lng"); double memberDiscount = result.getAsDouble("pricing.members.members");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsString
Get a string value from the requested path.
For example: JSON
{ "profile" { "location" { "city" : "London", "region" : "Ontario", "country" : "Canada", ... etc }, }Expression
String city = result.getAsDouble("//city"); String province = result.getAsDouble("//location//region"); String country = result.getAsDouble("profile//location//country");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
get
Get the object value from the requested path. This method may return a Map, List, String, or null.
Parameters
path
Returns
the object at the given path, or null.
Throws
IllegalArgumentException
- Throws:
IllegalArgumentException
-
getSizeOfArray
Get the size of an array at the requested path.
For example: JSON
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, { "long_name" : "Ontario", "short_name" : "ON", "types" : [ "administrative_area_level_1", "political" ] }, ... etc } }Expression
int size = result.getSizeOfArray("/results[0]/address_components"); int size2 = result.getSizeOfArray("results"); int size3 = result.getSizeOfArray("/results[0]/address_components[2]/types");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsStringArray
Get an array of string values from the requested path.
For example: JSON
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, { "long_name" : "Ontario", "short_name" : "ON", "types" : [ "administrative_area_level_1", "political" ] }, ... etc } }Expression
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsIntegerArray
Get an array of values from the requested path.
For example: JSON
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, { "long_name" : "Ontario", "short_name" : "ON", "types" : [ "administrative_area_level_1", "political" ] }, ... etc } }Expression
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
-
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts. -
NumberFormatException: @throws NumberFormatException if the value at path can not be converted to an integer.
- Throws:
IllegalArgumentException
-
getAsLongArray
Get an array of values from the requested path.
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
-
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts. -
NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a long.
- Throws:
IllegalArgumentException
-
getAsDoubleArray
Get an array of values from the requested path.
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
-
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts. -
NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a double.
- Throws:
IllegalArgumentException
-
getAsBooleanArray
Get an array of values from the requested path.
String types[] = result .getAsStringArray("/results[0]/address_components[2]/types");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
getAsShortArray
Get an array of short values from the requested path.
short values[] = result.getAsShortArray("//values");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
-
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts. -
NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a short.
- Throws:
IllegalArgumentException
-
getAsFloatArray
Get an array of float values from the requested path.
float values[] = result.getAsFloatArray("//values");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
-
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts. -
NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a float.
- Throws:
IllegalArgumentException
-
getAsByteArray
Get an array of byte values from the requested path.
byte values[] = result.getAsByteArray("//values");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
-
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts. -
NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a byte.
- Throws:
IllegalArgumentException
-
getAsArray
Get a List of values from the requested path.
For example: JSON
{ "results" : [ { "address_components" : [ { "long_name" : "921-989", "short_name" : "921-989", "types" : [ "street_number" ] }, { "long_name" : "Country Club Crescent", "short_name" : "Country Club Crescent", "types" : [ "route" ] }, ... etc } }Expression
List addressComponents = result.getAsList("/results[0]/address_components"); result = Result.fromContent(addressComponents); String longName = result.getAsString("[1]/long_name");Parameters
path: Path expression to evaluate
Returns
the value at the requested path
Throws
IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
- Throws:
IllegalArgumentException
-
mapNamespaceAlias
-