public <T extends AbstractDbEntity> String buildJsonStringByTypeAndId(long Id, Class<T> type) { String result = ""; T DBobject = objectsController.select(type, Id); if (DBobject == null) { return result; } // may return null JsonObjectBuilder transJOB; try { transJOB = JsonObjectFiller.getJsonObjectBuilderFromClassInstance(DBobject); } catch (IllegalArgumentException | IllegalAccessException e) { return result; } JsonObject JObject = transJOB.build(); Map<String, Boolean> config = new HashMap<>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory factory = Json.createWriterFactory(config); // http://blog.eisele.net/2013/02/test-driving-java-api-for-processing.html StringWriter sw = new StringWriter(); try (JsonWriter jw = factory.createWriter(sw)) { jw.writeObject(JObject); } result = sw.toString(); return result; }
public String JSonObjectBuilderToString(JsonObjectBuilder transJOB) { String returnString = ""; JsonObject JObject = transJOB.build(); Map<String, Boolean> config = new HashMap<>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory factory = Json.createWriterFactory(config); // http://blog.eisele.net/2013/02/test-driving-java-api-for-processing.html StringWriter sw = new StringWriter(); try (JsonWriter jw = factory.createWriter(sw)) { jw.writeObject(JObject); } returnString = sw.toString(); return returnString; }
@Override public JsonResource setContents(JsonStructure structure) { if (structure == null) throw new IllegalArgumentException("JsonStructure must not be null."); try { if (!exists()) { getParent().mkdirs(); if (!createNewFile()) { throw new IOException("Failed to create file: " + getUnderlyingResourceObject()); } } JsonWriterFactory writerFactory = Json.createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true)); try (OutputStream out = getFileOperations().createOutputStream(getUnderlyingResourceObject()); JsonWriter writer = writerFactory.createWriter(out)) { writer.write(structure); } } catch (IOException e) { throw new ResourceException("Error while setting the Json contents", e); } return this; }