private static void unwrap(final List<MarshalledMessage> messages, final JSONArray val) { for (int i = 0; i < val.size(); i++) { final JSONValue v = val.get(i); if (v.isArray() != null) { unwrap(messages, v.isArray()); } else { messages.add(new MarshalledMessageImpl((JSONObject) v)); } } }
static JSONArray asArray(JSONValue value) { JSONArray array = value.isArray(); if (array == null) { throw new DecodingException("Expected a json array, but was given: " + value); } return array; }
@Override protected void onInit(final JSONValue data) { try { final JSONArray criteria = data.isArray(); this.criteria.clear(); for (int i = 0; i < criteria.size(); i++) { final JSONObject searchTypeJSON = criteria.get(i).isObject(); final Map<String, Criterion> searchType = new LinkedHashMap<String, Criterion>(); for (int j = 0; j < searchTypeJSON.get("elements").isArray().size(); j++) { final Criterion criterion = Criterion.readMe(j, searchTypeJSON); if (j == 0 && (selectedCriterion == null && i == 0 || selectedCriterion != null && criterion.displaytype.equalsIgnoreCase(selectedCriterion.displaytype))) { selectedCriterion = criterion; selectedCriterionHistory.put(selectedCriterion.displaytype, selectedCriterion); } searchType.put(criterion.getName(), criterion); } this.criteria.put( searchTypeJSON.get("displaytype").isString().stringValue().trim(), searchType); } } catch (final Exception e) { getLogger().severe(getClass().getName().replace("Impl", "") + ".onInit() : " + e); } }
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; }
public static <KeyType, ValueType> Map<KeyType, ValueType> toMap( JSONValue value, AbstractJsonEncoderDecoder<KeyType> keyEncoder, AbstractJsonEncoderDecoder<ValueType> valueEncoder, Style style) { if (value == null || value.isNull() != null) { return null; } switch (style) { case DEFAULT: case SIMPLE: { JSONObject object = value.isObject(); if (object == null) { throw new DecodingException("Expected a json object, but was given: " + value); } HashMap<KeyType, ValueType> rc = new HashMap<KeyType, ValueType>(object.size() * 2); for (String key : object.keySet()) { rc.put(keyEncoder.decode(key), valueEncoder.decode(object.get(key))); } return rc; } case JETTISON_NATURAL: { JSONObject object = value.isObject(); if (object == null) { throw new DecodingException("Expected a json object, but was given: " + value); } value = object.get("entry"); if (value == null) { throw new DecodingException("Expected an entry array not found"); } JSONArray entries = value.isArray(); if (entries == null) { throw new DecodingException("Expected an entry array, but was given: " + value); } HashMap<KeyType, ValueType> rc = new HashMap<KeyType, ValueType>(object.size() * 2); for (int i = 0; i < entries.size(); i++) { JSONObject entry = entries.get(i).isObject(); if (entry == null) throw new DecodingException("Expected an entry object, but was given: " + value); JSONValue key = entry.get("key"); if (key == null) throw new DecodingException("Expected an entry key field not found"); JSONString k = key.isString(); if (k == null) throw new DecodingException( "Expected an entry key to be a string, but was given: " + value); rc.put(keyEncoder.decode(k.stringValue()), valueEncoder.decode(entry.get("value"))); } return rc; } default: throw new UnsupportedOperationException( "The encoding style is not yet supported: " + style.name()); } }
public static JSONArray parseJSONArray(String jsonText) { if (jsonText != null && 0 < jsonText.length()) { JSONValue jsonValue = JSONParser.parse(jsonText); if (jsonValue != null) { return jsonValue.isArray(); } } return null; }
/** * 静态函数:根据输入的Json字符串,解析成一个完整的菜单树 * * @param menuStr Json字符串 * @return 菜单树形根节点 */ public static GWTMenu GetMenuList(String menuStr) { // 解析菜单 JSONValue obj1 = JSONParser.parse(menuStr); JSONArray menuList = obj1.isArray(); GWTMenu menu = new GWTMenu(); for (int i = 0; i < menuList.size(); i++) { AddGWTMenu(menu, menuList.get(i)); } return menu; }
/** * @param jsonObj * @param key * @return */ public static JSONArray getArray(final JSONObject jsonObj, final String key) { JSONArray ret = null; // assume failure if (jsonObj != null && key != null) { JSONValue val = jsonObj.get(key); if (val != null) { ret = val.isArray(); } } return ret; }
private void addProperties(Response response) { GWT.log(response.getText(), null); try { // parse the response text into JSON JSONValue jsonValue = JSONParser.parse(response.getText()); JSONArray jsonArray = jsonValue.isArray(); if (jsonArray != null) { for (int index = 0; index < jsonArray.size(); index++) { addProperty(((JSONObject) jsonArray.get(index)), index); } } else { throw new JSONException("Invalid Json structure when retrieve the Sling nodes"); } } catch (JSONException e) { e.printStackTrace(); GWT.log("ResourceGrids - Could not parse JSON", e); } }
public static JSONArray jsonValueToArray(JSONValue jsonValue) { if (jsonValue != null) { return jsonValue.isArray(); } return null; }
// 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(); }