@Override public void toJSONString(JSONStringer stringer) throws JSONException { super.toJSONString(stringer); stringer.key(Members.KEY_ITERATE.name()).value(m_keyIterate); stringer.key(Members.LOOKUP_TYPE.name()).value(m_lookupType.toString()); stringer.key(Members.SORT_DIRECTION.name()).value(m_sortDirection.toString()); stringer.key(Members.TARGET_INDEX_NAME.name()).value(m_targetIndexName); stringer.key(Members.END_EXPRESSION.name()); stringer.value(m_endExpression); stringer.key(Members.SEARCHKEY_EXPRESSIONS.name()).array(); for (AbstractExpression ae : m_searchkeyExpressions) { assert (ae instanceof JSONString); stringer.value(ae); } stringer.endArray(); }
public JSONWrapper(Object value, JSONCompareMode mode) throws IllegalArgumentException { if (value instanceof String) { try { JSONParser.parseJSON((String) value); } catch (JSONException e) { throw new IllegalArgumentException(e); } } else if (!(value instanceof JSONArray) && !(value instanceof JSONObject) && !(value instanceof JSONString)) { throw new IllegalArgumentException("Acceptable values for JSONWrapper are JSONArray/JSONObject/JSONString/String, got(" + value.getClass() + ")::" + value); } this.value = value; this.mode = mode; }
public void toJSONString(JSONStringer stringer) throws JSONException { stringer.key(Members.PLAN_NODES.name()).array(); for (AbstractPlanNode node : m_planNodes) { assert (node instanceof JSONString); stringer.value(node); } stringer.endArray(); // end entries stringer.key(Members.PARAMETERS.name()).array(); for (Pair<Integer, VoltType> parameter : m_parameters) { stringer.array().value(parameter.getFirst()).value(parameter.getSecond().name()).endArray(); } stringer.endArray(); }
public void toJSONString(JSONStringer stringer) throws JSONException { stringer.key(Members.TYPE.name()).value(m_type.toString()); stringer.key(Members.VALUE_TYPE.name()).value(m_valueType == null ? null : m_valueType.name()); stringer.key(Members.VALUE_SIZE.name()).value(m_valueSize); if (m_left != null) { assert (m_left instanceof JSONString); stringer.key(Members.LEFT.name()).value(m_left); } if (m_right != null) { assert (m_right instanceof JSONString); stringer.key(Members.RIGHT.name()).value(m_right); } }
@Override public void toJSONString(JSONStringer stringer) throws JSONException { super.toJSONString(stringer); stringer.key(Members.VALUES.name()).array(); for (AbstractExpression expr : m_values) { assert (expr instanceof JSONString); stringer.value(expr); } stringer.endArray(); }
private String valueToString(Object value, boolean escapeToUnicode) throws JSONException { if (value == null || value.equals(null)) { return "null"; } if (value instanceof JSONString) { Object object; try { object = ((JSONString)value).toJSONString(); } catch (Exception e) { throw new JSONException(e); } if (object instanceof String) { return (String)object; } throw new JSONException("Bad value from toJSONString: " + object); } if (value instanceof Number) { return JSONObject.numberToString((Number) value); } if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) { return value.toString(); } if (value instanceof Map) { return new JSONObject((Map)value).toString(); } if (value instanceof Collection) { return new JSONArray((Collection)value).toString(); } if (value.getClass().isArray()) { return new JSONArray(value).toString(); } return quote(value.toString(), escapeToUnicode); }
/** * Make a JSON text of an Object value. If the object has an * value.toJSONString() method, then that method will be used to produce * the JSON text. The method is required to produce a strictly * conforming text. If the object does not contain a toJSONString * method (which is the most common case), then a text will be * produced by other means. If the value is an array or Collection, * then a JSONArray will be made from it and its toJSONString method * will be called. If the value is a MAP, then a JSONObject will be made * from it and its toJSONString method will be called. Otherwise, the * value's toString method will be called, and the result will be quoted. * * <p> * Warning: This method assumes that the data structure is acyclical. * @param value The value to be serialized. * @return a printable, displayable, transmittable * representation of the object, beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @throws JSONException If the value is or contains an invalid number. */ public static String valueToString(Object value) throws JSONException { if (value == null || value.equals(null)) { return "null"; } if (value instanceof JSONString) { Object object; try { object = ((JSONString)value).toJSONString(); } catch (Exception e) { throw new JSONException(e); } if (object instanceof String) { return (String)object; } throw new JSONException("Bad value from toJSONString: " + object); } if (value instanceof Number) { return numberToString((Number) value); } if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) { return value.toString(); } if (value instanceof Map) { return new JSONObject((Map)value).toString(); } if (value instanceof Collection) { return new JSONArray((Collection)value).toString(); } if (value.getClass().isArray()) { return new JSONArray(value).toString(); } return quote(value.toString()); }
/** * Make a JSON text of an Object value. If the object has an * value.toJSONString() method, then that method will be used to produce * the JSON text. The method is required to produce a strictly * conforming text. If the object does not contain a toJSONString * method (which is the most common case), then a text will be * produced by other means. If the value is an array or Collection, * then a JSONArray will be made from it and its toJSONString method * will be called. If the value is a MAP, then a JSONObject will be made * from it and its toJSONString method will be called. Otherwise, the * value's toString method will be called, and the result will be quoted. * * <p> * Warning: This method assumes that the data structure is acyclical. * @param value The value to be serialized. * @return a printable, displayable, transmittable * representation of the object, beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @throws JSONException If the value is or contains an invalid number. */ static String valueToString(Object value) throws JSONException { if (value == null || value.equals(null)) { return "null"; } if (value instanceof JSONString) { Object o; try { o = ((JSONString)value).toJSONString(); } catch (Exception e) { throw new JSONException(e); } if (o instanceof String) { return (String)o; } throw new JSONException("Bad value from toJSONString: " + o); } if (value instanceof Number) { return numberToString((Number) value); } if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) { return value.toString(); } if (value instanceof Map) { return new JSONObject((Map)value).toString(); } if (value instanceof Collection) { return new JSONArray((Collection)value).toString(); } if (value.getClass().isArray()) { return new JSONArray(value).toString(); } return quote(value.toString()); }
/** * Make a prettyprinted JSON text of an object value. * <p> * Warning: This method assumes that the data structure is acyclical. * @param value The value to be serialized. * @param indentFactor The number of spaces to add to each level of * indentation. * @param indent The indentation of the top level. * @return a printable, displayable, transmittable * representation of the object, beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @throws JSONException If the object contains an invalid number. */ static String valueToString(Object value, int indentFactor, int indent) throws JSONException { if (value == null || value.equals(null)) { return "null"; } try { if (value instanceof JSONString) { Object o = ((JSONString)value).toJSONString(); if (o instanceof String) { return (String)o; } } } catch (Exception e) { /* forget about it */ } if (value instanceof Number) { return numberToString((Number) value); } if (value instanceof Boolean) { return value.toString(); } if (value instanceof JSONObject) { return ((JSONObject)value).toString(indentFactor, indent); } if (value instanceof JSONArray) { return ((JSONArray)value).toString(indentFactor, indent); } if (value instanceof Map) { return new JSONObject((Map)value).toString(indentFactor, indent); } if (value instanceof Collection) { return new JSONArray((Collection)value).toString(indentFactor, indent); } if (value.getClass().isArray()) { return new JSONArray(value).toString(indentFactor, indent); } return quote(value.toString()); }
private GQLValue convert(Object object) { if (object == null || JSONObject.NULL.equals(object)) { return null; } else if (object instanceof JSONObject) { return convert((JSONObject) object); } else if (object instanceof JSONString) { return GQLValues.stringValue(object.toString()); } else if (object instanceof JSONArray) { return convert((JSONArray) object); } else if (object instanceof String) { return GQLValues.stringValue((String) object); } else if (object instanceof Integer || object instanceof Integer) { return GQLValues.intValue((int)object); } else if (object instanceof Long) { return GQLValues.intValue((long)object); } else if (object instanceof Double) { return GQLValues.floatValue((double)object); } else if (object instanceof Float) { return GQLValues.floatValue((float)object); } else { throw new RuntimeException("Unable to convert JSON " + object.getClass()); } }
/** * Make a prettyprinted JSON text of an object value. * <p> * Warning: This method assumes that the data structure is acyclical. * @param value The value to be serialized. * @param indentFactor The number of spaces to add to each level of * indentation. * @param indent The indentation of the top level. * @return a printable, displayable, transmittable * representation of the object, beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @throws JSONException If the object contains an invalid number. */ static String valueToString( Object value, int indentFactor, int indent ) throws JSONException { if (value == null || value.equals(null)) { return "null"; } try { if (value instanceof JSONString) { Object o = ((JSONString)value).toJSONString(); if (o instanceof String) { return (String)o; } } } catch (Exception ignore) { } if (value instanceof Number) { return numberToString((Number) value); } if (value instanceof Boolean) { return value.toString(); } if (value instanceof JSONObject) { return ((JSONObject)value).toString(indentFactor, indent); } if (value instanceof JSONArray) { return ((JSONArray)value).toString(indentFactor, indent); } if (value instanceof Map) { return new JSONObject((Map)value).toString(indentFactor, indent); } if (value instanceof Collection) { return new JSONArray((Collection)value).toString(indentFactor, indent); } if (value.getClass().isArray()) { return new JSONArray(value).toString(indentFactor, indent); } return quote(value.toString()); }
/** * Wrap an object, if necessary. If the object is null, return the NULL * object. If it is an array or collection, wrap it in a JSONArray. If * it is a map, wrap it in a JSONObject. If it is a standard property * (Double, String, et al) then it is already wrapped. Otherwise, if it * comes from one of the java packages, turn it into a string. And if * it doesn't, try to wrap it in a JSONObject. If the wrapping fails, * then null is returned. * * @param object The object to wrap * @return The wrapped value */ public static Object wrap(Object object) { try { if (object == null) { return NULL; } if (object instanceof JSONObject || object instanceof JSONArray || NULL.equals(object) || object instanceof JSONString || object instanceof Byte || object instanceof Character || object instanceof Short || object instanceof Integer || object instanceof Long || object instanceof Boolean || object instanceof Float || object instanceof Double || object instanceof String) { return object; } if (object instanceof Collection) { return new JSONArray((Collection)object); } if (object.getClass().isArray()) { return new JSONArray(object); } if (object instanceof Map) { return new JSONObject((Map)object); } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null ? objectPackage.getName() : ""; if ( objectPackageName.startsWith("java.") || objectPackageName.startsWith("javax.") || object.getClass().getClassLoader() == null ) { return object.toString(); } return new JSONObject(object); } catch(Exception exception) { return null; } }