Java Code Examples for com.google.gson.Gson#newJsonWriter()

The following examples show how to use com.google.gson.Gson#newJsonWriter() . 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: Exporter.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void run(Writer writer) throws IOException {
    final Gson gson = buildGson();
    final JsonWriter out = gson.newJsonWriter(writer);

    final DirCollectHistory src = new DirCollectHistory(getDirPath());

    out.beginArray();
    try {
        final Iterator<TimeSeriesCollection> tsdata_iter = src.stream().iterator();
        while (tsdata_iter.hasNext()) {
            final TimeSeriesCollection tsdata = tsdata_iter.next();
            Json.toJson(gson, out, tsdata);
        }
    } finally {
        out.endArray();
    }
}
 
Example 2
Source File: JsonOutputFormat.java    From buildnumber-maven-plugin with MIT License 5 votes vote down vote up
@Override
public void write( Properties props, OutputStream out )
    throws IOException
{
    Gson gson = new Gson();
    JsonWriter jsonWriter = gson.newJsonWriter( new OutputStreamWriter( out, "UTF-8" ) );
    jsonWriter.beginObject();
    for ( Object key : props.keySet() )
    {
        jsonWriter.name( (String) key );
        jsonWriter.value( props.getProperty( (String) key ) );
    }
    jsonWriter.endObject();
    jsonWriter.flush();
}
 
Example 3
Source File: BufferGeometry.java    From BlueMap with MIT License 4 votes vote down vote up
public String toJson() {
	try {

		StringWriter sw = new StringWriter();
		Gson gson = new GsonBuilder().create();
		JsonWriter json = gson.newJsonWriter(sw);

		json.beginObject(); // main-object

		// set special values
		json.name("type").value("BufferGeometry");
		json.name("uuid").value(UUID.randomUUID().toString().toUpperCase());

		json.name("data").beginObject(); // data
		json.name("attributes").beginObject(); // attributes

		for (Entry<String, BufferAttribute> entry : attributes.entrySet()) {
			json.name(entry.getKey());
			entry.getValue().writeJson(json);
		}

		json.endObject(); // attributes

		json.name("groups").beginArray(); // groups

		// write groups into json
		for (MaterialGroup g : groups) {
			json.beginObject();

			json.name("materialIndex").value(g.getMaterialIndex());
			json.name("start").value(g.getStart());
			json.name("count").value(g.getCount());

			json.endObject();
		}

		json.endArray(); // groups
		json.endObject(); // data
		json.endObject(); // main-object

		// save and return
		json.flush();
		return sw.toString();

	} catch (IOException e) {
		// since we are using a StringWriter there should never be an IO exception
		// thrown
		throw new RuntimeException(e);
	}
}