Search in sources :

Example 26 with JsonGenerator

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

the class JsonSerializerGWT 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.writeStartObject();
        gen.writeStringField("errorId", pex.getErrorId());
        if (pex instanceof RpcException) {
            gen.writeStringField("type", ((RpcException) pex).getType());
            gen.writeStringField("errorInfo", ((RpcException) pex).getErrorInfo());
        } else {
            gen.writeStringField("type", pex.getClass().getSimpleName());
            gen.writeStringField("errorInfo", pex.getMessage());
        }
        // write reason param for this case
        if (pex instanceof ExtendMembershipException) {
            gen.writeStringField("reason", ((ExtendMembershipException) pex).getReason().toString());
        }
        gen.writeEndObject();
    }
    gen.close();
}
Also used : RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) JsonGenerator(org.codehaus.jackson.JsonGenerator) ExtendMembershipException(cz.metacentrum.perun.core.api.exceptions.ExtendMembershipException)

Example 27 with JsonGenerator

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

the class JsonSerializerJSONSIMPLE 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 28 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project apex-core by apache.

the class StramWebServices method init.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void init() {
    //clear content type
    httpResponse.setContentType(null);
    if (!initialized) {
        Map<Class<?>, Class<? extends StringCodec<?>>> codecs = dagManager.getApplicationAttributes().get(DAGContext.STRING_CODECS);
        StringCodecs.loadConverters(codecs);
        if (codecs != null) {
            SimpleModule sm = new SimpleModule("DTSerializationModule", new Version(1, 0, 0, null));
            for (Map.Entry<Class<?>, Class<? extends StringCodec<?>>> entry : codecs.entrySet()) {
                try {
                    final StringCodec<Object> codec = (StringCodec<Object>) entry.getValue().newInstance();
                    sm.addSerializer(new SerializerBase(entry.getKey()) {

                        @Override
                        public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
                            jgen.writeString(codec.toString(value));
                        }
                    });
                } catch (Exception ex) {
                    LOG.error("Caught exception when instantiating codec for class {}", entry.getKey().getName(), ex);
                }
            }
            objectMapper.registerModule(sm);
        }
        initialized = true;
    }
}
Also used : SerializerBase(org.codehaus.jackson.map.ser.std.SerializerBase) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JSONException(org.codehaus.jettison.json.JSONException) StringCodec(com.datatorrent.api.StringCodec) Version(org.codehaus.jackson.Version) JsonGenerator(org.codehaus.jackson.JsonGenerator) JSONObject(org.codehaus.jettison.json.JSONObject) SerializerProvider(org.codehaus.jackson.map.SerializerProvider) Map(java.util.Map) BeanMap(org.apache.commons.beanutils.BeanMap) HashMap(java.util.HashMap) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) SimpleModule(org.codehaus.jackson.map.module.SimpleModule)

Example 29 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project eiger by wlloyd.

the class LeveledManifest method serialize.

public synchronized void serialize() {
    File manifestFile = cfs.directories.getOrCreateLeveledManifest();
    File oldFile = new File(manifestFile.getPath().replace(EXTENSION, "-old.json"));
    File tmpFile = new File(manifestFile.getPath().replace(EXTENSION, "-tmp.json"));
    JsonFactory f = new JsonFactory();
    try {
        JsonGenerator g = f.createJsonGenerator(tmpFile, JsonEncoding.UTF8);
        g.useDefaultPrettyPrinter();
        g.writeStartObject();
        g.writeArrayFieldStart("generations");
        for (int level = 0; level < generations.length; level++) {
            g.writeStartObject();
            g.writeNumberField("generation", level);
            g.writeArrayFieldStart("members");
            for (SSTableReader ssTableReader : generations[level]) g.writeNumber(ssTableReader.descriptor.generation);
            // members
            g.writeEndArray();
            // generation
            g.writeEndObject();
        }
        // for field generations
        g.writeEndArray();
        // write global object
        g.writeEndObject();
        g.close();
        if (oldFile.exists() && manifestFile.exists())
            FileUtils.deleteWithConfirm(oldFile);
        if (manifestFile.exists())
            FileUtils.renameWithConfirm(manifestFile, oldFile);
        assert tmpFile.exists();
        FileUtils.renameWithConfirm(tmpFile, manifestFile);
        logger.debug("Saved manifest {}", manifestFile);
    } catch (IOException e) {
        throw new IOError(e);
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) IOError(java.io.IOError) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) File(java.io.File)

Example 30 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project cassandra by apache.

the class JsonTransformer method toJson.

public static void toJson(ISSTableScanner currentScanner, Stream<UnfilteredRowIterator> partitions, boolean rawTime, TableMetadata metadata, OutputStream out) throws IOException {
    try (JsonGenerator json = jsonFactory.createJsonGenerator(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
        JsonTransformer transformer = new JsonTransformer(json, currentScanner, rawTime, metadata);
        json.writeStartArray();
        partitions.forEach(transformer::serializePartition);
        json.writeEndArray();
    }
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter)

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