Java Code Examples for com.dslplatform.json.JsonWriter#reset()

The following examples show how to use com.dslplatform.json.JsonWriter#reset() . 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: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void toJson(Object obj, Type type, OutputStream stream) throws JsonbException {
	if (type == null) throw new JsonbException("type can't be null");
	if (stream == null) throw new JsonbException("stream can't be null");
	JsonWriter jw = localWriter.get();
	try {
		jw.reset(stream);
		if (!dslJson.serialize(jw, type, obj)) {
			throw new JsonbException("Unable to serialize provided " + type);
		}
		jw.flush();
	} catch (SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	} finally {
		jw.reset(null);
	}
}
 
Example 2
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toJson(Object obj) throws JsonbException {
	try {
		JsonWriter writer = localWriter.get();
		writer.reset();
		dslJson.serialize(writer, obj);
		return new String(writer.getByteBuffer(), 0, writer.size(), "UTF-8");
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example 3
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toJson(Object obj, Type type) throws JsonbException {
	if (type == null) throw new JsonbException("type can't be null");
	try {
		JsonWriter writer = localWriter.get();
		writer.reset();
		if (!dslJson.serialize(writer, type, obj)) {
			throw new JsonbException("Unable to serialize provided " + type);
		}
		return new String(writer.getByteBuffer(), 0, writer.size(), "UTF-8");
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example 4
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void toJson(Object obj, Writer writer) throws JsonbException {
	if (writer == null) throw new JsonbException("writer can't be null");
	try {
		JsonWriter jw = localWriter.get();
		jw.reset();
		dslJson.serialize(jw, obj);
		//TODO: not ideal... but lets use it instead of throwing an exception
		writer.write(new String(jw.getByteBuffer(), 0, jw.size(), "UTF-8"));
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example 5
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void toJson(Object obj, Type type, Writer writer) throws JsonbException {
	if (type == null) throw new JsonbException("type can't be null");
	if (writer == null) throw new JsonbException("writer can't be null");
	try {
		JsonWriter jw = localWriter.get();
		jw.reset();
		if (!dslJson.serialize(jw, type, obj)) {
			throw new JsonbException("Unable to serialize provided " + type);
		}
		writer.write(new String(jw.getByteBuffer(), 0, jw.size(), "UTF-8"));
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example 6
Source File: ApmServerLogShipper.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
private void writeFileMetadata(OutputStream os, File file) throws IOException {
    JsonWriter jw = payloadSerializer.getJsonWriter();
    jw.reset();
    payloadSerializer.serializeFileMetaData(file);
    os.write(jw.getByteBuffer(), 0, jw.size());
}
 
Example 7
Source File: Utils.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static JsonWriter getJson() {
	JsonWriter json = threadJson.get();
	json.reset();
	return json;
}