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

The following examples show how to use javax.json.Json#createWriterFactory() . 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
Source File: JsonUtil.java    From geoportal-server-harvester with Apache License 2.0 6 votes vote down vote up
/**
 * 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 File: SystemTestJsonWriter.java    From tcases with 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 3
Source File: TestXmlSchema2JsonSchema.java    From iaf with 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 4
Source File: ApiListenerServlet.java    From iaf with 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 5
Source File: ApiStart2.java    From logbook-kai with 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 6
Source File: RequestTestDefWriter.java    From tcases with 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 7
Source File: MocoTestConfigWriter.java    From tcases with 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 8
Source File: MocoServerConfigWriter.java    From tcases with 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 9
Source File: PrintUtil.java    From microprofile-graphql with 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 10
Source File: ProjectJsonWriter.java    From tcases with 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 11
Source File: SystemInputJsonWriter.java    From tcases with 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 12
Source File: GeneratorSetJsonWriter.java    From tcases with 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 13
Source File: ExecutionTest.java    From smallrye-graphql with 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 14
Source File: CdiExecutionTest.java    From smallrye-graphql with 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 15
Source File: ResourceProcessor.java    From FHIR with 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 16
Source File: Util.java    From jcypher with 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
Source File: DbService.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public DbService(DbRepository repository) {
    this.repository = repository;

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