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

The following examples show how to use com.dslplatform.json.JsonWriter#getByteBuffer() . 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: WriteDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public final void write(final JsonWriter writer, @Nullable final T instance) {
	if (instance == null) {
		writer.writeNull();
	} else if (alwaysSerialize) {
		writer.writeByte(JsonWriter.OBJECT_START);
		writeContentFull(writer, instance);
		writer.writeByte(JsonWriter.OBJECT_END);
	} else {
		writer.writeByte(JsonWriter.OBJECT_START);
		if (writeContentMinimal(writer, instance)) {
			writer.getByteBuffer()[writer.size() - 1] = JsonWriter.OBJECT_END;
		} else {
			writer.writeByte(JsonWriter.OBJECT_END);
		}
	}
}
 
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: BuilderTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testResolution() throws IOException {
	Immutable wo = new Immutable(100, 200);
	JsonWriter writer = json.newWriter();
	json.serialize(writer, wo);
	Assert.assertEquals("{\"x\":100,\"y\":200}", writer.toString());
	ByteArrayInputStream is = new ByteArrayInputStream(writer.getByteBuffer(), 0, writer.size());
	Immutable wo2 = json.deserialize(Immutable.class, is);
	Assert.assertEquals(wo.y, wo2.y);
	Assert.assertEquals(wo.x, wo2.x);
}