@Override protected void renderMergedOutputModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { OutputStream stream = this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream(); Object value = filterModel(model); JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding); // A workaround for JsonGenerators not applying serialization features // https://github.com/FasterXML/jackson-databind/issues/12 if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) { generator.useDefaultPrettyPrinter(); } if (this.prefixJson) { generator.writeRaw("{} && "); } this.objectMapper.writeValue(generator, value); if (this.updateContentLength) { writeToResponse(response, (ByteArrayOutputStream) stream); } }
@Test public void test1() throws IOException { MapData ld0 = new MapData(); ld0.put("Copyright", "\u00A9 2014, David R. Smith. All Rights Reserved"); ld0.put("License", "http://www.apache.org/licenses/LICENSE-2.0.txt"); MapData ld1 = new MapData(); ld1.put("some key", "some data"); ld1.put("another key", "more data"); MapDataFormatter format = new MapDataFormatter(); format.add(ld0); format.add(ld1); StringWriter writer = new StringWriter(); JsonFactory f = new JsonFactory(); JsonGenerator g = null; try { g = f.createGenerator(writer); g.useDefaultPrettyPrinter(); g.writeStartObject(); format.format(g, writer); g.writeEndObject(); } finally { g.close(); } System.err.println(writer.toString()); MapIterator iter3 = new MapDataContentsIterator(ld0); while (iter3.hasNext()) { String label = iter3.next(); Assert.assertNotNull(iter3.get(label)); } }
public byte[] toJSONBytes() { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(outputStream, JsonEncoding.UTF8); jsonGenerator.useDefaultPrettyPrinter(); jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("type", ContentType); jsonGenerator.writeStringField("version", Version); jsonGenerator.writeStringField("uuid", workspaceUUID); jsonGenerator.writeStringField("name", workspaceName); jsonGenerator.writeStringField("description", workspaceDescription); // TODO: More details about who is making the change when and from what previous version? // jsonGenerator.writeStringField("timestamp", timestamp); // jsonGenerator.writeStringField("user", user); // jsonGenerator.writeStringField("previous", previous); jsonGenerator.writeEndObject(); jsonGenerator.close(); return outputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } }
public static void toJson(Object pojo, FileWriter fw, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { JsonGenerator jg = jf.createGenerator(fw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writeValue(jg, pojo); }
static { try { WRITER = JSON_MAPPER.getFactory().createGenerator(System.out); } catch (final IOException e) { throw new RuntimeException(e); } WRITER.configure(Feature.AUTO_CLOSE_TARGET, false); WRITER.useDefaultPrettyPrinter(); }
public static String toJson(Object pojo, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { StringWriter sw = new StringWriter(); JsonGenerator jg = jf.createGenerator(sw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writeValue(jg, pojo); return sw.toString(); }
public JsonWriter(OutputStream out, boolean pretty, boolean useExtendedOutput) throws IOException { JsonGenerator writer = factory.createJsonGenerator(out); if (pretty) { writer = writer.useDefaultPrettyPrinter(); } if (useExtendedOutput) { gen = new ExtendedJsonOutput(writer); } else { gen = new BasicJsonOutput(writer); } }
/** * Helper method called to set or override settings of passed-in {@link JsonGenerator} * * @since 2.1 */ private final void _configureJsonGenerator(JsonGenerator jgen) { if (_prettyPrinter != null) { PrettyPrinter pp = _prettyPrinter; if (pp == NULL_PRETTY_PRINTER) { jgen.setPrettyPrinter(null); } else { /* [JACKSON-851]: Better take care of stateful PrettyPrinters... * like the DefaultPrettyPrinter. */ if (pp instanceof Instantiatable<?>) { pp = (PrettyPrinter) ((Instantiatable<?>) pp).createInstance(); } jgen.setPrettyPrinter(pp); } } else if (_config.isEnabled(SerializationFeature.INDENT_OUTPUT)) { jgen.useDefaultPrettyPrinter(); } // [JACKSON-520]: add support for pass-through schema: if (_schema != null) { jgen.setSchema(_schema); } }
@Override public void serialize(CyNetworkView networkView, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.useDefaultPrettyPrinter(); jgen.writeStartObject(); jgen.writeStringField( CytoscapeJsNetworkModule.FORMAT_VERSION_TAG, CytoscapeJsNetworkModule.FORMAT_VERSION); jgen.writeStringField(CytoscapeJsNetworkModule.GENERATED_BY_TAG, "cytoscape-" + version); jgen.writeStringField( CytoscapeJsNetworkModule.TARGET_CYJS_VERSION_TAG, CytoscapeJsNetworkModule.CYTOSCAPEJS_VERSION); final CyNetwork network = networkView.getModel(); // Serialize network data table jgen.writeObjectFieldStart(DATA.getTag()); jgen.writeObject(network.getRow(network)); jgen.writeEndObject(); jgen.writeObjectFieldStart(ELEMENTS.getTag()); // Write array final List<CyNode> nodes = network.getNodeList(); final List<CyEdge> edges = network.getEdgeList(); jgen.writeArrayFieldStart(NODES.getTag()); for (final CyNode node : nodes) { jgen.writeStartObject(); // Data field jgen.writeObjectFieldStart(DATA.getTag()); jgen.writeStringField(ID.getTag(), node.getSUID().toString()); // Write CyRow in "data" field jgen.writeObject(network.getRow(node)); jgen.writeEndObject(); // Position and other visual props jgen.writeObject(networkView.getNodeView(node)); // Special case for cytoscape.js format: // - Selected jgen.writeBooleanField( CyNetwork.SELECTED, network.getRow(node).get(CyNetwork.SELECTED, Boolean.class)); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeArrayFieldStart(EDGES.getTag()); for (final CyEdge edge : edges) { jgen.writeStartObject(); jgen.writeObjectFieldStart(DATA.getTag()); jgen.writeStringField(ID.getTag(), edge.getSUID().toString()); jgen.writeStringField(SOURCE.getTag(), edge.getSource().getSUID().toString()); jgen.writeStringField(TARGET.getTag(), edge.getTarget().getSUID().toString()); // Write CyRow in "data" field jgen.writeObject(network.getRow(edge)); jgen.writeEndObject(); // Special case for cytoscape.js format: // - Selected jgen.writeBooleanField( CyNetwork.SELECTED, network.getRow(edge).get(CyNetwork.SELECTED, Boolean.class)); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeEndObject(); }