Java Code Examples for org.json.JSONStringer#endArray()

The following examples show how to use org.json.JSONStringer#endArray() . 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: PreUtils.java    From Hook with Apache License 2.0 6 votes vote down vote up
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 2
Source File: Utils.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * serialize String values in JSON format
 * 
 * @param array
 * @return
 */
public static String getJSON(String[] array) throws Exception
{
	try
	{
		JSONStringer js = new JSONStringer();
		js.array();
		if (array != null)
		{
			for (String value : array)
			{
				js.value(value);
			}
		}
		js.endArray();
		return js.toString();
	}
	catch (JSONException e)
	{
		throw new Exception("cannot produce JSON", e);
	}
}
 
Example 3
Source File: JsonEncoder.java    From egads with GNU General Public License v3.0 5 votes vote down vote up
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 4
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testValueJSONNull() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(JSONObject.NULL);
    stringer.endArray();
    assertEquals("[null]", stringer.toString());
}
 
Example 5
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testArray() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(false);
    stringer.value(5.0);
    stringer.value(5L);
    stringer.value("five");
    stringer.value(null);
    stringer.endArray();
    assertEquals("[false,5,5,\"five\",null]", stringer.toString());
}
 
Example 6
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testValueObjectMethods() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(Boolean.FALSE);
    stringer.value(Double.valueOf(5.0));
    stringer.value(Long.valueOf(5L));
    stringer.endArray();
    assertEquals("[false,5,5]", stringer.toString());
}
 
Example 7
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test what happens when extreme values are emitted. Such values are likely
 * to be rounded during parsing.
 */
public void testNumericRepresentations() throws JSONException {
    if (System.getProperty("os.arch").equals("armv7")) {
      // On armv7, MIN_VALUE is indistinguishable from zero.
      return;
    }
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(Long.MAX_VALUE);
    stringer.value(Double.MIN_VALUE);
    stringer.endArray();
    assertEquals("[9223372036854775807,4.9E-324]", stringer.toString());
}
 
Example 8
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testJSONArrayAsValue() throws JSONException {
    JSONArray array = new JSONArray();
    array.put(false);
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.value(array);
    stringer.endArray();
    assertEquals("[[false]]", stringer.toString());
}
 
Example 9
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMaxDepthWithArrayValue() throws JSONException {
    JSONArray array = new JSONArray();
    array.put(false);

    JSONStringer stringer = new JSONStringer();
    for (int i = 0; i < 20; i++) {
        stringer.array();
    }
    stringer.value(array);
    for (int i = 0; i < 20; i++) {
        stringer.endArray();
    }
    assertEquals("[[[[[[[[[[[[[[[[[[[[[false]]]]]]]]]]]]]]]]]]]]]", stringer.toString());
}
 
Example 10
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMultipleRoots() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.endArray();
    try {
        stringer.object();
        fail();
    } catch (JSONException e) {
    }
}
 
Example 11
Source File: SqlDBOperate.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
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 12
Source File: SqlDBOperate.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
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: MindmupExporter.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
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 14
Source File: JSONStringerTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testEmptyArray() throws JSONException {
    JSONStringer stringer = new JSONStringer();
    stringer.array();
    stringer.endArray();
    assertEquals("[]", stringer.toString());
}
 
Example 15
Source File: SqlDBOperate.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
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 16
Source File: SqlDBOperate.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
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();
}