Search in sources :

Example 6 with JsonFactory

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);
    }
}
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 JsonFactory

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

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);
}
Also used : HttpServerResponse(io.reactivex.netty.protocol.http.server.HttpServerResponse) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RxNetty(io.reactivex.netty.RxNetty) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BackendResponse(perf.test.utils.BackendResponse) ServiceResponseBuilder(perf.test.utils.ServiceResponseBuilder) JsonFactory(org.codehaus.jackson.JsonFactory) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) LogLevel(io.netty.handler.logging.LogLevel) JsonParseException(perf.test.utils.JsonParseException) HttpServerRequest(io.reactivex.netty.protocol.http.server.HttpServerRequest) Map(java.util.Map) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) HttpClientBuilder(io.reactivex.netty.protocol.http.client.HttpClientBuilder) BackendResponse(perf.test.utils.BackendResponse) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Observable(rx.Observable) JsonParseException(perf.test.utils.JsonParseException)

Example 9 with JsonFactory

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));
}
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 10 with JsonFactory

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;
}
Also used : ArrayList(java.util.ArrayList) JsonFactory(org.codehaus.jackson.JsonFactory) StringReader(java.io.StringReader) List(java.util.List) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

JsonFactory (org.codehaus.jackson.JsonFactory)38 JsonGenerator (org.codehaus.jackson.JsonGenerator)15 JsonParser (org.codehaus.jackson.JsonParser)13 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)13 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 File (java.io.File)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 GenericRecord (org.apache.avro.generic.GenericRecord)4 JsonNode (org.codehaus.jackson.JsonNode)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 BufferedWriter (java.io.BufferedWriter)2 DataOutputStream (java.io.DataOutputStream)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Date (java.util.Date)2 List (java.util.List)2