Search in sources :

Example 1 with EncodeException

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);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Instant(java.time.Instant) JsonObject(io.vertx.core.json.JsonObject) EncodeException(io.vertx.core.json.EncodeException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) JsonArray(io.vertx.core.json.JsonArray) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(io.vertx.core.json.JsonObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with EncodeException

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);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) EncodeException(io.vertx.core.json.EncodeException) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with EncodeException

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);
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) EncodeException(io.vertx.core.json.EncodeException) IOException(java.io.IOException)

Aggregations

EncodeException (io.vertx.core.json.EncodeException)3 IOException (java.io.IOException)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 StringWriter (java.io.StringWriter)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1