Search in sources :

Example 16 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project stargate-core by tuplejump.

the class Group method toByteBuffer.

public ByteBuffer toByteBuffer() throws IOException {
    BufferRecycler bufferRecycler = bufferThreadLocal.get();
    ByteArrayBuilder bytes = new ByteArrayBuilder(bufferRecycler);
    IOContext ioContext = new IOContext(bufferRecycler, bytes, false);
    JsonGenerator gen = new Utf8Generator(ioContext, 0, null, bytes);
    gen.enable(JsonGenerator.Feature.QUOTE_FIELD_NAMES);
    gen.enable(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS);
    gen.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
    writeJson(gen);
    gen.flush();
    bytes.flush();
    bytes.close();
    return ByteBuffer.wrap(bytes.toByteArray());
}
Also used : BufferRecycler(org.codehaus.jackson.util.BufferRecycler) IOContext(org.codehaus.jackson.io.IOContext) JsonGenerator(org.codehaus.jackson.JsonGenerator) ByteArrayBuilder(org.codehaus.jackson.util.ByteArrayBuilder) Utf8Generator(org.codehaus.jackson.impl.Utf8Generator)

Example 17 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project YCSB by brianfrankcooper.

the class MemcachedClient method toJson.

protected static String toJson(Map<String, ByteIterator> values) throws IOException {
    ObjectNode node = MAPPER.createObjectNode();
    HashMap<String, String> stringMap = StringByteIterator.getStringMap(values);
    for (Map.Entry<String, String> pair : stringMap.entrySet()) {
        node.put(pair.getKey(), pair.getValue());
    }
    JsonFactory jsonFactory = new JsonFactory();
    Writer writer = new StringWriter();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
    MAPPER.writeTree(jsonGenerator, node);
    return writer.toString();
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) StringWriter(java.io.StringWriter) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) HashMap(java.util.HashMap) Map(java.util.Map) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 18 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project perun by CESNET.

the class JsonSerializer method writePerunException.

@Override
public void writePerunException(PerunException pex) throws IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    if (pex == null) {
        throw new IllegalArgumentException("pex is null");
    } else {
        gen.writeObject(pex);
        gen.flush();
    }
    gen.close();
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator)

Example 19 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project perun by CESNET.

the class JsonSerializer method write.

@Override
public void write(Object object) throws RpcException, IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    try {
        gen.writeObject(object);
        gen.flush();
        gen.close();
    } catch (JsonProcessingException ex) {
        throw new RpcException(RpcException.Type.CANNOT_SERIALIZE_VALUE, ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 20 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project perun by CESNET.

the class JsonSerializer method write.

@Override
public void write(Object object) throws RpcException, IOException {
    JsonGenerator gen = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    if (object instanceof Throwable) {
        throw new IllegalArgumentException("Tried to serialize a throwable object using write()", (Throwable) object);
    }
    try {
        gen.writeObject(object);
        gen.close();
    } catch (JsonProcessingException ex) {
        throw new RpcException(RpcException.Type.CANNOT_SERIALIZE_VALUE, ex);
    }
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Aggregations

JsonGenerator (org.codehaus.jackson.JsonGenerator)52 JsonFactory (org.codehaus.jackson.JsonFactory)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IOException (java.io.IOException)11 StringWriter (java.io.StringWriter)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)8 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)6 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)5 JsonNode (org.codehaus.jackson.JsonNode)5 OutputStreamWriter (java.io.OutputStreamWriter)4 File (java.io.File)3 HashMap (java.util.HashMap)3 GET (javax.ws.rs.GET)3 Response (javax.ws.rs.core.Response)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BufferedWriter (java.io.BufferedWriter)2 DataOutputStream (java.io.DataOutputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2