Search in sources :

Example 6 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project databus by linkedin.

the class FieldToAvro method buildAvroSchema.

public String buildAvroSchema(String namespace, String topRecordAvroName, String topRecordDatabaseName, String[][] headers, TableTypeInfo topRecordTypeInfo) {
    if (namespace == null)
        throw new IllegalArgumentException("namespace should not be null.");
    if (topRecordAvroName == null)
        throw new IllegalArgumentException("topRecordAvroName should not be null.");
    if (topRecordDatabaseName == null)
        throw new IllegalArgumentException("topRecordDatabaseName should not be null.");
    if (topRecordTypeInfo == null)
        throw new IllegalArgumentException("topRecordTypeInfo should not be null.");
    FieldInfo fieldInfo = new FieldInfo(topRecordDatabaseName, topRecordTypeInfo, -1);
    Map<String, Object> field = fieldToAvro(fieldInfo, true);
    // Overwrite the name with the nice Java record name
    field.put("name", topRecordAvroName);
    // Add namespace
    field.put("namespace", namespace);
    // Add doc and serialize to JSON
    try {
        SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a zzz");
        field.put("doc", "Auto-generated Avro schema for " + topRecordDatabaseName + ". Generated at " + df.format(new Date(System.currentTimeMillis())));
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = new JsonFactory();
        StringWriter writer = new StringWriter();
        JsonGenerator jgen = factory.createJsonGenerator(writer);
        jgen.useDefaultPrettyPrinter();
        mapper.writeValue(jgen, field);
        return writer.getBuffer().toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : JsonFactory(org.codehaus.jackson.JsonFactory) Date(java.util.Date) StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 7 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project databus by linkedin.

the class InteractiveSchemaGenerator method filterUserFields.

private String filterUserFields(String schema) throws IOException, DatabusException {
    //No filtering is necessary, just return the same schema
    if (!_areFieldsFiltered) {
        return schema;
    }
    Schema avroSchema = Schema.parse(schema);
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    StringWriter writer = new StringWriter();
    JsonGenerator jgen = factory.createJsonGenerator(writer);
    jgen.useDefaultPrettyPrinter();
    @SuppressWarnings("checked") HashMap<String, Object> schemaMap = new ObjectMapper().readValue(schema, HashMap.class);
    @SuppressWarnings("checked") ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) schemaMap.get("fields");
    int i = 0;
    while (i < list.size()) {
        Schema.Field field = avroSchema.getField(list.get(i).get("name"));
        String dbFieldName;
        if (field.schema().getType() == Schema.Type.ARRAY) {
            throw new DatabusException("Field not supported for filtering");
        //TODO fix this
        /*
        String innerSchema = field.getProp("items");
        if(innerSchema == null)
          throw new DatabusException("Unable to the inner schema type of the array");
        Schema innerSchemaParsed = Schema.parse(innerSchema);
        dbFieldName = SchemaHelper.getMetaField(innerSchemaParsed, "dbFieldName");
        */
        } else
            dbFieldName = SchemaHelper.getMetaField(field, "dbFieldName");
        if (dbFieldName == null)
            throw new DatabusException("Unable to determine the dbFieldName from the meta information");
        if (!_userFields.contains(dbFieldName.toUpperCase(Locale.ENGLISH)))
            list.remove(i);
        else
            i++;
    }
    mapper.writeValue(jgen, schemaMap);
    return writer.getBuffer().toString();
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) JsonFactory(org.codehaus.jackson.JsonFactory) ArrayList(java.util.ArrayList) StringWriter(java.io.StringWriter) DatabusException(com.linkedin.databus2.core.DatabusException) JsonGenerator(org.codehaus.jackson.JsonGenerator) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 8 with JsonGenerator

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

the class RestRepresentationWriterTests method shouldWriteNestedMaps.

@Test
public void shouldWriteNestedMaps() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator json = new JsonFactory(new Neo4jJsonCodec()).createJsonGenerator(out);
    JsonNode rest = serialize(out, json, new RestRepresentationWriter(URI.create("localhost")));
    MatcherAssert.assertThat(rest.size(), equalTo(1));
    JsonNode firstCell = rest.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 9 with JsonGenerator

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

the class BatchOperations method readBody.

private String readBody(JsonParser jp) throws IOException {
    JsonNode node = mapper.readTree(jp);
    StringWriter out = new StringWriter();
    JsonGenerator gen = jsonFactory.createJsonGenerator(out);
    mapper.writeTree(gen, node);
    gen.flush();
    gen.close();
    return out.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonNode(org.codehaus.jackson.JsonNode)

Example 10 with JsonGenerator

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

the class JsonHelper method createJsonFrom.

public static String createJsonFrom(Object data) throws JsonBuildRuntimeException {
    try {
        StringWriter writer = new StringWriter();
        try {
            JsonGenerator generator = OBJECT_MAPPER.getJsonFactory().createJsonGenerator(writer).useDefaultPrettyPrinter();
            writeValue(generator, data);
        } finally {
            writer.close();
        }
        return writer.getBuffer().toString();
    } catch (IOException e) {
        throw new JsonBuildRuntimeException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException)

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