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);
}
}
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();
}
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));
}
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();
}
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);
}
}
Aggregations