@Test public void optional() throws ParseException { String jsonStr = "{'name1': 'fred', 'name2' : 1, 'name3' : null, 'name4': { 'age': 1 }}"; JSONObject object = MapboxTestUtils.object(jsonStr); assertEquals("fred", parser.optional(String.class, object, "name1", null)); assertNull(parser.optional(String.class, object, "name0", null)); assertEquals("sample", parser.optional(String.class, object, "name0", "sample")); assertNull(parser.optional(String.class, object, "name3", null)); assertEquals("sample", parser.optional(String.class, object, "name3", "sample")); try { assertEquals("1", parser.optional(String.class, object, "name2", null)); } catch (MBFormatException expected) { assertTrue(expected.toString().contains("name2")); } try { assertNull(parser.optional(String.class, object, "name4", null)); fail("Optional name4 lookup was object, rather than string"); } catch (MBFormatException expected) { assertTrue(expected.toString().contains("name4")); } try { assertEquals("sample", parser.optional(String.class, object, "name4", "sample")); fail("Optional name4 lookup was object, rather than string"); } catch (MBFormatException expected) { assertTrue(expected.toString().contains("name4")); } }
public <T> T optional(Class<T> type, JSONObject json, String name, T fallback) { if (json == null) { throw new IllegalArgumentException("json required"); } else if (name == null) { throw new IllegalArgumentException("name required for json object access"); } else if (!json.containsKey(name)) { return fallback; } else { Object value = json.get(name); if (!json.containsKey(name) || json.get(name) == null) { return fallback; } else if (Number.class.isAssignableFrom(type)) { T number = Converters.convert(value, type); if (number == null) { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } else { return type.cast(number); } } else if (type.isInstance(value)) { return type.cast(value); } else { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } } }
MBObjectParser { public <T> T optional(Class<T> type, JSONObject json, String name, T fallback) { if (json == null) { throw new IllegalArgumentException("json required"); } else if (name == null) { throw new IllegalArgumentException("name required for json object access"); } else if (!json.containsKey(name)) { return fallback; } else { Object value = json.get(name); if (!json.containsKey(name) || json.get(name) == null) { return fallback; } else if (Number.class.isAssignableFrom(type)) { T number = Converters.convert(value, type); if (number == null) { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } else { return type.cast(number); } } else if (type.isInstance(value)) { return type.cast(value); } else { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } } } }
MBObjectParser { public <T> T optional(Class<T> type, JSONObject json, String name, T fallback) { if (json == null) { throw new IllegalArgumentException("json required"); } else if (name == null) { throw new IllegalArgumentException("name required for json object access"); } else if (!json.containsKey(name)) { return fallback; } else { Object value = json.get(name); if (!json.containsKey(name) || json.get(name) == null) { return fallback; } else if (Number.class.isAssignableFrom(type)) { T number = Converters.convert(value, type); if (number == null) { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } else { return type.cast(number); } } else if (type.isInstance(value)) { return type.cast(value); } else { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } } } MBObjectParser(Class<?> context);  MBObjectParser(Class<MBFilter> context, MBObjectParser parse); }
MBObjectParser { public <T> T optional(Class<T> type, JSONObject json, String name, T fallback) { if (json == null) { throw new IllegalArgumentException("json required"); } else if (name == null) { throw new IllegalArgumentException("name required for json object access"); } else if (!json.containsKey(name)) { return fallback; } else { Object value = json.get(name); if (!json.containsKey(name) || json.get(name) == null) { return fallback; } else if (Number.class.isAssignableFrom(type)) { T number = Converters.convert(value, type); if (number == null) { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } else { return type.cast(number); } } else if (type.isInstance(value)) { return type.cast(value); } else { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } } } 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> T optional(Class<T> type, JSONObject json, String name, T fallback) { if (json == null) { throw new IllegalArgumentException("json required"); } else if (name == null) { throw new IllegalArgumentException("name required for json object access"); } else if (!json.containsKey(name)) { return fallback; } else { Object value = json.get(name); if (!json.containsKey(name) || json.get(name) == null) { return fallback; } else if (Number.class.isAssignableFrom(type)) { T number = Converters.convert(value, type); if (number == null) { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } else { return type.cast(number); } } else if (type.isInstance(value)) { return type.cast(value); } else { throw new MBFormatException( context.getSimpleName() + " optional \"" + name + "\" expects " + type.getSimpleName() + " value"); } } } 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);  }