Refine search
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); } }
private void setUnflattenedValue(JsonObject flattened, String key, JsonValue currentVal, String objKey, Integer aryIdx) { JsonValue val = flattened.get(key); if (objKey != null) { if (val.isArray()) { JsonValue jsonArray = Json.array(); for (JsonValue arrayVal : val.asArray()) { jsonArray.asArray().add( Json.parse(newJsonUnflattener(arrayVal.toString()).unflatten())); } currentVal.asObject().add(objKey, jsonArray); } else { currentVal.asObject().add(objKey, val); } } else { // aryIdx != null assureJsonArraySize(currentVal.asArray(), aryIdx); currentVal.asArray().set(aryIdx, val); } }
private JsonValue findOrCreateJsonObject(JsonValue currentVal, String objKey, Integer aryIdx) { if (objKey != null) { JsonObject jsonObj = currentVal.asObject(); if (jsonObj.get(objKey) == null) { JsonValue obj = Json.object(); jsonObj.add(objKey, obj); return obj; } return jsonObj.get(objKey); } else { // aryIdx != null JsonArray jsonAry = currentVal.asArray(); if (jsonAry.size() <= aryIdx || jsonAry.get(aryIdx).equals(Json.NULL)) { JsonValue obj = Json.object(); assureJsonArraySize(jsonAry, aryIdx); jsonAry.set(aryIdx, obj); return obj; } return jsonAry.get(aryIdx); } }
private JsonValue findOrCreateJsonArray(JsonValue currentVal, String objKey, Integer aryIdx) { if (objKey != null) { JsonObject jsonObj = currentVal.asObject(); if (jsonObj.get(objKey) == null) { JsonValue ary = Json.array(); jsonObj.add(objKey, ary); return ary; } return jsonObj.get(objKey); } else { // aryIdx != null JsonArray jsonAry = currentVal.asArray(); if (jsonAry.size() <= aryIdx || jsonAry.get(aryIdx).equals(Json.NULL)) { JsonValue ary = Json.array(); assureJsonArraySize(jsonAry, aryIdx); jsonAry.set(aryIdx, ary); return ary; } return jsonAry.get(aryIdx); } }
@SuppressWarnings({"unchecked", "rawtypes"}) public FractionListParser(InputStream fractionListJson) throws IOException { try (InputStreamReader reader = new InputStreamReader(fractionListJson)) { Json.parse(reader).asArray().forEach(entry -> { JsonObject fraction = entry.asObject(); FractionDescriptor fd = getFractionDescriptor(fraction); addDependencies(fraction, fd); }); } }
private JsonArray unflattenArray(JsonArray array) { JsonArray unflattenArray = Json.array().asArray(); for (JsonValue value : array) { if (value.isArray()) { unflattenArray.add(unflattenArray(value.asArray())); } else if (value.isObject()) { unflattenArray.add(Json.parse(new JsonUnflattener(value.toString()) .withSeparator(separator).unflatten())); } else { unflattenArray.add(value); } } return unflattenArray; }
@Override public Users minimaljson(Reader reader) throws IOException { com.eclipsesource.json.JsonObject jso = com.eclipsesource.json.Json.parse(reader).asObject(); com.eclipsesource.json.JsonValue v; Users uc = new Users(); if ((v = jso.get("users")) != null) { com.eclipsesource.json.JsonArray jsarr = v.asArray(); uc.users = new ArrayList<>(); for (com.eclipsesource.json.JsonValue vi : jsarr) { uc.users.add(minimaljsonUser(vi.asObject())); } } return uc; }
@Override public Object readFromString(String string) throws IOException { return Json.parse(string); }
/** * 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; }
@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; }
@Override public JsonObject jSerialize(X509Certificate certificate) { final JsonObject o = Json.object(); // add pubkey info o.add("pubkey", publicKeySerDes.jSerialize(certificate.getPublicKey())); // Get certificate (DER encoding) try { o.add("b64Der", Base64.encodeBase64String(certificate.getEncoded())); } catch (CertificateEncodingException e) { throw new RuntimeException(e); } return o; }
private static com.eclipsesource.json.JsonArray tryToReadMinimalJsonArrayFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asArray(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON array from string!") .cause(e) .build(); } }
public static byte[] serialize(Observation obs) { JsonObject o = Json.object(); o.set("request", Hex.encodeHexString(serializer.serializeRequest(obs.getRequest()).bytes)); if (obs.getContext() != null) o.set("peer", EndpointContextSerDes.serialize(obs.getContext())); else o.set("peer", EndpointContextSerDes.serialize(obs.getRequest().getDestinationContext())); if (obs.getRequest().getUserContext() != null) { JsonObject ctxObject = Json.object(); for (Entry<String, String> e : obs.getRequest().getUserContext().entrySet()) { ctxObject.set(e.getKey(), e.getValue()); } o.set("context", ctxObject); } return o.toString().getBytes(); }
@Override public ModelAndView doPost(IHTTPSession session) { JsonValue request = Json.parse(WebServer.getRequestBody(session)); if (!request.isObject()) { return new BadRequest("expected object"); } String username = ((JsonObject) request).getString("username", null); String password = ((JsonObject) request).getString("password", null); return doLogin(auth, username, password); }