public class MBObjectParser extends Object
JSONObject
and perform Expression and
Filter conversions. These utilities are used by the MBStyle to convert JSON to simple Java
objects, process functions and perform common JSON manipulation tasks.
MBObjectParser parse = new MBObjectParser( ff );
Expression fillOpacity = parse.percent( json, "fill-opacity", 1.0 );
Expression fillColor = parse.color( json, "fill-color", Color.BLACK );
String id = parse.get("id");
String visibility = parse.getBoolean("visibility");
String source = parse.get("source");
Non generic "get" methods, like paint(JSONObject)
, are in position to provide an
appropriate default value.
JSONObject paint = parse.paint( layer );
Constructor and Description |
---|
MBObjectParser(Class<?> context)
Parser used to in the provided context.
|
MBObjectParser(Class<MBFilter> context,
MBObjectParser parse)
Copy constructor allowing reuse of factories, while returning correct
context . |
Modifier and Type | Method and Description |
---|---|
<T> T[] |
array(Class<T> type,
JSONObject json,
String name,
T[] fallback)
Lookup an array of the provided type in the provided JSONObject at 'tag', with a fallback if
that tag is not found.
|
double[] |
array(JSONObject json,
String name,
double[] fallback)
Convert to double[]
|
int[] |
array(JSONObject json,
String name,
int[] fallback)
Convert to int[]
|
Expression |
bool(JSONObject json,
String name,
boolean fallback)
Convert json to Expression boolean, or a function.
|
Expression |
color(JSONObject json,
String name,
Color fallback)
Convert json to GeoTools Expression color, or a function.
|
Literal |
color(String color)
Handles literal color definitions supplied as a string, returning a
Literal . |
Literal |
constant(Object value,
Class<? extends Enum<?>> enumeration)
Utility method used to convert enumerations to an appropriate GeoTools literal string.
|
Color |
convertToColor(String color)
Converts color definitions supplied as a string to Color objects:
{"line-color": "yellow"
named: a few have been put in pass test cases, prnding: plan to use
Hints.COLOR_DEFINITION to allow for web colors. |
Displacement |
displacement(JSONObject json,
String name,
Displacement fallback)
Maps a json value at 'tag' in the provided JSONObject to a
Displacement . |
<T extends Enum<?>> |
enumToExpression(JSONObject json,
String name,
Class<T> enumeration,
T fallback)
Parse a Mapbox enumeration property to a GeoTools Expression that evaluates to a GeoTools
constant (supports the property being specified as a mapbox function).
|
Expression |
font(JSONObject json,
String name)
Convert the provided object to a font Expression (or function).
|
String |
get(JSONArray json,
int index)
Quickly access required json index (as a String).
|
String |
get(JSONObject json,
String name)
Quickly access required json tag (as a String).
|
String |
get(JSONObject json,
String name,
String fallback)
Quickly access optional json tag.
|
Boolean |
getBoolean(JSONArray json,
int index)
Look up a Boolean in the provided
JSONArray at the provided index, or throw an MBFormatException . |
Boolean |
getBoolean(JSONObject json,
String name)
Look up a Boolean in the provided
JSONObject at the provided 'tag', or thrown an
MBFormatException . |
Boolean |
getBoolean(JSONObject json,
String name,
Boolean fallback)
Look up a Boolean in the provided
JSONObject at the provided 'tag', or thrown an
MBFormatException , with a fallback if the json is null or contains no such 'tag'. |
<T extends Enum<?>> |
getEnum(JSONObject json,
String name,
Class<T> enumeration,
T fallback)
Convert a String value to an enum value, ignoring case, with the provided fallback.
|
FilterFactory2 |
getFilterFactory()
Shared FilterFactory
|
JSONArray |
getJSONArray(JSONObject json,
String name)
Access json contains a JSONArray for the indicated tag.
|
JSONArray |
getJSONArray(JSONObject json,
String name,
JSONArray fallback)
Access a JSONArray at the provided tag in the provided JSONObject, with a fallback if no
JSONArray is found at that tag.
|
JSONObject |
getJSONObject(JSONObject json,
String name)
Access JSONObject for the indicated tag.
|
JSONObject |
getJSONObject(JSONObject json,
String name,
JSONObject fallback)
Access JSONObject for the indicated tag, with the provided fallback if the the json does not
contain a JSONObject for that tag.
|
double |
getNumeric(JSONArray json,
int index)
Look up a double in the provided
JSONArray at the provided index, or throw an MBFormatException . |
Double |
getNumeric(JSONObject json,
String name)
Look up a Double in the provided
JSONObject at the provided 'tag', or thrown an
MBFormatException . |
StyleFactory2 |
getStyleFactory()
Shared StyleFactory
|
boolean |
isArray(JSONArray json,
int index)
True if array has an array element at the provided index, False otherwise.
|
boolean |
isArray(JSONObject json,
String name)
True if json has an array value for the provided name, False otherwise.
|
boolean |
isBoolean(JSONArray json,
int index)
True if array has a boolean element at the provided index, False otherwise.
|
boolean |
isBoolean(JSONObject json,
String name)
True if json has a boolean value for the provided name, False otherwise.
|
boolean |
isDefined(JSONArray json,
int index)
True if array has an element at the provided index, False otherwise.
|
boolean |
isDefined(JSONObject json,
String name)
True if json has a value for the provided name, False otherwise.
|
boolean |
isNumeric(JSONArray json,
int index)
True if array has a numeric element at the provided index, False otherwise.
|
boolean |
isNumeric(JSONObject json,
String name)
True if json has a numeric value for the provided name, False otherwise.
|
boolean |
isObject(JSONArray json,
int index)
True if array has an object element at the provided index, False otherwise.
|
boolean |
isObject(JSONObject json,
String name)
True if json has an object value for the provided name, False otherwise.
|
boolean |
isPropertyDefined(JSONObject json,
String propertyName)
Deprecated.
|
boolean |
isString(JSONArray json,
int index)
True if array has a string element at the provided index, False otherwise.
|
boolean |
isString(JSONObject json,
String name)
True if json has a string value for the provided name, False otherwise.
|
JSONArray |
jsonArray(Object obj)
Casts the provided obj to a JSONArray (otherwise throws an
MBFormatException ). |
JSONArray |
jsonArray(Object obj,
String message)
Casts the provided obj to a JSONArray (otherwise throws an
MBFormatException with the
provided message). |
JSONObject |
jsonObect(Object obj,
String message)
Casts the provided obj to a JSONObject (safely reporting format exception, with the provided
message).
|
JSONObject |
jsonObject(Object obj)
Casts the provided obj to a JSONObject (safely reporting format exception
|
JSONObject |
layout(JSONObject layer)
Safely look up layout in provided layer json.
|
Expression |
number(JSONArray json,
int index)
Convert the value at 'index' in the provided JSONArray to a numeric Expression or a Function.
|
Expression |
number(JSONArray json,
int index,
Number fallback)
Convert the value in the provided JSONArray at index to a numeric Expression, or a function,
with a fallback if the json is null.
|
Expression |
number(JSONObject json,
String name)
Convert the value in the provided JSONObject at 'tag' to a numeric GeoTools Expression, or a
Function.
|
Expression |
number(JSONObject json,
String name,
Number fallback)
Convert the value in the provided JSONObject at 'tag' to a numeric Expression, or a function,
with a fallback if the json is null.
|
<T> T |
optional(Class<T> type,
JSONObject json,
String name,
T fallback)
Optional lookup, will return fallback if not available.
|
JSONObject |
paint(JSONObject layer)
Safely look up paint in provided layer json.
|
Expression |
percentage(JSONObject json,
String name)
Convert json to Expression number between 0 and 1, or a function.
|
Expression |
percentage(JSONObject json,
String name,
Number fallback)
Convert json to Expression number between 0 and 1, or a function.
|
<T> T |
require(Class<T> type,
JSONArray json,
int index)
Retrieve an object of the provided type in the JSONArray at this index, throwing an
MBFormatException if no object of that type is found at that index of the array. |
<T> T |
require(Class<T> type,
JSONObject json,
String name)
Retrieve an object of the provided type in the JSONObject at this tag, throwing an
MBFormatException if no object of that type is found at that tag in the object. |
Expression |
string(JSONArray json,
int index)
Convert the value in the provided JSONArray at index to a string Literal, or a Function.
|
Expression |
string(JSONObject json,
String name,
String fallback)
Convert json to GeoTools Expression string, or a function.
|
Object |
value(JSONArray json,
int index)
Access a literal value (string, numeric, or boolean).
|
Object |
value(JSONObject json,
String name)
Access a literal value (string, numeric, or boolean).
|
public MBObjectParser(Class<?> context)
context
- Context this parser is being used in (for better error reporting)public MBObjectParser(Class<MBFilter> context, MBObjectParser parse)
context
.context
- Context this parser is being used in (for better error reporting)parse
- Parent parser used to configure factories consistentlypublic FilterFactory2 getFilterFactory()
public StyleFactory2 getStyleFactory()
public JSONObject paint(JSONObject layer)
Paint is optional, returning an empty JSONObject (to prevent the need for null checks).
MBFormatException
- If paint is provided as an invalid type (such as boolean).public JSONObject layout(JSONObject layer)
Layout is optional, returning an empty JSONObject (to prevent the need for null checks).
MBFormatException
- If layout is provided as an invalid type (such as boolean).public JSONObject getJSONObject(JSONObject json, String name)
Confirms json contains the provided tag as a JSONObject, correctly throwing MBFormatException
if not available.
json
- The JSONObject in which to lookup the provided tag and return a JSONObjectname
- The tag to look up in the provided JSONObjectMBFormatException
- If JSONObject not available for the provided tagpublic JSONObject getJSONObject(JSONObject json, String name, JSONObject fallback)
json
- The JSONObject in which to lookup the provided tag and return a JSONObjectname
- The tag to look up in the provided JSONObjectfallback
- The JSONObject to return if the provided json does not contain a JSONObject
for that tag.public JSONArray getJSONArray(JSONObject json, String name)
Confirms json contains the provided tag as a JSONArray, correctly throwing MBFormatException
if not available.
MBFormatException
- If JSONObject not available for the provided tagpublic JSONArray getJSONArray(JSONObject json, String name, JSONArray fallback)
json
- The JSONObject in which to lookup the provided tag and return a JSONArrayname
- The tag to look up in the provided JSONObjectfallback
- The JSONArray to return if the provided json does not contain a JSONArray for
that tag.public Object value(JSONArray json, int index)
json
- JSONArray object to parse.index
- position in the provided array for which to retrieve a value.MBFormatException
- if required index not available.public Object value(JSONObject json, String name)
MBFormatException
- if required tag not available.public String get(JSONArray json, int index)
MBFormatException
- if required index not available.public String get(JSONObject json, String name)
json
- The object in which to look up the Stringname
- Tag to lookupMBFormatException
- if required tag not available.public String get(JSONObject json, String name, String fallback)
json
- The object in which to look up the Stringname
- Tag to lookupfallback
- default stringpublic double getNumeric(JSONArray json, int index)
JSONArray
at the provided index, or throw an MBFormatException
.json
- The array in which to look up the doubleindex
- The index at which to look up the doublepublic Double getNumeric(JSONObject json, String name)
JSONObject
at the provided 'tag', or thrown an
MBFormatException
.json
- The object in which to look up the Doublename
- The tag at which to look up the Doublepublic Boolean getBoolean(JSONArray json, int index)
JSONArray
at the provided index, or throw an MBFormatException
.json
- The array in which to look up the Booleanindex
- The index at which to look up the Booleanpublic Boolean getBoolean(JSONObject json, String name)
JSONObject
at the provided 'tag', or thrown an
MBFormatException
.json
- The object in which to look up the Booleanname
- The tag at which to look up the Booleanpublic Boolean getBoolean(JSONObject json, String name, Boolean fallback)
JSONObject
at the provided 'tag', or thrown an
MBFormatException
, with a fallback if the json is null or contains no such 'tag'.json
- The object in which to look up the Booleanname
- The tag at which to look up the Booleanfallback
- The value to return if the json is null or contains no such 'tag'.public <T> T require(Class<T> type, JSONArray json, int index)
MBFormatException
if no object of that type is found at that index of the array.T
- Class to returntype
- The type of the object to retrieve.json
- The JSONArray in which to retrieve the object.index
- The index in the JSONArray at which to retrieve the object.public <T> T require(Class<T> type, JSONObject json, String name)
MBFormatException
if no object of that type is found at that tag in the object.T
- Class to returntype
- The type of the object to retrieve.json
- The JSONObject in which to retrieve the object.name
- The index in the JSONObject at which to retrieve the object.public <T> T optional(Class<T> type, JSONObject json, String name, T fallback)
T
- Class to returntype
- Type to lookupjson
- The JSONObject in which to lookup the valuename
- The tag at which to lookupthe value in the JSONObjectfallback
- The fallback value to use if the JSONObject is null or does not contain the
provided tagMBFormatException
- If value is found and is not the expected typepublic <T extends Enum<?>> T getEnum(JSONObject json, String name, Class<T> enumeration, T fallback)
T
- Enumeration type to returnjson
- The json object to parse the value fromname
- The key used to retrieve the value from the json.enumeration
- The enum to convert the value to.fallback
- default value if json is nullMBFormatException
- if the value is not a String, or it is not a valid value for the
enumeration.public <T extends Enum<?>> Expression enumToExpression(JSONObject json, String name, Class<T> enumeration, T fallback)
For example, converts LineMBLayer.LineJoin.BEVEL
to an expression that evaluates to the String
value "bevel", or LineMBLayer.LineJoin.MITER
to an expression that evaluates to "mitre".
json
- The json object containing the propertyname
- The json key corresponding to the propertyenumeration
- The Mapbox enumeration that the value should be an instance offallback
- The fallback enumeration value, if the value is missing or invalid for the
provided enumeration.public Literal constant(Object value, Class<? extends Enum<?>> enumeration)
Any conversion between mapbox constants and geotools constants will be done here.
value
- The value to be converted to the appropriate GeoTools literalenumeration
- The type of the mapbox enumerationpublic JSONObject jsonObject(Object obj) throws MBFormatException
MBFormatException
public JSONObject jsonObect(Object obj, String message) throws MBFormatException
obj
- The object to castmessage
- The message for the exception of the object is not a JSONObjectMBFormatException
public JSONArray jsonArray(Object obj) throws MBFormatException
MBFormatException
).obj
- The object to castMBFormatException
public JSONArray jsonArray(Object obj, String message) throws MBFormatException
MBFormatException
with the
provided message).obj
- The object to castmessage
- The message for the exception of the object is not a JSONArrayMBFormatException
public <T> T[] array(Class<T> type, JSONObject json, String name, T[] fallback)
type
- The type of the arrayjson
- The JSONObject in which to look up the arrayname
- The tag at which to look up the arrayfallback
- The fallback arraypublic int[] array(JSONObject json, String name, int[] fallback)
public double[] array(JSONObject json, String name, double[] fallback)
public Expression percentage(JSONObject json, String name) throws MBFormatException
json
- json representationMBFormatException
public Expression percentage(JSONObject json, String name, Number fallback) throws MBFormatException
json
- json representationname
- The tag at which to look up the percentagefallback
- default value if json is nullMBFormatException
public Expression font(JSONObject json, String name) throws MBFormatException
json
- The json context of the object, used for error messages.name
- Tag used to lookup font valueMBFormatException
public Expression number(JSONArray json, int index) throws MBFormatException
json
- The JSONArray in which to look up a valueindex
- The index in the JSONArrayMBFormatException
public Expression number(JSONArray json, int index, Number fallback) throws MBFormatException
json
- The JSONArray in which to look up the valueindex
- The index in the JSONArray at which to look up the valuefallback
- default value if json is nullMBFormatException
public Expression number(JSONObject json, String name) throws MBFormatException
json
- The JSONObject in which to look up the valuename
- The tag in the JSONObject at which to look up the valueMBFormatException
public Expression number(JSONObject json, String name, Number fallback) throws MBFormatException
json
- The JSONObject in which to look up the valuename
- The tag in the JSONObject at which to look up the valuefallback
- default value if the JSONObject is nullMBFormatException
public Expression string(JSONArray json, int index)
json
- The JSONArray in which to look up the valueindex
- The index in the JSONArray at which to look up the valuepublic Expression string(JSONObject json, String name, String fallback) throws MBFormatException
json
- json representationfallback
- default value if json is nullMBFormatException
public Expression color(JSONObject json, String name, Color fallback) throws MBFormatException
json
- json representationfallback
- default value (string representation of color) if json is nullMBFormatException
public Literal color(String color)
Literal
.
{"line-color": "yellow"named: a few have been put in pass test cases, prnding: plan to use
Hints.COLOR_DEFINITION
to allow for web colors.
{"line-color": "#ffff00"}hex: hex color conversion are supplied by
ColorConverterFactory
{"line-color": "#ff0"}hex: we will need to special case this
{"line-color": "rgb(255, 255, 0)"}- we will need to special case this
{"line-color": "rgba(255, 255, 0, 1)"}- we will need to special case this
{"line-color": "hsl(100, 50%, 50%)"}- we will need to special case this
{"line-color": "hsla(100, 50%, 50%, 1)"}- we will need to special case this
Hints.COLOR_DEFINITION
"CSS" to support the use of web colors names.color
- name of color (CSS or "web" colors)public Color convertToColor(String color)
{"line-color": "yellow"named: a few have been put in pass test cases, prnding: plan to use
Hints.COLOR_DEFINITION
to allow for web colors.
{"line-color": "#ffff00"}hex: hex color conversion are supplied by
ColorConverterFactory
{"line-color": "#ff0"}hex: we will need to special case this
{"line-color": "rgb(255, 255, 0)"}- we will need to special case this
{"line-color": "rgba(255, 255, 0, 1)"}- we will need to special case this
{"line-color": "hsl(100, 50%, 50%)"}- we will need to special case this
{"line-color": "hsla(100, 50%, 50%, 1)"}- we will need to special case this
Hints.COLOR_DEFINITION
"CSS" to support the use of web colors names.color
- name of color (CSS or "web" colors)public Expression bool(JSONObject json, String name, boolean fallback) throws MBFormatException
json
- json representationfallback
- default value if json is nullMBFormatException
public Displacement displacement(JSONObject json, String name, Displacement fallback)
Displacement
.json
- The JSONObject in which to look up a displacement valuename
- The tag in the JSONObjectfallback
- The fallback displacement, if no value is found at that tag.@Deprecated public boolean isPropertyDefined(JSONObject json, String propertyName) throws MBFormatException
MBFormatException
public boolean isDefined(JSONObject json, String name) throws MBFormatException
json
- JSON objectname
- Name used to look up a valueMBFormatException
public boolean isDefined(JSONArray json, int index) throws MBFormatException
json
- JSON arrayindex
- Index of array elementMBFormatException
public boolean isString(JSONObject json, String name) throws MBFormatException
json
- JSON objectname
- Name used to look up a valueMBFormatException
public boolean isString(JSONArray json, int index) throws MBFormatException
json
- JSON arrayindex
- Index of array elementMBFormatException
public boolean isNumeric(JSONObject json, String name) throws MBFormatException
json
- JSON objectname
- Name used to look up a valueMBFormatException
public boolean isNumeric(JSONArray json, int index) throws MBFormatException
json
- JSON arrayindex
- Index of array elementMBFormatException
public boolean isBoolean(JSONObject json, String name) throws MBFormatException
json
- JSON objectname
- Name used to look up a valueMBFormatException
public boolean isBoolean(JSONArray json, int index) throws MBFormatException
json
- JSON arrayindex
- Index of array elementMBFormatException
public boolean isArray(JSONObject json, String name) throws MBFormatException
json
- JSON objectname
- Name used to look up a valueMBFormatException
public boolean isArray(JSONArray json, int index) throws MBFormatException
json
- JSON arrayindex
- Index of array elementMBFormatException
public boolean isObject(JSONObject json, String name) throws MBFormatException
json
- JSON objectname
- Name used to look up a valueMBFormatException
public boolean isObject(JSONArray json, int index) throws MBFormatException
json
- JSON arrayindex
- Index of array elementMBFormatException
Copyright © 1996–2022 Geotools. All rights reserved.