use of org.codehaus.jackson.JsonFactory 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.JsonFactory 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.JsonFactory in project WSPerfLab by Netflix-Skunkworks.
the class TestRouteBasic method handle.
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
long startTime = System.currentTimeMillis();
List<String> _id = request.getQueryParameters().get("id");
if (_id == null || _id.size() != 1) {
return writeError(request, response, "Please provide a numerical 'id' value. It can be a random number (uuid).");
}
long id = Long.parseLong(String.valueOf(_id.get(0)));
Observable<List<BackendResponse>> acd = getDataFromBackend("/mock.json?numItems=2&itemSize=50&delay=50&id=" + id).doOnError(Throwable::printStackTrace).<// Eclipse 20140224-0627 can't infer without this type hint even though the Java 8 compiler can
List<BackendResponse>>flatMap(responseA -> {
Observable<BackendResponse> responseC = getDataFromBackend("/mock.json?numItems=1&itemSize=5000&delay=80&id=" + responseA.getResponseKey());
Observable<BackendResponse> responseD = getDataFromBackend("/mock.json?numItems=1&itemSize=1000&delay=1&id=" + responseA.getResponseKey());
return Observable.zip(Observable.just(responseA), responseC, responseD, Arrays::asList);
}).doOnError(Throwable::printStackTrace);
Observable<List<BackendResponse>> be = getDataFromBackend("/mock.json?numItems=25&itemSize=30&delay=150&id=" + id).<// Eclipse 20140224-0627 can't infer without this type hint even though the Java 8 compiler can
List<BackendResponse>>flatMap(responseB -> {
Observable<BackendResponse> responseE = getDataFromBackend("/mock.json?numItems=100&itemSize=30&delay=4&id=" + responseB.getResponseKey());
return Observable.zip(Observable.just(responseB), responseE, Arrays::asList);
}).doOnError(Throwable::printStackTrace);
return Observable.zip(acd, be, (_acd, _be) -> {
BackendResponse responseA = _acd.get(0);
BackendResponse responseB = _be.get(0);
BackendResponse responseC = _acd.get(1);
BackendResponse responseD = _acd.get(2);
BackendResponse responseE = _be.get(1);
return new BackendResponse[] { responseA, responseB, responseC, responseD, responseE };
}).flatMap(backendResponses -> {
try {
ByteArrayOutputStream responseStream = ServiceResponseBuilder.buildTestAResponse(jsonFactory, backendResponses);
response.getHeaders().addHeader("Content-Type", "application/json");
addResponseHeaders(response, startTime);
int contentLength = responseStream.size();
response.getHeaders().addHeader("Content-Length", contentLength);
return response.writeBytesAndFlush(responseStream.toByteArray());
} catch (Exception e) {
return writeError(request, response, "Failed: " + e.getMessage());
}
}).doOnError(Throwable::printStackTrace);
}
use of org.codehaus.jackson.JsonFactory 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.JsonFactory in project tdi-studio-se by Talend.
the class ExchangeUtils method parseJsonObject.
public static List parseJsonObject(String jsonContent, Class clazz) throws Exception {
// need factory for creating parser to use
List objList = new ArrayList();
// for 4.1.0 the is no json param on server ,so jsonContent is "wrong parameters for version"
if (!jsonContent.startsWith("[")) {
return objList;
}
JsonFactory jf = new JsonFactory();
JsonNode node = new ObjectMapper().reader().readTree(jf.createJsonParser(new StringReader(jsonContent)));
List<Object> list = new ObjectMapper().readValue(node.traverse(), List.class);
for (Object source : list) {
Object obj = clazz.newInstance();
BeanUtils.copyProperties(obj, source);
objList.add(obj);
}
return objList;
}
Aggregations