@Override public int run(InputStream stdin, PrintStream out, PrintStream err, List<String> args) throws Exception { if (args.size() != 2) { err.println("Expected 2 arguments: schema binary_data_file"); err.println("Use '-' as binary_data_file for stdin."); return 1; } Schema schema = Schema.parse(args.get(0)); InputStream input; boolean needsClosing; if (args.get(1).equals("-")) { input = stdin; needsClosing = false; } else { input = new FileInputStream(args.get(1)); needsClosing = true; } try { DatumReader<Object> reader = new GenericDatumReader<Object>(schema); Object datum = reader.read(null, DecoderFactory.get().binaryDecoder(input, null)); DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema); JsonGenerator g = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8); g.useDefaultPrettyPrinter(); writer.write(datum, EncoderFactory.get().jsonEncoder(schema, g)); g.flush(); out.println(); out.flush(); } finally { if (needsClosing) { input.close(); } } return 0; }
public static void toJson(Object pojo, Writer writer, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { JsonGenerator jg = jf.createJsonGenerator(writer); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writeValue(jg, pojo); }
public static String toJson(Object pojo, boolean prettyPrint) throws JsonMappingException, JsonGenerationException, IOException { StringWriter sw = new StringWriter(); JsonGenerator jg = jf.createJsonGenerator(sw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writeValue(jg, pojo); return sw.toString(); }
public String toJSON(T object) throws IOException { JsonFactory jsonFactory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(jsonFactory); mapper.getSerializationConfig().setAnnotationIntrospector(new JacksonAnnotationIntrospector()); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); jsonFactory.setCodec(mapper); StringWriter writer = new StringWriter(); JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer); jsonGenerator.useDefaultPrettyPrinter(); jsonGenerator.writeObject(object); return writer.getBuffer().toString(); }
protected String toJson(boolean pretty) { try { StringWriter writer = new StringWriter(); JsonGenerator gen = FACTORY.createJsonGenerator(writer); if (pretty) gen.useDefaultPrettyPrinter(); toJson(gen); gen.flush(); return writer.toString(); } catch (IOException e) { throw new RuntimeException(e); } }
private void process(File input) throws IOException { JsonFactory jsonF = new JsonFactory(); JsonParser jp = jsonF.createJsonParser(input); TwitterEntry entry = read(jp); // let's write to a file, using UTF-8 encoding (only sensible one) StringWriter strw = new StringWriter(); JsonGenerator jg = jsonF.createJsonGenerator(strw); jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier // Here we would modify it... for now, will just (re)indent it write(jg, entry); System.out.println("Result = [" + strw.toString() + "]"); }
public static void toJson(Object pojo, Writer fw, boolean prettyPrint) throws JsonException { JsonGenerator jg = createJsonGenerator(fw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } try { m.writeValue(jg, pojo); } catch (JsonParseException e) { throw new JsonException(e); } catch (JsonMappingException e) { throw new JsonException(e); } catch (IOException e) { throw new JsonException(e); } }
/** * Writes the response values back to the http response. This allows the calling code to parse the * response values for display to the user. * * @param responseMap the response params to write to the http response * @param response the http response * @throws IOException */ private void writeToResponse(Map<String, String> responseMap, HttpServletResponse response) throws IOException { // Note: setting the content-type to text/html because otherwise IE prompt the user to download // the result rather than handing it off to the GWT form response handler. // See JIRA issue https://issues.jboss.org/browse/SRAMPUI-103 response.setContentType("text/html; charset=UTF8"); // $NON-NLS-1$ JsonFactory f = new JsonFactory(); JsonGenerator g = f.createJsonGenerator(response.getOutputStream(), JsonEncoding.UTF8); g.useDefaultPrettyPrinter(); g.writeStartObject(); for (java.util.Map.Entry<String, String> entry : responseMap.entrySet()) { String key = entry.getKey(); String val = entry.getValue(); g.writeStringField(key, val); } g.writeEndObject(); g.flush(); g.close(); }
public static String toJson(Object pojo, Class<?> viewClass, boolean prettyPrint) throws JsonException { try { StringWriter sw = new StringWriter(); JsonGenerator jg = createJsonGenerator(sw); if (prettyPrint) { jg.useDefaultPrettyPrinter(); } m.writerWithView(viewClass).writeValue(jg, pojo); return sw.toString(); } catch (JsonParseException e) { throw new JsonException(e); } catch (JsonMappingException e) { throw new JsonException(e); } catch (IOException e) { throw new JsonException(e); } }
public static XMLStreamWriter createWriter( Writer writer, JSONConfiguration config, boolean writingList) throws IOException { switch (config.getNotation()) { case NATURAL: final JsonGenerator rawGenerator = new JsonFactory().createJsonGenerator(writer); if (config.isHumanReadableFormatting()) { rawGenerator.useDefaultPrettyPrinter(); } final JsonGenerator bodyGenerator = writingList ? JacksonArrayWrapperGenerator.createArrayWrapperGenerator( rawGenerator, config.isRootUnwrapping() ? 0 : 1) : rawGenerator; if (config.isRootUnwrapping()) { return new Stax2JacksonWriter( JacksonRootStrippingGenerator.createRootStrippingGenerator( bodyGenerator, writingList ? 2 : 1), config); } else { return new Stax2JacksonWriter(bodyGenerator, config); } case MAPPED: return JsonXmlStreamWriter.createWriter(writer, config); case BADGERFISH: return new BadgerFishXMLStreamWriter(writer); case MAPPED_JETTISON: Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } return new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer); default: return null; } }
private void serialize() throws GVCException { JsonGenerator jsonGenerator; StringWriter stringWriter = new StringWriter(); try { jsonGenerator = jsonFactory.createJsonGenerator(stringWriter); } catch (IOException e) { throw new GVCException(e); } jsonGenerator.useDefaultPrettyPrinter(); ObjectNode rootNode = objectMapper.createObjectNode(); // Parent if (this.parent == null) rootNode.put("parent", "null"); else rootNode.put("parent", this.parent.getHash()); // Date rootNode.put("date", df.format(this.date)); // Comment rootNode.put("comment", this.comment); // Files Added /* This is a JSON "object", it looks something like this: * { * filesAdded: { * sd5df5s2: [file1, file2], * f52s556c: [file3] * } * } */ if (this.filesAdded == null) { rootNode.put("filesAdded", "null"); } else { ObjectNode addedOb = rootNode.putObject("filesAdded"); for (String fHash : this.filesAdded.keySet()) { // this will be an array of filenames for this hash ArrayNode aNode = addedOb.putArray(fHash); for (File file : this.filesAdded.get(fHash)) { // Normalize the filesystem separator to / String fileS = file.toString().replace(File.separator, "/"); aNode.add(fileS); } } } // Files Removed if (this.filesRemoved == null) { rootNode.put("filesRemoved", "null"); } else { ObjectNode removedOb = rootNode.putObject("filesRemoved"); for (String fHash : this.filesRemoved.keySet()) { // this will be an array of filenames for this hash ArrayNode rNode = removedOb.putArray(fHash); for (File file : this.filesRemoved.get(fHash)) { // Normalize the filesystem separator to / String fileS = file.toString().replace(File.separator, "/"); rNode.add(fileS); } } } // And write it all out to a string. try { jsonGenerator.writeObject(rootNode); this.serialized = stringWriter.getBuffer().toString(); } catch (Exception e) { throw new GVCException(e); } }
private void serialize(Query query, OutputStream outputStream) throws IOException { JsonGenerator g = jsonFactory.createJsonGenerator(outputStream, JsonEncoding.UTF8); g.useDefaultPrettyPrinter(); g.writeStartObject(); g.writeStringField("name", "jmxtrans"); g.writeStringField("type", "metric"); g.writeStringField("handler", sensuhandler); StringBuffer jsonoutput = new StringBuffer(); List<String> typeNames = getTypeNames(); for (Result result : query.getResults()) { Map<String, Object> resultValues = result.getValues(); if (resultValues != null) { for (Map.Entry<String, Object> values : resultValues.entrySet()) { if (NumberUtils.isNumeric(values.getValue())) { Object value = values.getValue(); jsonoutput .append(JmxUtils.getKeyString(query, result, values, typeNames, null)) .append(" ") .append(value) .append(" ") .append(TimeUnit.SECONDS.convert(result.getEpoch(), TimeUnit.MILLISECONDS)) .append(System.getProperty("line.separator")); } } } } g.writeStringField("output", jsonoutput.toString()); g.writeEndObject(); g.flush(); g.close(); }