Java Code Examples for org.json.JSONStringer#object()
The following examples show how to use
org.json.JSONStringer#object() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testMaxDepthWithObjectValue() throws JSONException { JSONObject object = new JSONObject(); object.put("a", false); JSONStringer stringer = new JSONStringer(); for (int i = 0; i < 20; i++) { stringer.object(); stringer.key("b"); } stringer.value(object); for (int i = 0; i < 20; i++) { stringer.endObject(); } assertEquals("{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":" + "{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":{\"b\":" + "{\"a\":false}}}}}}}}}}}}}}}}}}}}}", stringer.toString()); }
Example 2
Source File: Utils.java From yawl with GNU Lesser General Public License v3.0 | 6 votes |
/** * serialize String keys and String values in JSON format, sort keys * according to order in sortedList * * @param map * @return */ public static String getJSON(List<String> sortedList, Map<String, Object> map) throws Exception { try { JSONStringer js = new JSONStringer(); js.object(); if (map != null) { for (String key : sortedList) { js.key(key); js.value(map.get(key)); } } js.endObject(); return js.toString(); } catch (JSONException e) { throw new Exception("cannot produce JSON", e); } }
Example 3
Source File: PreUtils.java From Hook with Apache License 2.0 | 6 votes |
public static void putMap(String key, Map<Integer, String> age2nameMap) { if (age2nameMap != null) { JSONStringer jsonStringer = new JSONStringer(); try { jsonStringer.array(); for (Integer integer : age2nameMap.keySet()) { jsonStringer.object(); jsonStringer.key("age"); jsonStringer.value(integer); jsonStringer.key("name"); jsonStringer.value(age2nameMap.get(integer)); jsonStringer.endObject(); } jsonStringer.endArray(); } catch (JSONException e) { e.printStackTrace(); } sp.edit().putString(key, jsonStringer.toString()).commit(); } }
Example 4
Source File: JSONUtils.java From orion.server with Eclipse Public License 1.0 | 5 votes |
public static String buildJSON(Map<String, Object> params) throws JSONException { final JSONStringer stringer = new JSONStringer(); stringer.object(); for (Map.Entry<String, Object> param : params.entrySet()) { if (param.getKey() != null && !"".equals(param.getKey()) && param.getValue() != null && !"" .equals(param.getValue())) { stringer.key(param.getKey()).value(param.getValue()); } } return stringer.endObject().toString(); }
Example 5
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testMultipleRoots() throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.array(); stringer.endArray(); try { stringer.object(); fail(); } catch (JSONException e) { } }
Example 6
Source File: JsonEncoder.java From egads with GNU General Public License v3.0 | 5 votes |
public static void // modifies json_out toJson(Object object, JSONStringer json_out) throws Exception { json_out.object(); // for each inherited class... for (Class c = object.getClass(); c != Object.class; c = c .getSuperclass()) { // for each member variable... Field[] fields = c.getDeclaredFields(); for (Field f : fields) { // if variable is static/private... skip it if (Modifier.isStatic(f.getModifiers())) { continue; } if (Modifier.isPrivate(f.getModifiers())) { continue; } Object value = f.get(object); // if variable is a complex type... recurse on sub-objects if (value instanceof JsonAble) { json_out.key(f.getName()); ((JsonAble) value).toJson(json_out); // if variable is an array... recurse on sub-objects } else if (value instanceof ArrayList) { json_out.key(f.getName()); json_out.array(); for (Object e : (ArrayList) value) { toJson(e, json_out); } json_out.endArray(); // if variable is a simple type... convert to json } else { json_out.key(f.getName()).value(value); } } } json_out.endObject(); }
Example 7
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testKeyValue() throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.object(); stringer.key("a").value(false); stringer.key("b").value(5.0); stringer.key("c").value(5L); stringer.key("d").value("five"); stringer.key("e").value(null); stringer.endObject(); assertEquals("{\"a\":false," + "\"b\":5," + "\"c\":5," + "\"d\":\"five\"," + "\"e\":null}", stringer.toString()); }
Example 8
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testRepeatedKey() throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.object(); stringer.key("a").value(true); stringer.key("a").value(false); stringer.endObject(); // JSONStringer doesn't attempt to detect duplicates assertEquals("{\"a\":true,\"a\":false}", stringer.toString()); }
Example 9
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testEmptyKey() throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.object(); stringer.key("").value(false); stringer.endObject(); assertEquals("{\"\":false}", stringer.toString()); // legit behaviour! }
Example 10
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testJSONObjectAsValue() throws JSONException { JSONObject object = new JSONObject(); object.put("a", false); JSONStringer stringer = new JSONStringer(); stringer.object(); stringer.key("b").value(object); stringer.endObject(); assertEquals("{\"b\":{\"a\":false}}", stringer.toString()); }
Example 11
Source File: JSONStringerTest.java From j2objc with Apache License 2.0 | 4 votes |
public void testEmptyObject() throws JSONException { JSONStringer stringer = new JSONStringer(); stringer.object(); stringer.endObject(); assertEquals("{}", stringer.toString()); }
Example 12
Source File: SqlDBOperate.java From WifiChat with GNU General Public License v2.0 | 4 votes |
public String sendChattingInfoToJSON(int sendID, int receiverID) { List<ChattingInfo> infos; infos = getAllMessageFromChattingInfo(sendID, receiverID); JSONStringer jsonText = new JSONStringer(); try { // 首先是{,对象开始。object和endObject必须配对使用 jsonText.object(); jsonText.key("chatting"); // 键user的值是数组。array和endArray必须配对使用 jsonText.array(); for (ChattingInfo info : infos) { jsonText.object(); jsonText.key("id"); jsonText.value(info.getId()); jsonText.key("sendID"); jsonText.value(info.getSendID()); jsonText.key("receiverID"); jsonText.value(info.getReceiverID()); jsonText.key("chatting"); jsonText.value(info.getInfo()); jsonText.key("date"); jsonText.value(info.getDate()); jsonText.key("style"); jsonText.value(info.getStyle()); jsonText.endObject(); } jsonText.endArray(); // },对象结束 jsonText.endObject(); } catch (JSONException ex) { throw new RuntimeException(ex); } return jsonText.toString(); }
Example 13
Source File: SqlDBOperate.java From WifiChat with GNU General Public License v2.0 | 4 votes |
public String sendUserInfoToJSON() { List<UserInfo> users; int count = (int) getCountOfUserInfo(); users = getScrollDataOfUserInfo(0, count); JSONStringer jsonText = new JSONStringer(); try { // 首先是{,对象开始。object和endObject必须配对使用 jsonText.object(); jsonText.key("user"); // 键user的值是数组。array和endArray必须配对使用 jsonText.array(); for (UserInfo user : users) { jsonText.object(); jsonText.key("id"); jsonText.value(user.getId()); jsonText.key("name"); jsonText.value(user.getName()); jsonText.key("sex"); jsonText.value(user.getSex()); jsonText.key("age"); jsonText.value(user.getAge()); jsonText.key("IMEI"); jsonText.value(user.getIMEI()); jsonText.key("ip"); jsonText.value(user.getIPAddr()); jsonText.key("status"); jsonText.value(user.getIsOnline()); jsonText.key("avater"); jsonText.value(user.getAvater()); jsonText.key("lastdate"); jsonText.value(user.getLastDate()); jsonText.key("device"); jsonText.value(user.getDevice()); jsonText.key("constellation"); jsonText.value(user.getConstellation()); jsonText.endObject(); } jsonText.endArray(); // },对象结束 jsonText.endObject(); } catch (JSONException ex) { throw new RuntimeException(ex); } return jsonText.toString(); }
Example 14
Source File: MindmupExporter.java From netbeans-mmd-plugin with Apache License 2.0 | 4 votes |
private void writeRoot(@Nonnull final JSONStringer stringer, @Nonnull final MindMapPanelConfig cfg, @Nullable final Topic root) { stringer.object(); stringer.key("formatVersion").value(3L); stringer.key("id").value("root"); stringer.key("ideas").object(); final Map<String, String> linkMap = new HashMap<>(); final Map<String, TopicId> uuidTopicMap = new HashMap<>(); if (root != null) { stringer.key("1").object(); writeTopic(stringer, cfg, new AtomicInteger(1), root, linkMap, uuidTopicMap); stringer.endObject(); stringer.key("title").value(GetUtils.ensureNonNull(root.getText(), "[Root]")); } else { stringer.key("title").value("Empty map"); } stringer.endObject(); if (!linkMap.isEmpty()) { stringer.key("links").array(); for (final Map.Entry<String, String> entry : linkMap.entrySet()) { final TopicId from = uuidTopicMap.get(entry.getKey()); final TopicId to = uuidTopicMap.get(entry.getValue()); if (from != null && to != null) { stringer.object(); stringer.key("ideaIdFrom").value(from.id); stringer.key("ideaIdTo").value(to.id); stringer.key("attr").object(); stringer.key("style").object(); stringer.key("arrow").value("to"); stringer.key("color").value(Utils.color2html(cfg.getJumpLinkColor(), false)); stringer.key("lineStyle").value("dashed"); stringer.endObject(); stringer.endObject(); stringer.endObject(); } } stringer.endArray(); } stringer.endObject(); }
Example 15
Source File: SqlDBOperate.java From FlyWoo with Apache License 2.0 | 4 votes |
public String sendChattingInfoToJSON(int sendID, int receiverID) { List<ChattingInfo> infos; infos = getAllMessageFromChattingInfo(sendID, receiverID); JSONStringer jsonText = new JSONStringer(); try { // 首先是{,对象开始。object和endObject必须配对使用 jsonText.object(); jsonText.key("chatting"); // 键user的值是数组。array和endArray必须配对使用 jsonText.array(); for (ChattingInfo info : infos) { jsonText.object(); jsonText.key("id"); jsonText.value(info.getId()); jsonText.key("sendID"); jsonText.value(info.getSendID()); jsonText.key("receiverID"); jsonText.value(info.getReceiverID()); jsonText.key("chatting"); jsonText.value(info.getInfo()); jsonText.key("date"); jsonText.value(info.getDate()); jsonText.key("style"); jsonText.value(info.getStyle()); jsonText.endObject(); } jsonText.endArray(); // },对象结束 jsonText.endObject(); } catch (JSONException ex) { throw new RuntimeException(ex); } return jsonText.toString(); }
Example 16
Source File: SqlDBOperate.java From FlyWoo with Apache License 2.0 | 4 votes |
public String sendUserInfoToJSON() { List<UserInfo> users; int count = (int) getCountOfUserInfo(); users = getScrollDataOfUserInfo(0, count); JSONStringer jsonText = new JSONStringer(); try { // 首先是{,对象开始。object和endObject必须配对使用 jsonText.object(); jsonText.key("user"); // 键user的值是数组。array和endArray必须配对使用 jsonText.array(); for (UserInfo user : users) { jsonText.object(); jsonText.key("id"); jsonText.value(user.getId()); jsonText.key("name"); jsonText.value(user.getName()); jsonText.key("sex"); jsonText.value(user.getSex()); jsonText.key("age"); jsonText.value(user.getAge()); jsonText.key("IMEI"); jsonText.value(user.getIMEI()); jsonText.key("ip"); jsonText.value(user.getIPAddr()); jsonText.key("status"); jsonText.value(user.getIsOnline()); jsonText.key("avater"); jsonText.value(user.getAvater()); jsonText.key("lastdate"); jsonText.value(user.getLastDate()); jsonText.key("device"); jsonText.value(user.getDevice()); jsonText.key("constellation"); jsonText.value(user.getConstellation()); jsonText.endObject(); } jsonText.endArray(); // },对象结束 jsonText.endObject(); } catch (JSONException ex) { throw new RuntimeException(ex); } return jsonText.toString(); }
Example 17
Source File: Settings.java From onedev with MIT License | 4 votes |
public CharSequence toJson() { try { JSONStringer writer = new JSONStringer(); writer.object(); Json.writeObject(writer, "minimumInputLength", minimumInputLength); Json.writeObject(writer, "minimumResultsForSearch", minimumResultsForSearch); Json.writeObject(writer, "maximumSelectionSize", maximumSelectionSize); Json.writeObject(writer, "placeholder", placeholder); Json.writeObject(writer, "allowClear", allowClear); Json.writeObject(writer, "multiple", multiple); Json.writeObject(writer, "closeOnSelect", closeOnSelect); Json.writeFunction(writer, "id", id); Json.writeFunction(writer, "matcher", matcher); Json.writeFunction(writer, "tokenizer", tokenizer); Json.writeFunction(writer, "sortResults", sortResults); Json.writeFunction(writer, "formatSelection", formatSelection); Json.writeFunction(writer, "formatResult", formatResult); Json.writeFunction(writer, "formatNoMatches", formatNoMatches); Json.writeFunction(writer, "formatInputTooShort", formatInputTooShort); Json.writeFunction(writer, "formatResultCssClass", formatResultCssClass); Json.writeFunction(writer, "formatSelectionTooBig", formatSelectionTooBig); Json.writeFunction(writer, "formatLoadMore", formatLoadMore); Json.writeFunction(writer, "formatSearching", formatSearching); Json.writeFunction(writer, "escapeMarkup", escapeMarkup); Json.writeFunction(writer, "createSearchChoice", createSearchChoice); Json.writeFunction(writer, "initSelection", initSelection); Json.writeFunction(writer, "query", query); Json.writeObject(writer, "width", width); Json.writeObject(writer, "openOnEnter", openOnEnter); Json.writeFunction(writer, "containerCss", containerCss); Json.writeObject(writer, "containerCssClass", containerCssClass); Json.writeFunction(writer, "dropdownCss", dropdownCss); Json.writeObject(writer, "dropdownCssClass", dropdownCssClass); Json.writeObject(writer, "separator", separator); Json.writeObject(writer, "tokenSeparators", tokenSeparators); Json.writeObject(writer, "dropdownAutoWidth", dropdownAutoWidth); if (ajax != null) { writer.key("ajax"); ajax.toJson(writer); } Json.writeFunction(writer, "data", data); Json.writeFunction(writer, "tags", tags); writer.endObject(); return writer.toString(); } catch (JSONException e) { throw new RuntimeException("Could not convert Select2 settings object to Json", e); } }