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 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; }
@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; }
@Override public ModelAndView doGet(IHTTPSession session) { ModelAndView result = new ModelAndView(); JsonObject entity = Json.object(); for (Entry<String, Result> cur : Metrics.HEALTH_REGISTRY.runHealthChecks().entrySet()) { JsonObject value = Json.object().add("status", cur.getValue().getDetails().get("status").toString()); if (!cur.getValue().isHealthy()) { value.add("message", cur.getValue().getMessage()); } entity.add(cur.getKey(), value); } result.setData(entity.toString()); return result; }
public String toJson() { JsonObject result = Json.object(); if (!isEmpty()) { JsonObject errors = Json.object(); if (general != null) { errors.add("general", general); } for (Entry<String, String> cur : entrySet()) { errors.add(cur.getKey(), cur.getValue()); } result.add("errors", errors); } return result.toString(); }
public static String writeJsonObject(Object object) throws UnifyException { try { JsonObject jsonObject = Json.object(); DataUtils.writeJsonObject(object, jsonObject); return jsonObject.toString(); } catch (UnifyException e) { throw e; } catch (Exception e) { throw new UnifyException(UnifyCoreErrorConstants.DATAUTIL_ERROR, e); } }
@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; }
private void sendAck(String ticket) { try (Jedis j = pool.getResource()) { JsonObject m = Json.object(); m.add("ticket", ticket); m.add("ack", true); j.publish(RESPONSE_CHANNEL, m.toString()); } }
private void sendAck(String ticket) { try (Jedis j = pool.getResource()) { JsonObject m = Json.object(); m.add("ticket", ticket); m.add("ack", true); j.publish(RESPONSE_CHANNEL, m.toString()); } }
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(); }
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()); } }
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()); } }
public static JsonObject serialize(Identity identity) { JsonObject o = Json.object(); o.set(KEY_ADDRESS, identity.getPeerAddress().getHostString()); o.set(KEY_PORT, identity.getPeerAddress().getPort()); if (identity.isPSK()) { o.set(KEY_ID, identity.getPskIdentity()); } else if (identity.isRPK()) { PublicKey publicKey = identity.getRawPublicKey(); o.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded())); } else if (identity.isX509()) { o.set(KEY_CN, identity.getX509CommonName()); } return o; }
public static JsonObject serialize(Identity identity) { JsonObject o = Json.object(); o.set(KEY_ADDRESS, identity.getPeerAddress().getHostString()); o.set(KEY_PORT, identity.getPeerAddress().getPort()); if (identity.isPSK()) { o.set(KEY_ID, identity.getPskIdentity()); } else if (identity.isRPK()) { PublicKey publicKey = identity.getRawPublicKey(); o.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded())); } else if (identity.isX509()) { o.set(KEY_CN, identity.getX509CommonName()); } return o; }
private void sendNotification(String ticket, LwM2mNode value) { try (Jedis j = pool.getResource()) { JsonObject m = Json.object(); m.add("ticket", ticket); m.add("rep", ResponseSerDes.jSerialize(ObserveResponse.success(value))); j.publish(RESPONSE_CHANNEL, m.toString()); } }
private void sendNotification(String ticket, LwM2mNode value) { try (Jedis j = pool.getResource()) { JsonObject m = Json.object(); m.add("ticket", ticket); m.add("rep", ResponseSerDes.jSerialize(ObserveResponse.success(value))); j.publish(RESPONSE_CHANNEL, m.toString()); } }
@Override public JsonObject jSerialize(ResourceModel m) { final JsonObject o = Json.object(); o.add("id", m.id); o.add("name", m.name); o.add("operations", m.operations.toString()); o.add("instancetype", m.multiple ? "multiple" : "single"); o.add("mandatory", m.mandatory); o.add("type", m.type.toString().toLowerCase()); o.add("range", m.rangeEnumeration); o.add("units", m.units); o.add("description", m.description); return o; }
public static ModelAndView doLogin(Authenticator auth, String username, String password) { String token = auth.authenticate(username, password); ModelAndView result = new ModelAndView(); if (token == null) { result.setData(new ValidationResult("Invalid login or password").toJson()); result.setStatus(Response.Status.UNAUTHORIZED); } else { JsonObject data = Json.object(); data.add("access_token", token); data.add("token_type", "bearer"); data.add("expires_in", auth.getMaxAgeMillis() / 1000); result.setData(data.toString()); } return result; }