Java Code Examples for javax.json.JsonWriter#write()

The following examples show how to use javax.json.JsonWriter#write() . 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: TargetDirectoryJsonStructureWriter.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private File writeStructure(final File targetDir, final String directory,
		final String fileName, JsonStructure structure) throws IOException {
	Validate.notNull(fileName);

	final File dir = new File(targetDir, directory);
	dir.mkdirs();
	final File file = new File(dir, fileName);
	final FileWriter fileWriter = new FileWriter(file);
	try {
		final JsonWriter jsonWriter = provider.createWriterFactory(
				Collections.singletonMap(JsonGenerator.PRETTY_PRINTING,
						Boolean.TRUE)).createWriter(fileWriter);
		jsonWriter.write(structure);

	} finally {
		try {
			fileWriter.close();
		} catch (IOException ignored) {
		}
	}
	return file;
}
 
Example 3
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 4
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 5
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 6
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 7
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 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: 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 10
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 11
Source File: CodeModelJsonStructureWriter.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private JTextFile createTextFile(String fileName,
		JsonStructure jsonStructure) throws IOException {
	Validate.notNull(fileName);
	final JTextFile textFile = new JTextFile(fileName);
	final StringWriter stringWriter = new StringWriter();
	final JsonWriter jsonWriter = provider.createWriterFactory(
			Collections.singletonMap(JsonGenerator.PRETTY_PRINTING,
					Boolean.TRUE)).createWriter(stringWriter);
	jsonWriter.write(jsonStructure);
	textFile.setContents(stringWriter.toString());
	return textFile;
}