Java Code Examples for javax.json.Json#createWriterFactory()

The following examples show how to use javax.json.Json#createWriterFactory() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Create a JSON string.
 * @param structure the structure JsonAoject or JsonArray
 * @param pretty for pretty print
 * @return the JSON string
 */
public static String toJson(JsonStructure structure, boolean pretty) {
  JsonWriter writer  = null;
  StringWriter result = new StringWriter();
  try {
    Map<String, Object> props = new HashMap<String,Object>();
    if (pretty) props.put(JsonGenerator.PRETTY_PRINTING,true);
    JsonWriterFactory factory = Json.createWriterFactory(props);
    writer = factory.createWriter(result);
    writer.write(structure); 
  } finally {
    try {
      if (writer != null) writer.close();
    } catch (Throwable t) {
      t.printStackTrace(System.err);
    }
  }
  return result.toString().trim();
}
 
Example 2
Source Project: microprofile-graphql   File: PrintUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static String prettyJson(JsonObject jsonObject) {
    if(jsonObject!=null){
        StringWriter sw = new StringWriter();
        Map<String, Object> properties = new HashMap<>(1);
        properties.put(JsonGenerator.PRETTY_PRINTING, true);

        JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
        try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
            jsonWriter.writeObject(jsonObject);
        }
        return sw.toString();
    }
    return null;
}
 
Example 3
Source Project: FHIR   File: ResourceProcessor.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(null);
    JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(null);
    JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(null);

    File dir = new File("src/main/resources/hl7/fhir/us/davinci-pdex-plan-net/package/");
    for (File file : dir.listFiles()) {
        String fileName = file.getName();
        if (!fileName.endsWith(".json") || file.isDirectory()) {
            continue;
        }
        JsonObject jsonObject = null;
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            JsonReader jsonReader = jsonReaderFactory.createReader(reader);
            jsonObject = jsonReader.readObject();

            JsonObjectBuilder jsonObjectBuilder = jsonBuilderFactory.createObjectBuilder(jsonObject);

            JsonObject text = jsonObject.getJsonObject("text");
            if (text != null) {
                JsonObjectBuilder textBuilder = jsonBuilderFactory.createObjectBuilder(text);
                String div = text.getString("div");
                div = div.replace("<p><pre>", "<pre>").replace("</pre></p>", "</pre>");
                textBuilder.add("div", div);
                jsonObjectBuilder.add("text", textBuilder);
            }

            if (!jsonObject.containsKey("version")) {
                System.out.println("file: " + file + " does not have a version");
                jsonObjectBuilder.add("version", "0.1.0");
            }

            jsonObject = jsonObjectBuilder.build();
        }
        try (FileWriter writer = new FileWriter(file)) {
            JsonWriter jsonWriter = jsonWriterFactory.createWriter(writer);
            jsonWriter.write(jsonObject);
        }
    }
}
 
Example 4
private String getPrettyJson(JsonObject jsonObject) {

        JsonWriterFactory writerFactory = Json.createWriterFactory(JSON_PROPERTIES);

        try (StringWriter sw = new StringWriter();
                JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
            jsonWriter.writeObject(jsonObject);
            return sw.toString();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }
 
Example 5
Source Project: smallrye-graphql   File: ExecutionTest.java    License: Apache License 2.0 5 votes vote down vote up
private String getPrettyJson(JsonObject jsonObject) {

        JsonWriterFactory writerFactory = Json.createWriterFactory(JSON_PROPERTIES);

        try (StringWriter sw = new StringWriter();
                JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
            jsonWriter.writeObject(jsonObject);
            return sw.toString();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }
 
Example 6
Source Project: tcases   File: GeneratorSetJsonWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given system test definition the form of a JSON document.
 */
public void write( IGeneratorSet generatorSet)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  jsonWriter.write( GeneratorSetJson.toJson( generatorSet));
  }
 
Example 7
Source Project: tcases   File: SystemInputJsonWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given system test definition the form of a JSON document.
 */
public void write( SystemInputDef systemInput)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  jsonWriter.write( SystemInputJson.toJson( systemInput));
  }
 
Example 8
Source Project: tcases   File: ProjectJsonWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given project definition the form of a JSON document.
 */
public void write( Project project)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  jsonWriter.write( ProjectJson.toJson( project));
  }
 
Example 9
Source Project: tcases   File: SystemTestJsonWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given system test definition the form of a JSON document.
 */
public void write( SystemTestDef systemTest)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  jsonWriter.write( SystemTestJson.toJson( systemTest));
  }
 
Example 10
Source Project: tcases   File: MocoServerConfigWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given system test definition the form of a JSON document.
 */
public void write( RequestTestDef requestCases)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  try
    {
    jsonWriter.write( expectedConfigs( requestCases));
    }
  catch( Exception e)
    {
    throw new RequestCaseException( String.format( "Can't write Moco server configuration for %s", requestCases), e);
    }
  }
 
Example 11
Source Project: tcases   File: MocoTestConfigWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given request cases in the form of a JSON document.
 */
public void write( MocoTestConfig mocoTestConfig)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  jsonWriter.write( MocoTestConfigJson.toJson( mocoTestConfig));
  }
 
Example 12
Source Project: tcases   File: RequestTestDefWriter.java    License: MIT License 5 votes vote down vote up
/**
 * Writes the given request cases in the form of a JSON document.
 */
public void write( RequestTestDef requestTestDef)
  {
  JsonWriterFactory writerFactory = Json.createWriterFactory( MapBuilder.of( PRETTY_PRINTING, true).build());
  JsonWriter jsonWriter = writerFactory.createWriter( getWriter());

  jsonWriter.write( RequestCaseJson.toJson( requestTestDef));
  }
 
Example 13
Source Project: logbook-kai   File: ApiStart2.java    License: MIT License 5 votes vote down vote up
/**
 * store
 * 
 * @param obj api_data
 */
private void store(JsonObject root) {
    if (AppConfig.get().isStoreApiStart2()) {
        try {
            String dir = AppConfig.get().getStoreApiStart2Dir();
            if (dir == null || "".equals(dir))
                return;

            Path dirPath = Paths.get(dir);
            Path parent = dirPath.getParent();
            if (parent != null && !Files.exists(parent)) {
                Files.createDirectories(parent);
            }

            JsonWriterFactory factory = Json
                    .createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
            for (Entry<String, JsonValue> entry : root.entrySet()) {
                String key = entry.getKey();
                JsonValue val = entry.getValue();
                JsonObject obj = Json.createObjectBuilder().add(key, val).build();

                Path outPath = dirPath.resolve(key + ".json");

                try (OutputStream out = Files.newOutputStream(outPath)) {
                    try (JsonWriter writer = factory.createWriter(out)) {
                        writer.write(obj);
                    }
                }
            }
        } catch (Exception e) {
            LoggerHolder.get().warn("api_start2の保存に失敗しました", e);
        }
    }
}
 
Example 14
Source Project: iaf   File: ApiListenerServlet.java    License: Apache License 2.0 5 votes vote down vote up
public void returnJson(HttpServletResponse response, int status, JsonObject json) throws IOException {
	response.setStatus(status);
	Map<String, Boolean> config = new HashMap<>();
	config.put(JsonGenerator.PRETTY_PRINTING, true);
	JsonWriterFactory factory = Json.createWriterFactory(config);
	try (JsonWriter jsonWriter = factory.createWriter(response.getOutputStream(), Charset.forName("UTF-8"))) {
		jsonWriter.write(json);
	}
}
 
Example 15
Source Project: iaf   File: TestXmlSchema2JsonSchema.java    License: Apache License 2.0 5 votes vote down vote up
private String jsonPrettyPrint(String json) {
	StringWriter sw = new StringWriter();
	JsonReader jr = Json.createReader(new StringReader(json));
	JsonObject jobj = jr.readObject();

	Map<String, Object> properties = new HashMap<>(1);
	properties.put(JsonGenerator.PRETTY_PRINTING, true);

	JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
	try (JsonWriter jsonWriter = writerFactory.createWriter(sw)) {
		jsonWriter.writeObject(jobj);
	}

	return sw.toString().trim();
}
 
Example 16
Source Project: jcypher   File: Util.java    License: Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 17
public DbService(DbRepository repository) {
    this.repository = repository;

    this.jsonWriterFactory = Json.createWriterFactory(null);
    this.jsonBuilderFactory = Json.createBuilderFactory(Collections.emptyMap());
}