Refine search
@Override public String toString() { StringWriter sw = new StringWriter(); try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) { jw.write(this); } return sw.toString(); }
String toJson(JsonObject toJsonFunc) { StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter)); jsonWriter.writeObject(toJsonFunc); jsonWriter.close(); return stringWriter.toString(); }
@Override public void writeTo(JsonArray jsonValues, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { LogMessages.LOGGER.debugf("Provider : %s, Method : writeTo", getClass().getName()); JsonWriter writer = findWriter(mediaType, entityStream); try { writer.writeArray(jsonValues); } finally { writer.close(); } } }
/** * Returns a function (writer) converting {@link JsonStructure} to the {@link Flow.Publisher Publisher} * of {@link DataChunk}s. * * @param charset a charset to use or {@code null} for default charset * @return created function */ public Function<JsonStructure, Flow.Publisher<DataChunk>> writer(Charset charset) { return json -> { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonWriter writer = (charset == null) ? jsonWriterFactory.createWriter(baos) : jsonWriterFactory.createWriter(baos, charset); writer.write(json); writer.close(); return ContentWriters.byteArrayWriter(false) .apply(baos.toByteArray()); }; }
JsonObjectBuilder builder = Json.createObjectBuilder(); String text = context.getProperty(WEBHOOK_TEXT).evaluateAttributeExpressions(flowFile).getValue(); if (text != null && !text.isEmpty()) { JsonArrayBuilder jsonArrayBuiler = Json.createArrayBuilder(); for (PropertyDescriptor attachment : attachments) { String s = context.getProperty(attachment).evaluateAttributeExpressions(flowFile).getValue(); JsonReader reader = Json.createReader(new StringReader(s)); JsonObject attachmentJson = reader.readObject(); jsonArrayBuiler.add(attachmentJson); StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = Json.createWriter(stringWriter); jsonWriter.writeObject(jsonObject); jsonWriter.close(); conn.setDoOutput(true); DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream()); String payload = "payload=" + URLEncoder.encode(stringWriter.getBuffer().toString(), "UTF-8"); outputStream.writeBytes(payload); outputStream.close();
public static String prettyPrint(javax.json.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(); }
/** * Gets the plainkey parameters. * * @return the plainkey parameters */ private String getPlainkeyParameters() { StringWriter keyParameterWriter = new StringWriter(); Json.createWriter(keyParameterWriter).writeObject(this.keyParameters); return keyParameterWriter.getBuffer().toString(); }
public String to(DocStructure userObject) { if (!initialized) { throw new IllegalStateException("The system is not initialized"); } StringWriter stringWriter = new StringWriter(100); JsonWriter writer = Json.createWriter(stringWriter); JsonObject jsonStructure = (JsonObject) userObject.accept(TO_JSON, colSchema); writer.write(jsonStructure); return stringWriter.toString(); }
@Override public String toString() { StringWriter sw = new StringWriter(); JsonWriter jw = new JsonWriterImpl(sw, bufferPool); jw.write(this); jw.close(); return sw.toString(); } }
private String format(final String json) { final JsonProvider provider = JsonProvider.provider(); final StringWriter out = new StringWriter(); try (final JsonReader reader = provider.createReader(new StringReader(json)); final JsonWriter jsonWriter = provider.createWriterFactory(singletonMap(JsonGenerator.PRETTY_PRINTING, true)) .createWriter(out)) { // jsonWriter.write(reader.readValue()); // bug in RI, can switch to johnzon final JsonStructure read = reader.read(); if (read.getValueType() == JsonValue.ValueType.OBJECT) { jsonWriter.writeObject(JsonObject.class.cast(read)); } else if (read.getValueType() == JsonValue.ValueType.ARRAY) { jsonWriter.writeArray(JsonArray.class.cast(read)); } else { // no reformatting return json; } return out.toString().trim(); } } }
private void writeArray(ServletResponse res,JsonArray a) throws IOException{ try(StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = Json.createWriter(stringWriter)){ jsonWriter.writeArray(a); res.getWriter().write(stringWriter.toString()); } catch (IOException ex) { log.log(Level.SEVERE, null, ex); } res.getWriter().flush(); }
public static String prettyPrint(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(); }
private String formatJson(JsonArray data) { StringWriter writer = new StringWriter(); JsonWriter gen = writerFactory.createWriter(writer); gen.writeArray(data); return writer.toString(); } }
public static void main(String[] args) { StringWriter s = new StringWriter(); JsonWriter writer = Json.createWriter(s); writer.writeObject(buildJSon()); System.out.println(s); }
public HttpResponse getMessage(int accessoryId, int iid, EventableCharacteristic changed) throws Exception { JsonArrayBuilder characteristics = Json.createArrayBuilder(); JsonObjectBuilder characteristicBuilder = Json.createObjectBuilder(); characteristicBuilder.add("aid", accessoryId); characteristicBuilder.add("iid", iid); changed.supplyValue(characteristicBuilder); characteristics.add(characteristicBuilder.build()); JsonObject data = Json.createObjectBuilder().add("characteristics", characteristics).build(); try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Json.createWriter(baos).write(data); byte[] dataBytes = baos.toByteArray(); return new EventResponse(dataBytes); } }
public static void apply(OutputStream out, K k) { try { DataOutputStream data = new DataOutputStream(out); JsonWriter jsonWriter = Json.createWriter(data); JsonObjectBuilder kterm = Json.createObjectBuilder(); kterm.add("format", "KAST"); kterm.add("version", 1); kterm.add("term", toJson(k)); jsonWriter.write(kterm.build()); jsonWriter.close(); data.close(); } catch (IOException e) { throw KEMException.criticalError("Could not write K term to Json", e, k); } }
/** * Print JSON. * @param src Source * @return JSON * @throws IOException If fails */ private static byte[] print(final RsJson.Source src) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (JsonWriter writer = Json.createWriter(baos)) { writer.write(src.toJson()); } return baos.toByteArray(); }
@Override public void writeTo(JsonStructure t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { if (entityStream == null) { throw new IOException("Initialized OutputStream should be provided"); } JsonWriter writer = null; try { writer = Json.createWriter(entityStream); writer.write(t); } finally { if (writer != null) { writer.close(); } } }
private static byte[] serialize(final JsonObject jsonObject) { try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) { final Map<String, ?> config = singletonMap(JsonGenerator.PRETTY_PRINTING, true); final JsonWriter jsonWriter = Json.createWriterFactory(config).createWriter(output); jsonWriter.write(jsonObject); jsonWriter.close(); return output.toByteArray(); } catch (IOException e) { throw new RuntimeException("Could not write Swagger output", e); } }
@Override protected void doWrite(final JsonWriter writer, final JsonStructure structure) { writer.write(structure); } }