Search in sources :

Example 51 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project neo4j by neo4j.

the class GraphExtractionWriterTest method write.

private JsonNode write(Map<String, Object> row) throws IOException, JsonParseException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator json = jsonFactory.createJsonGenerator(out);
    json.writeStartObject();
    try {
        new GraphExtractionWriter().write(json, row.keySet(), new MapRow(row), checker);
    } finally {
        json.writeEndObject();
        json.flush();
    }
    return JsonHelper.jsonNode(out.toString(UTF_8.name()));
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 52 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project neo4j by neo4j.

the class RowWriterTests method shouldWriteNestedMaps.

@Test
public void shouldWriteNestedMaps() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator json = new JsonFactory(new Neo4jJsonCodec()).createJsonGenerator(out);
    JsonNode row = serialize(out, json, new RowWriter());
    MatcherAssert.assertThat(row.size(), equalTo(1));
    JsonNode firstCell = row.get(0);
    MatcherAssert.assertThat(firstCell.get("one").get("two").size(), is(2));
    MatcherAssert.assertThat(firstCell.get("one").get("two").get(0).asBoolean(), is(true));
    MatcherAssert.assertThat(firstCell.get("one").get("two").get(1).get("three").asInt(), is(42));
}
Also used : JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 53 with JsonGenerator

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

the class AnalyzedTextSerializer method serialize.

/**
     * Serializes the parsed {@link AnalysedText} to the {@link OutputStream} by
     * using the {@link Charset}.
     * @param at the {@link AnalysedText} to serialize
     * @param out the {@link OutputStream} 
     * @param charset the {@link Charset}. UTF-8 is used as default if <code>null</code>
     * is parsed
     */
public void serialize(AnalysedText at, OutputStream out, Charset charset) throws IOException {
    if (at == null) {
        throw new IllegalArgumentException("The parsed AnalysedText MUST NOT be NULL!");
    }
    if (out == null) {
        throw new IllegalArgumentException("The parsed OutputStream MUST NOT be NULL");
    }
    if (charset == null) {
        charset = UTF8;
    }
    JsonFactory jsonFactory = mapper.getJsonFactory();
    JsonGenerator jg = jsonFactory.createJsonGenerator(new OutputStreamWriter(out, charset));
    jg.useDefaultPrettyPrinter();
    jg.writeStartObject();
    jg.writeArrayFieldStart("spans");
    jg.writeTree(writeSpan(at));
    for (Iterator<Span> it = at.getEnclosed(EnumSet.allOf(SpanTypeEnum.class)); it.hasNext(); ) {
        jg.writeTree(writeSpan(it.next()));
    }
    jg.writeEndArray();
    jg.writeEndObject();
    jg.close();
}
Also used : SpanTypeEnum(org.apache.stanbol.enhancer.nlp.model.SpanTypeEnum) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) Span(org.apache.stanbol.enhancer.nlp.model.Span)

Example 54 with JsonGenerator

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

the class JsonMetaDataFormatter method showErrors.

@Override
public void showErrors(DataOutputStream out, WMValidateResourcePlanResponse response) throws HiveException {
    JsonGenerator generator = null;
    try {
        generator = new ObjectMapper().getJsonFactory().createJsonGenerator(out);
        generator.writeStartObject();
        generator.writeArrayFieldStart("errors");
        for (String error : response.getErrors()) {
            generator.writeString(error);
        }
        generator.writeEndArray();
        generator.writeArrayFieldStart("warnings");
        for (String error : response.getWarnings()) {
            generator.writeString(error);
        }
        generator.writeEndArray();
        generator.writeEndObject();
    } catch (IOException e) {
        throw new HiveException(e);
    } finally {
        if (generator != null) {
            IOUtils.closeQuietly(generator);
        }
    }
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

JsonGenerator (org.codehaus.jackson.JsonGenerator)54 JsonFactory (org.codehaus.jackson.JsonFactory)15 IOException (java.io.IOException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)11 StringWriter (java.io.StringWriter)10 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 HashMap (java.util.HashMap)4 File (java.io.File)3 PrintWriter (java.io.PrintWriter)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 ArrayList (java.util.ArrayList)2