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()); } }
/** * tagValueグループに含まれるtagをリストとして取得する * * @param message * @return */ public ArrayList<String> getTags(String message) { JSONObject obj = JSONParser.parseStrict(message).isObject().get(KEY_MESSENGER_TAGVALUE_GROUP).isObject(); ArrayList<String> tags = new ArrayList<String>(); Set<String> currentSet = obj.keySet(); for (Iterator<String> currentSetItel = currentSet.iterator(); currentSetItel.hasNext(); ) { tags.add(currentSetItel.next()); } return tags; }
/** * tagValueグループに含まれるvalueをリストとして取得する * * @param message * @return */ public ArrayList<JSONValue> getValues(String message) { JSONObject obj = JSONParser.parseStrict(message).isObject().get(KEY_MESSENGER_TAGVALUE_GROUP).isObject(); ArrayList<JSONValue> values = new ArrayList<JSONValue>(); Set<String> currentSet = obj.keySet(); for (Iterator<String> currentSetItel = currentSet.iterator(); currentSetItel.hasNext(); ) { String currentKey = currentSetItel.next(); values.add(obj.get(currentKey)); } return values; }
// 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(); }
private void fromJson(final JSONValue data) { try { if (null != data) { restoreddata = data.isObject(); final String version = restoreddata.get("historyversion").isString().stringValue(); if (!version.equals(SearchPanelModel.historyversion)) { throw new Exception("versionstring of search history has changed, need to reset history"); } final String selectedtype = restoreddata.get("selectedtype").isString().stringValue(); final Iterator<String> iter = criteria.keySet().iterator(); while (iter.hasNext()) { final String searchTypeDisplayKey = iter.next(); if (!restoreddata.containsKey( criteria.get(searchTypeDisplayKey).values().iterator().next().type)) { searchhistory.put(searchTypeDisplayKey, new SearchHistory()); searchhistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.SEARCH_HISTORY_LIMIT); accesshistory.put(searchTypeDisplayKey, new AccessHistory()); accesshistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.ACCESS_HISTORY_LIMIT); continue; } final String type = criteria.get(searchTypeDisplayKey).values().iterator().next().type; final JSONObject searchdata = restoreddata.get(type.toLowerCase()).isObject(); final JSONObject userdata = searchdata.get(Context.get().getChallengeLogin().toLowerCase()).isObject(); final Criterion criterion = criteria .get(searchTypeDisplayKey) .get(userdata.get("selectedcriterion").isString().stringValue()); if (criterion.displaytype.equalsIgnoreCase(selectedtype)) { selectedCriterion = criterion; } selectedCriterionHistory.put(criterion.displaytype, criterion); final JSONObject criteriadata = userdata.get("criteria").isObject(); for (final String key : criteriadata.keySet()) { criteria .get(criterion.displaytype) .get(key) .setHistory(criteriadata.get(key).isObject()); } if (userdata.containsKey("searchhistory")) { searchhistory.put( searchTypeDisplayKey, SearchHistory.Reader.read(userdata.get("searchhistory").isObject())); } else { searchhistory.put(searchTypeDisplayKey, new SearchHistory()); searchhistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.SEARCH_HISTORY_LIMIT); } if (userdata.containsKey("accesshistory")) { accesshistory.put( searchTypeDisplayKey, AccessHistory.Reader.read(userdata.get("accesshistory").isObject())); } else { accesshistory.put(searchTypeDisplayKey, new AccessHistory()); accesshistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.ACCESS_HISTORY_LIMIT); } } } } catch (final Exception e) { getLogger().severe("SearchModel.fromJson() : " + e); reset(); } }
/** * 送信メッセージ構造を構築する * * <p>KEY_MESSENGER_NAME:送信者名 KEY_MESSENGER_ID:送信者ID KEY_TO_NAME:送信先 KEY_MESSENGER_EXEC:実行コマンド * KEY_MESSENGER_TAGVALUE_GROUP:タグとバリューのグループ * * @param receiverName * @param command * @param tagValue * @return */ public JSONObject getMessageStructure( int messageCategoly, String messageID, String senderName, String senderID, String receiverName, String receiverID, String command, JSONObject... tagValue) { // JSONObject[] tagValue JSONObject messageMap = new JSONObject(); messageMap.put(KEY_MESSENGER_NAME, new JSONString(senderName)); messageMap.put(KEY_MESSENGER_ID, new JSONString(senderID)); messageMap.put(KEY_MESSAGE_ID, new JSONString(messageID)); messageMap.put(KEY_MESSAGE_CATEGOLY, new JSONNumber(messageCategoly)); switch (messageCategoly) { case MS_CATEGOLY_LOCAL: messageMap.put(KEY_TO_NAME, new JSONString(receiverName)); messageMap.put(KEY_MESSENGER_EXEC, new JSONString(command)); messageMap.put(KEY_TO_ID, new JSONString(senderID)); break; case MS_CATEGOLY_CALLCHILD: messageMap.put(KEY_TO_NAME, new JSONString(receiverName)); messageMap.put(KEY_TO_ID, new JSONString(receiverID)); messageMap.put(KEY_MESSENGER_EXEC, new JSONString(command)); break; case MS_CATEGOLY_CALLPARENT: messageMap.put(KEY_TO_NAME, new JSONString(receiverName)); messageMap.put(KEY_TO_ID, new JSONString(receiverID)); messageMap.put(KEY_MESSENGER_EXEC, new JSONString(command)); break; case MS_CATEGOLY_PARENTSEARCH: case MS_CATEGOLY_PARENTSEARCH_S: messageMap.put(KEY_TO_NAME, new JSONString(receiverName)); messageMap.put(KEY_TO_ID, new JSONString(receiverID)); messageMap.put(KEY_MESSENGER_EXEC, new JSONString(command)); messageMap.put(KEY_PARENT_NAME, new JSONString(receiverName)); break; case MS_CATEGOLY_PARENTSEARCH_RET: messageMap.put(KEY_PARENT_NAME, new JSONString(getName())); messageMap.put(KEY_PARENT_ID, new JSONString(getID())); messageMap.put(KEY_TO_NAME, new JSONString(receiverName)); messageMap.put(KEY_TO_ID, new JSONString(receiverID)); messageMap.put(KEY_MESSENGER_EXEC, new JSONString(command)); break; case MS_CATEGOLY_REMOVE_CHILD: case MS_CATEGOLY_REMOVE_PARENT: default: debug.assertTrue(false, "not ready yet"); break; } JSONObject tagValueGroup = new JSONObject(); for (JSONObject currentObject : tagValue) { for (Iterator<String> currentItel = currentObject.keySet().iterator(); currentItel.hasNext(); ) { String currentKey = currentItel.next(); tagValueGroup.put(currentKey, currentObject.get(currentKey)); // オブジェクトの移し替え } } messageMap.put(KEY_MESSENGER_TAGVALUE_GROUP, tagValueGroup); // debug.trace("messageMap_"+messageMap);//しばらくin-outのテスト用にとっておこう。 return messageMap; }