public static void main(String[] args) throws IOException { // convert Java to writer JsonObject root = Json.object().add("message", "Hi").add( "place", Json.object().add("name", "World!") ); StringWriter writer = new StringWriter(); root.writeTo(writer); String json = writer.toString(); System.out.println(json); System.out.println(); // convert writer to Java JsonObject obj = Json.parse(json).asObject(); String message = obj.get("message").asString(); String name = obj.get("place").asObject().get("name").asString(); System.out.println(message + " " + name); } }
@Override public void endObjectValue(JsonObject object, String name) { object.add(name, value); }
@Override protected void setUp() throws IOException { jsonObject = new JsonObject(); for (int index = 0; index < size; index++) { String name = Integer.toHexString(index); jsonObject.add(name, index); } }
/** * Appends a new member to the end of this object, with the specified name and the JSON * representation of the specified <code>boolean</code> value. * <p> * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name * that already exists in the object will append another member with the same name. In order to * replace existing members, use the method <code>set(name, value)</code> instead. However, * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to * search for existing members). Therefore <em>add</em> should be preferred when constructing new * objects. * </p> * * @param name * the name of the member to add * @param value * the value of the member to add * @return the object itself, to enable method chaining */ public JsonObject add(String name, boolean value) { add(name, Json.value(value)); return this; }
/** * Appends a new member to the end of this object, with the specified name and the JSON * representation of the specified string. * <p> * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name * that already exists in the object will append another member with the same name. In order to * replace existing members, use the method <code>set(name, value)</code> instead. However, * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to * search for existing members). Therefore <em>add</em> should be preferred when constructing new * objects. * </p> * * @param name * the name of the member to add * @param value * the value of the member to add * @return the object itself, to enable method chaining */ public JsonObject add(String name, String value) { add(name, Json.value(value)); return this; }
/** * Appends a new member to the end of this object, with the specified name and the JSON * representation of the specified <code>double</code> value. * <p> * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name * that already exists in the object will append another member with the same name. In order to * replace existing members, use the method <code>set(name, value)</code> instead. However, * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to * search for existing members). Therefore <em>add</em> should be preferred when constructing new * objects. * </p> * * @param name * the name of the member to add * @param value * the value of the member to add * @return the object itself, to enable method chaining */ public JsonObject add(String name, double value) { add(name, Json.value(value)); return this; }
/** * Appends a new member to the end of this object, with the specified name and the JSON * representation of the specified <code>long</code> value. * <p> * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name * that already exists in the object will append another member with the same name. In order to * replace existing members, use the method <code>set(name, value)</code> instead. However, * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to * search for existing members). Therefore <em>add</em> should be preferred when constructing new * objects. * </p> * * @param name * the name of the member to add * @param value * the value of the member to add * @return the object itself, to enable method chaining */ public JsonObject add(String name, long value) { add(name, Json.value(value)); return this; }
/** * Appends a new member to the end of this object, with the specified name and the JSON * representation of the specified <code>int</code> value. * <p> * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name * that already exists in the object will append another member with the same name. In order to * replace existing members, use the method <code>set(name, value)</code> instead. However, * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to * search for existing members). Therefore <em>add</em> should be preferred when constructing new * objects. * </p> * * @param name * the name of the member to add * @param value * the value of the member to add * @return the object itself, to enable method chaining */ public JsonObject add(String name, int value) { add(name, Json.value(value)); return this; }
/** * Appends a new member to the end of this object, with the specified name and the JSON * representation of the specified <code>float</code> value. * <p> * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name * that already exists in the object will append another member with the same name. In order to * replace existing members, use the method <code>set(name, value)</code> instead. However, * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to * search for existing members). Therefore <em>add</em> should be preferred when constructing new * objects. * </p> * * @param name * the name of the member to add * @param value * the value of the member to add * @return the object itself, to enable method chaining */ public JsonObject add(String name, float value) { add(name, Json.value(value)); return this; }
private static JsonValue extractEnvironment(JsonObject caliperResults) { JsonObject details = caliperResults.get("environment").asObject().get("propertyMap").asObject(); details.add("benchmark.classname", extractBenchmarkName(caliperResults)); details.add("benchmark.executionTime", extractTimestamp(caliperResults)); return details; }
private static JsonObject extractMeasurement(JsonObject measurement) { JsonObject times = measurement.get("v").asObject() .get("measurementSetMap").asObject() .get("TIME").asObject(); return new JsonObject().add("variables", measurement.get("k").asObject().get("variables")) .add("units", times.get("unitNames")) .add("values", extractTimes(times.get("measurements").asArray())); }
private JsonObject toJson(Map<String, String> tags) { final JsonObject obj = new JsonObject(); for (Map.Entry<String, String> entry : tags.entrySet()) { obj.add(entry.getKey(), entry.getValue()); } return obj; }
@Override public void visit(CreateRequest request) { o.add("kind", "create"); o.add("contentFormat", request.getContentFormat().getCode()); if (request.getInstanceId() != null) o.add("instanceId", request.getInstanceId()); JsonArray resources = new JsonArray(); for (LwM2mResource resource : request.getResources()) { resources.add(LwM2mNodeSerDes.jSerialize(resource)); } o.add("resources", resources); }
@Override protected String createStringRepresentation() { final com.eclipsesource.json.JsonObject minJsonObject = new com.eclipsesource.json.JsonObject(); fields.values().forEach(field -> minJsonObject.add(field.getKeyName(), JsonFactory.convert(field.getValue()))); return minJsonObject.toString(); }
private void sendResponse(String ticket, LwM2mResponse response) { if (response instanceof ObserveResponse) { Observation observation = ((ObserveResponse) response).getObservation(); observatioIdToTicket.put(new KeyId(observation.getId()), ticket); } try (Jedis j = pool.getResource()) { JsonObject m = Json.object(); m.add("ticket", ticket); m.add("rep", ResponseSerDes.jSerialize(response)); j.publish(RESPONSE_CHANNEL, m.toString()); } }
@Override public com.eclipsesource.json.JsonValue minimaljson(Users obj) throws IOException { com.eclipsesource.json.JsonObject jso = com.eclipsesource.json.Json.object(); if (obj.users != null) { com.eclipsesource.json.JsonArray jsarr = (com.eclipsesource.json.JsonArray) com.eclipsesource.json.Json.array(); for (User u : obj.users) { jsarr.add(minimaljson(u)); } jso.add("users", jsarr); } return jso; }