@Test public void testEnumToExpression() throws ParseException { JSONObject object = MapboxTestUtils.object("{'text-pitch-alignment': 'map'}"); Expression expression = parser.enumToExpression( object, "text-pitch-alignment", SymbolMBLayer.Alignment.class, SymbolMBLayer.Alignment.AUTO); assertEquals("Enum correctly parsed", "map", expression.evaluate(null, String.class)); object = MapboxTestUtils.object("{'line-cap': 'round'}"); expression = parser.enumToExpression( object, "line-cap", LineMBLayer.LineCap.class, LineMBLayer.LineCap.BUTT); assertEquals("round", expression.evaluate(null, String.class)); object = MapboxTestUtils.object("{'line-join': 'bevel'}"); expression = parser.enumToExpression( object, "line-join", LineMBLayer.LineJoin.class, LineMBLayer.LineJoin.MITER); assertEquals("bevel", expression.evaluate(null, String.class)); }
public <T extends Enum<?>> Expression enumToExpression( JSONObject json, String name, Class<T> enumeration, T fallback) { Object value = json.get(name); if (value == null) { return constant(fallback.toString(), enumeration); } else if (value instanceof String) { String stringVal = (String) value; if ("".equals(stringVal.trim())) { return constant(fallback.toString(), enumeration); } try { return constant(stringVal, enumeration); } catch (Exception e) { LOGGER.log( Level.WARNING, "\"" + stringVal + "\" Exception parsing value \"" + stringVal + "\" for enumeration " + enumeration.getSimpleName() + ", falling back to default value."); return constant(fallback.toString(), enumeration); } } else if (value instanceof JSONObject) { MBFunction function = new MBFunction(this, (JSONObject) value); return function.enumeration(enumeration); } else { throw new MBFormatException( "Conversion of \"" + name + "\" value from " + value.getClass().getSimpleName() + " to " + enumeration.getSimpleName() + " not supported."); } }
MBObjectParser { public <T extends Enum<?>> Expression enumToExpression( JSONObject json, String name, Class<T> enumeration, T fallback) { Object value = json.get(name); if (value == null) { return constant(fallback.toString(), enumeration); } else if (value instanceof String) { String stringVal = (String) value; if ("".equals(stringVal.trim())) { return constant(fallback.toString(), enumeration); } try { return constant(stringVal, enumeration); } catch (Exception e) { LOGGER.log( Level.WARNING, "\"" + stringVal + "\" Exception parsing value \"" + stringVal + "\" for enumeration " + enumeration.getSimpleName() + ", falling back to default value."); return constant(fallback.toString(), enumeration); } } else if (value instanceof JSONObject) { MBFunction function = new MBFunction(this, (JSONObject) value); return function.enumeration(enumeration); } else { throw new MBFormatException( "Conversion of \"" + name + "\" value from " + value.getClass().getSimpleName() + " to " + enumeration.getSimpleName() + " not supported."); } } }
MBObjectParser { public <T extends Enum<?>> Expression enumToExpression( JSONObject json, String name, Class<T> enumeration, T fallback) { Object value = json.get(name); if (value == null) { return constant(fallback.toString(), enumeration); } else if (value instanceof String) { String stringVal = (String) value; if ("".equals(stringVal.trim())) { return constant(fallback.toString(), enumeration); } try { return constant(stringVal, enumeration); } catch (Exception e) { LOGGER.log( Level.WARNING, "\"" + stringVal + "\" Exception parsing value \"" + stringVal + "\" for enumeration " + enumeration.getSimpleName() + ", falling back to default value."); return constant(fallback.toString(), enumeration); } } else if (value instanceof JSONObject) { MBFunction function = new MBFunction(this, (JSONObject) value); return function.enumeration(enumeration); } else { throw new MBFormatException( "Conversion of \"" + name + "\" value from " + value.getClass().getSimpleName() + " to " + enumeration.getSimpleName() + " not supported."); } } MBObjectParser(Class<?> context);  MBObjectParser(Class<MBFilter> context, MBObjectParser parse); }
MBObjectParser { public <T extends Enum<?>> Expression enumToExpression( JSONObject json, String name, Class<T> enumeration, T fallback) { Object value = json.get(name); if (value == null) { return constant(fallback.toString(), enumeration); } else if (value instanceof String) { String stringVal = (String) value; if ("".equals(stringVal.trim())) { return constant(fallback.toString(), enumeration); } try { return constant(stringVal, enumeration); } catch (Exception e) { LOGGER.log( Level.WARNING, "\"" + stringVal + "\" Exception parsing value \"" + stringVal + "\" for enumeration " + enumeration.getSimpleName() + ", falling back to default value."); return constant(fallback.toString(), enumeration); } } else if (value instanceof JSONObject) { MBFunction function = new MBFunction(this, (JSONObject) value); return function.enumeration(enumeration); } else { throw new MBFormatException( "Conversion of \"" + name + "\" value from " + value.getClass().getSimpleName() + " to " + enumeration.getSimpleName() + " not supported."); } } MBObjectParser(Class<?> context);  MBObjectParser(Class<MBFilter> context, MBObjectParser parse); FilterFactory2 getFilterFactory(); StyleFactory2 getStyleFactory(); JSONObject paint(JSONObject layer); JSONObject layout(JSONObject layer); JSONObject getJSONObject(JSONObject json, String name); JSONObject getJSONObject(JSONObject json, String name, JSONObject fallback); JSONArray getJSONArray(JSONObject json, String name); JSONArray getJSONArray(JSONObject json, String name, JSONArray fallback); Object value(JSONArray json, int index); Object value(JSONObject json, String name); String get(JSONArray json, int index); String get(JSONObject json, String name); String get(JSONObject json, String name, String fallback); double getNumeric(JSONArray json, int index); Double getNumeric(JSONObject json, String name); Boolean getBoolean(JSONArray json, int index); Boolean getBoolean(JSONObject json, String name); Boolean getBoolean(JSONObject json, String name, Boolean fallback); T require(Class<T> type, JSONArray json, int index); T require(Class<T> type, JSONObject json, String name); T optional(Class<T> type, JSONObject json, String name, T fallback); T getEnum(
            JSONObject json, String name, Class<T> enumeration, T fallback); Expression enumToExpression(
            JSONObject json, String name, Class<T> enumeration, T fallback); Literal constant(Object value, Class<? extends Enum<?>> enumeration); JSONObject jsonObject(Object obj); JSONObject jsonObect(Object obj, String message); JSONArray jsonArray(Object obj); JSONArray jsonArray(Object obj, String message); @SuppressWarnings("unchecked") T[] array(Class<T> type, JSONObject json, String name, T[] fallback); int[] array(JSONObject json, String name, int[] fallback); double[] array(JSONObject json, String name, double[] fallback); Expression percentage(JSONObject json, String name); Expression percentage(JSONObject json, String name, Number fallback); Expression font(JSONObject json, String name); Expression number(JSONArray json, int index); Expression number(JSONArray json, int index, Number fallback); Expression number(JSONObject json, String name); Expression number(JSONObject json, String name, Number fallback); Expression string(JSONArray json, int index); Expression string(JSONObject json, String name, String fallback); Expression color(JSONObject json, String name, Color fallback); Literal color(String color); Color convertToColor(String color); Expression bool(JSONObject json, String name, boolean fallback); Displacement displacement(JSONObject json, String name, Displacement fallback); @Deprecated boolean isPropertyDefined(JSONObject json, String propertyName); boolean isDefined(JSONObject json, String name); boolean isDefined(JSONArray json, int index); boolean isString(JSONObject json, String name); boolean isString(JSONArray json, int index); boolean isNumeric(JSONObject json, String name); boolean isNumeric(JSONArray json, int index); boolean isBoolean(JSONObject json, String name); boolean isBoolean(JSONArray json, int index); boolean isArray(JSONObject json, String name); boolean isArray(JSONArray json, int index); boolean isObject(JSONObject json, String name); boolean isObject(JSONArray json, int index); }
MBObjectParser { public <T extends Enum<?>> Expression enumToExpression( JSONObject json, String name, Class<T> enumeration, T fallback) { Object value = json.get(name); if (value == null) { return constant(fallback.toString(), enumeration); } else if (value instanceof String) { String stringVal = (String) value; if ("".equals(stringVal.trim())) { return constant(fallback.toString(), enumeration); } try { return constant(stringVal, enumeration); } catch (Exception e) { LOGGER.log( Level.WARNING, "\"" + stringVal + "\" Exception parsing value \"" + stringVal + "\" for enumeration " + enumeration.getSimpleName() + ", falling back to default value."); return constant(fallback.toString(), enumeration); } } else if (value instanceof JSONObject) { MBFunction function = new MBFunction(this, (JSONObject) value); return function.enumeration(enumeration); } else { throw new MBFormatException( "Conversion of \"" + name + "\" value from " + value.getClass().getSimpleName() + " to " + enumeration.getSimpleName() + " not supported."); } } MBObjectParser(Class<?> context);  MBObjectParser(Class<MBFilter> context, MBObjectParser parse); FilterFactory2 getFilterFactory(); StyleFactory2 getStyleFactory(); JSONObject paint(JSONObject layer); JSONObject layout(JSONObject layer); JSONObject getJSONObject(JSONObject json, String name); JSONObject getJSONObject(JSONObject json, String name, JSONObject fallback); JSONArray getJSONArray(JSONObject json, String name); JSONArray getJSONArray(JSONObject json, String name, JSONArray fallback); Object value(JSONArray json, int index); Object value(JSONObject json, String name); String get(JSONArray json, int index); String get(JSONObject json, String name); String get(JSONObject json, String name, String fallback); double getNumeric(JSONArray json, int index); Double getNumeric(JSONObject json, String name); Boolean getBoolean(JSONArray json, int index); Boolean getBoolean(JSONObject json, String name); Boolean getBoolean(JSONObject json, String name, Boolean fallback); T require(Class<T> type, JSONArray json, int index); T require(Class<T> type, JSONObject json, String name); T optional(Class<T> type, JSONObject json, String name, T fallback); T getEnum(
            JSONObject json, String name, Class<T> enumeration, T fallback); Expression enumToExpression(
            JSONObject json, String name, Class<T> enumeration, T fallback); Literal constant(Object value, Class<? extends Enum<?>> enumeration); JSONObject jsonObject(Object obj); JSONObject jsonObect(Object obj, String message); JSONArray jsonArray(Object obj); JSONArray jsonArray(Object obj, String message); @SuppressWarnings("unchecked") T[] array(Class<T> type, JSONObject json, String name, T[] fallback); int[] array(JSONObject json, String name, int[] fallback); double[] array(JSONObject json, String name, double[] fallback); Expression percentage(JSONObject json, String name); Expression percentage(JSONObject json, String name, Number fallback); Expression font(JSONObject json, String name); Expression number(JSONArray json, int index); Expression number(JSONArray json, int index, Number fallback); Expression number(JSONObject json, String name); Expression number(JSONObject json, String name, Number fallback); Expression string(JSONArray json, int index); Expression string(JSONObject json, String name, String fallback); Expression color(JSONObject json, String name, Color fallback); Literal color(String color); Color convertToColor(String color); Expression bool(JSONObject json, String name, boolean fallback); Displacement displacement(JSONObject json, String name, Displacement fallback); @Deprecated boolean isPropertyDefined(JSONObject json, String propertyName); boolean isDefined(JSONObject json, String name); boolean isDefined(JSONArray json, int index); boolean isString(JSONObject json, String name); boolean isString(JSONArray json, int index); boolean isNumeric(JSONObject json, String name); boolean isNumeric(JSONArray json, int index); boolean isBoolean(JSONObject json, String name); boolean isBoolean(JSONArray json, int index); boolean isArray(JSONObject json, String name); boolean isArray(JSONArray json, int index); boolean isObject(JSONObject json, String name); boolean isObject(JSONArray json, int index);  }