/** * Convert a POJO into Serialized JSON form. * * @param o the POJO to serialize * @param s the OutputStream to write to.. * @throws IOException when there are problems creating the JSON. */ public static void serializeAsStream(Object o, OutputStream s) throws IOException { try { JsonStructure builtJsonObject = findFieldsToSerialize(o).mainObject; Map<String, Object> config = new HashMap<String, Object>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(config); JsonWriter streamWriter = writerFactory.createWriter(s); streamWriter.write(builtJsonObject); } catch (IllegalStateException ise) { // the reflective attempt to build the object failed. throw new IOException("Unable to build JSON for Object", ise); } catch (JsonException e) { throw new IOException("Unable to build JSON for Object", e); } }
@Test public void prettySimpleStructure() { final JsonWriterFactory writerFactory = Json.createWriterFactory(new HashMap<String, Object>() { { put(JsonGenerator.PRETTY_PRINTING, true); } }); StringWriter buffer = new StringWriter(); final JsonWriter writer = writerFactory.createWriter(buffer); writer.write(Json.createObjectBuilder().add("firstName", "John").build()); writer.close(); assertEquals("{\n" + " \"firstName\":\"John\"\n" + "}", buffer.toString()); }
@Test public void prettySimpleWriter() { final JsonWriterFactory writerFactory = Json.createWriterFactory(new HashMap<String, Object>() { { put(JsonGenerator.PRETTY_PRINTING, true); } }); StringWriter buffer = new StringWriter(); final JsonReader reader = Json.createReader(new ByteArrayInputStream("{\"firstName\":\"John\"}".getBytes())); final JsonWriter writer = writerFactory.createWriter(buffer); writer.write(reader.read()); writer.close(); reader.close(); assertEquals("{\n" + " \"firstName\":\"John\"\n" + "}", buffer.toString()); }
@Deprecated static String jsonObject2prettyString(JsonObject jsonObject) { Map<String, Boolean> config = new HashMap<>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config); StringWriter stringWriter = new StringWriter(); try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) { jsonWriter.writeObject(jsonObject); } return stringWriter.toString(); }
@Deprecated static String jsonArray2prettyString(JsonArray jsonArray) { Map<String, Boolean> config = new HashMap<>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config); StringWriter stringWriter = new StringWriter(); try (JsonWriter jsonWriter = jsonWriterFactory.createWriter(stringWriter)) { jsonWriter.writeArray(jsonArray); } return stringWriter.toString(); }
public static String marshallObject(Object obj) throws JAXBException { JsonObjectBuilderResult result = new JsonObjectBuilderResult(); getJc().createMarshaller().marshal(obj, result); Map<String, Object> jsonProperties = new HashMap<String, Object>(); jsonProperties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(jsonProperties); StringWriter strWriter = new StringWriter(); JsonWriter writer = writerFactory.createWriter(strWriter); writer.writeObject(result.getJsonObjectBuilder().build()); writer.close(); return strWriter.toString(); }
/** * Returns a pretty-printed JSON string for the given example instance of the given RAML type. * * @param type * RAML type declaration * @param example * example instance * @return pretty-printed JSON string */ public String prettyPrint(TypeDeclaration type, ExampleSpec example) { JsonValue json = toJsonValue(type, example); Map<String, Boolean> config = new HashMap<>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory jwf = Json.createWriterFactory(config); StringWriter sw = new StringWriter(); JsonWriter writer = jwf.createWriter(sw); if (json instanceof JsonStructure) { writer.write((JsonStructure) json); return sw.toString(); } else { return json.toString(); } }
private static JsonWriterFactory getPrettyJsonWriterFactory() { if (null == FACTORY_INSTANCE) { final Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); FACTORY_INSTANCE = Json.createWriterFactory(properties); } return FACTORY_INSTANCE; }
public static String jsonFormat(JsonStructure json, String... options) { StringWriter stringWriter = new StringWriter(); Map<String, Boolean> config = buildConfig(options); JsonWriterFactory writerFactory = Json.createWriterFactory(config); JsonWriter jsonWriter = writerFactory.createWriter(stringWriter); jsonWriter.write(json); jsonWriter.close(); return stringWriter.toString(); }
/** * Write the contents of {@link #assets} to {@link #file}. * <p> * This method ensures that the asset with id {@code n} is always written to the {@code n}th position in the file, using {@code null}s for padding if required. */ private synchronized void rewriteFile() throws FileNotFoundException, IOException { JsonArrayBuilder jsonToStore = Json.createArrayBuilder(); // Iterate through the assets in id order Map<String, JsonObject> assetMap = assets == null ? Collections.<String, JsonObject> emptyMap() : assets; for (int i = 1; i <= idCounter.get(); i++) { JsonObject json = assetMap.get(Integer.toString(i)); if (json == null) { jsonToStore.addNull(); } else { jsonToStore.add(json); } } // Write the assets back to the file FileOutputStream out = null; try { Map<String, Object> config = new HashMap<String, Object>(); config.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(config); out = new FileOutputStream(file); JsonWriter streamWriter = writerFactory.createWriter(out); streamWriter.write(jsonToStore.build()); } finally { if (out != null) { out.close(); } } }
private String prettyPrint(JsonObject json) { Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); StringWriter sw = new StringWriter(); try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) { jsonWriter.writeObject(json); } return sw.toString(); }
@Override public void contextInitialized(final ServletContextEvent servletContextEvent) { final ClassLoader classLoader = servletContextEvent.getServletContext().getClassLoader(); final JsonReaderFactory reader = newReadFactory(); READER_FACTORY_BY_LOADER.put(classLoader, reader); servletContextEvent.getServletContext().setAttribute(READER_ATTRIBUTE, reader); final JsonWriterFactory writer = newWriterFactory(); WRITER_FACTORY_BY_LOADER.put(classLoader, writer); servletContextEvent.getServletContext().setAttribute(WRITER_ATTRIBUTE, reader); }
public static JsonWriterFactory writerLocate() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = FactoryLocator.class.getClassLoader(); } final JsonWriterFactory factory = WRITER_FACTORY_BY_LOADER.get(loader); if (factory == null) { return newWriterFactory(); } return factory; }
/** * Prettyprints the given JSON structure. (All well-formed JSON strings * have an array or object at their top level.) * @param value a JSON structure * @return a pretty-printed JSON string */ public static String prettyprint(JsonStructure value) { StringWriter string = new StringWriter(); JsonWriterFactory writerFactory = Json.createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, null)); try (JsonWriter writer = writerFactory.createWriter(string)) { writer.write(value); } return string.toString(); }
/** * write a JsonObject formatted in a pretty way into a String * @param jsonObject * @return a String containing the JSON */ public static String writePretty(JsonObject jsonObject) { JsonWriterFactory factory = createPrettyWriterFactory(); StringWriter sw = new StringWriter(); JsonWriter writer = factory.createWriter(sw); writer.writeObject(jsonObject); String ret = sw.toString(); writer.close(); return ret; }
@Override public JsonWriterFactory writerFactory() { return writerFactory; }
public static JsonWriterFactory createWriterFactory(Map<String, ?> config) { return provider.createWriterFactory(config); }
public JsrMessageBodyWriter(final JsonWriterFactory factory, final boolean closeStreams) { this.factory = factory; this.close = closeStreams; }
@Override public JsonWriterFactory createWriterFactory(final Map<String, ?> stringMap) { return DELEGATE.createWriterFactory(stringMap); }
@Override public JsonWriterFactory createWriterFactory(final Map<String, ?> config) { return (config == null || config.isEmpty()) ? writerFactory : new JsonWriterFactoryImpl(config); }
private static JsonWriterFactory newWriterFactory() { return Json.createWriterFactory(Collections.<String, Object>emptyMap()); }
private static JsonWriterFactory createPrettyWriterFactory() { HashMap<String, Object> prettyConfigMap = new HashMap<String, Object>(); prettyConfigMap.put(JsonGenerator.PRETTY_PRINTING, Boolean.TRUE); JsonWriterFactory prettyWriterFactory = Json.createWriterFactory(prettyConfigMap); return prettyWriterFactory; }
@Override public JsonWriterFactory createWriterFactory( Map<String, ?> config ) { return defaultProvider.createWriterFactory(config); }
/** * Creates a writer factory for creating {@link JsonWriter} objects. * The factory is configured with the specified map of provider specific * configuration properties. Provider implementations should ignore any * unsupported configuration properties specified in the map. * * @param config a map of provider specific properties to configure the * JSON writers. The map may be empty or null * @return a JSON writer factory */ public static JsonWriterFactory createWriterFactory(Map<String, ?> config) { return PROVIDER_INSTANCE.createWriterFactory(config); }
JsonWriterFactory writerFactory();