// TODO(sbeutel): new map method to handle other key values than String public static <KeyType, ValueType> JSONValue toJSON( Map<KeyType, ValueType> value, AbstractJsonEncoderDecoder<KeyType> keyEncoder, AbstractJsonEncoderDecoder<ValueType> valueEncoder, Style style) { if (value == null) { return JSONNull.getInstance(); } switch (style) { case DEFAULT: case SIMPLE: { JSONObject rc = new JSONObject(); for (Entry<KeyType, ValueType> t : value.entrySet()) { // TODO find a way to check only once JSONValue k = keyEncoder.encode(t.getKey()); if (k.isString() != null) { rc.put(k.isString().stringValue(), valueEncoder.encode(t.getValue())); } else { rc.put(k.toString(), valueEncoder.encode(t.getValue())); } } return rc; } case JETTISON_NATURAL: { JSONObject rc = new JSONObject(); JSONArray entries = new JSONArray(); int i = 0; for (Entry<KeyType, ValueType> t : value.entrySet()) { JSONObject entry = new JSONObject(); // TODO find a way to check only once JSONValue k = keyEncoder.encode(t.getKey()); if (k.isString() != null) { entry.put("key", k); } else { entry.put("key", new JSONString(k.toString())); } entry.put("value", valueEncoder.encode(t.getValue())); entries.set(i++, entry); } rc.put("entry", entries); return rc; } default: throw new UnsupportedOperationException( "The encoding style is not yet supported: " + style.name()); } }
@Override public BigInteger decode(JSONValue value) throws DecodingException { if (value == null || value.isNull() != null) { return null; } JSONNumber number = value.isNumber(); if (number == null) { JSONString str = value.isString(); if (str == null) { throw new DecodingException( "Expected a json number r string, but was given: " + value); } // Doing a straight conversion from string to BigInteger will // not work for large values // So we convert to BigDecimal first and then convert it to // BigInteger. return new BigDecimal(str.stringValue()).toBigInteger(); } // Doing a straight conversion from string to BigInteger will not // work for large values // So we convert to BigDecimal first and then convert it to // BigInteger. return new BigDecimal(value.toString()).toBigInteger(); }
public static BigDecimal toBigDecimal(JSONValue value) { JSONNumber number = value.isNumber(); if (number == null) { throw new DecodingException("Expected a json number, but was given: " + value); } return new BigDecimal(value.toString()); }
public static List<MarshalledMessage> decodePayload(final String value) { if (value == null || value.trim().length() == 0) return Collections.emptyList(); /** * We have to do a two-stage decoding of the message. We cannot fully decode the message here, * as we cannot be sure the destination endpoint exists within this Errai bundle. So we extract * the ToSubject field and send the unparsed JSON object onwards. */ JSONValue val = null; try { val = JSONParser.parseStrict(value); } catch (ClassCastException e) { if (!GWT.isProdMode()) { System.out.println("*** working around devmode bug ***"); val = JSONParser.parseStrict(value); } } if (val == null) { return Collections.emptyList(); } final JSONArray arr = val.isArray(); if (arr == null) { throw new RuntimeException("unrecognized payload" + val.toString()); } final ArrayList<MarshalledMessage> list = new ArrayList<MarshalledMessage>(); unwrap(list, arr); return list; }
/** * Check if the json value is empty * * @param in json value to test * @return true if value is empty else returns false */ public static boolean isEmpty(JSONValue in) { boolean ret = true; // assume we have an empty value if (in != null) { String test = in.toString(); if (test.length() > 0 && !test.equals("[]") && !test.equals("{}")) { ret = false; } } return ret; }
@Override public String decode(JSONValue value) throws DecodingException { if (value == null || value.isNull() != null) { return null; } JSONString str = value.isString(); if (str == null) { if (value.isBoolean() != null || value.isNumber() != null) { return value.toString(); } throw new DecodingException("Expected a json string, but was given: " + value); } return str.stringValue(); }
@Override public UserDetails deserialize(String json) { JSONValue parsed = JSONParser.parseStrict(json); JSONObject jsonObj = parsed.isObject(); UserDetailsDTO user = new UserDetailsDTO(); if (jsonObj != null) { user.setUsername(jsonObj.get(UserDetailsDTO.USERNAME_FIELD).toString()); JSONArray array = jsonObj.get(UserDetailsDTO.AUTHORITIES_FIELD).isArray(); for (int i = 0; i < array.size(); i++) { JSONValue obj = array.get(i); user.getAuthorities().add(obj.toString()); } } return user; }
// This method assumes that the JSON value being passed is a JSON // non-primitive: // either an object or an array. // This helps to make this implementation as efficient as possible. protected String jsonNonPrimitiveToPrettyString(JSONValue jsonValue, int indentLevel) { StringBuffer buffer = new StringBuffer(); // If the value in question is an object JSONObject jsonValueObject = jsonValue.isObject(); if (jsonValueObject != null) { boolean firstKey = true; for (String key : jsonValueObject.keySet()) { if (firstKey) { firstKey = false; } else { buffer.append(",\n"); } for (int k = 0; k < indentLevel; ++k) { buffer.append(" "); } buffer.append(key).append(" : "); JSONValue jsonObjectValue = jsonValueObject.get(key); if (jsonObjectValue != null) { if (jsonObjectValue.isObject() != null) { buffer .append("{\n") .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1)) .append("}"); } else if (jsonObjectValue.isArray() != null) { buffer .append("[\n") .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1)) .append("]"); } else { // If the object value is a primitive, just prints it, // there is no need for a recursive call! buffer.append(jsonObjectValue.toString()); } } } } else if (jsonValue.isArray() != null) { // If the value in question is an array JSONArray jsonValueArray = jsonValue.isArray(); for (int i = 0; i < jsonValueArray.size(); ++i) { if (0 < i) { buffer.append(",\n"); } for (int k = 0; k < indentLevel; ++k) { buffer.append(" "); } JSONValue jsonArrayValue = jsonValueArray.get(i); if (jsonArrayValue != null) { if (jsonArrayValue.isObject() != null) { buffer .append("{\n") .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1)) .append("}"); } else if (jsonArrayValue.isArray() != null) { buffer .append("[\n") .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1)) .append("]"); } else { // If the object value is a primitive, just prints it, // there is no need for a recursive call! buffer.append(jsonArrayValue.toString()); } } } } return buffer.toString(); }
public Method json(JSONValue data) { defaultContentType(Resource.CONTENT_TYPE_JSON); builder.setRequestData(data.toString()); return this; }