use of io.vertx.core.json.EncodeException in project vert.x by eclipse.
the class JacksonCodec method encodeJson.
// In recursive calls, the callee is in charge of opening and closing the data structure
private static void encodeJson(Object json, JsonGenerator generator) throws EncodeException {
try {
if (json instanceof JsonObject) {
json = ((JsonObject) json).getMap();
} else if (json instanceof JsonArray) {
json = ((JsonArray) json).getList();
}
if (json instanceof Map) {
generator.writeStartObject();
for (Map.Entry<String, ?> e : ((Map<String, ?>) json).entrySet()) {
generator.writeFieldName(e.getKey());
encodeJson(e.getValue(), generator);
}
generator.writeEndObject();
} else if (json instanceof List) {
generator.writeStartArray();
for (Object item : (List<?>) json) {
encodeJson(item, generator);
}
generator.writeEndArray();
} else if (json instanceof String) {
generator.writeString((String) json);
} else if (json instanceof Number) {
if (json instanceof Short) {
generator.writeNumber((Short) json);
} else if (json instanceof Integer) {
generator.writeNumber((Integer) json);
} else if (json instanceof Long) {
generator.writeNumber((Long) json);
} else if (json instanceof Float) {
generator.writeNumber((Float) json);
} else if (json instanceof Double) {
generator.writeNumber((Double) json);
} else if (json instanceof Byte) {
generator.writeNumber((Byte) json);
} else if (json instanceof BigInteger) {
generator.writeNumber((BigInteger) json);
} else if (json instanceof BigDecimal) {
generator.writeNumber((BigDecimal) json);
} else {
generator.writeNumber(((Number) json).doubleValue());
}
} else if (json instanceof Boolean) {
generator.writeBoolean((Boolean) json);
} else if (json instanceof Instant) {
// RFC-7493
generator.writeString((ISO_INSTANT.format((Instant) json)));
} else if (json instanceof byte[]) {
// RFC-7493
generator.writeString(BASE64_ENCODER.encodeToString((byte[]) json));
} else if (json instanceof Buffer) {
// RFC-7493
generator.writeString(BASE64_ENCODER.encodeToString(((Buffer) json).getBytes()));
} else if (json instanceof Enum) {
// vert.x extra (non standard but allowed conversion)
generator.writeString(((Enum<?>) json).name());
} else if (json == null) {
generator.writeNull();
} else {
throw new EncodeException("Mapping " + json.getClass().getName() + " is not available without Jackson Databind on the classpath");
}
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
}
}
use of io.vertx.core.json.EncodeException in project vert.x by eclipse.
the class JacksonCodec method toBuffer.
@Override
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
ByteBuf buf = Unpooled.buffer();
// There is no need to use a try with resources here as jackson
// is a well-behaved and always calls the closes all streams in the
// "finally" block bellow.
ByteBufOutputStream out = new ByteBufOutputStream(buf);
JsonGenerator generator = createGenerator(out, pretty);
try {
encodeJson(object, generator);
generator.flush();
return Buffer.buffer(buf);
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
} finally {
close(generator);
}
}
use of io.vertx.core.json.EncodeException in project vert.x by eclipse.
the class JacksonCodec method toString.
@Override
public String toString(Object object, boolean pretty) throws EncodeException {
StringWriter sw = new StringWriter();
JsonGenerator generator = createGenerator(sw, pretty);
try {
encodeJson(object, generator);
generator.flush();
return sw.toString();
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
} finally {
close(generator);
}
}
Aggregations