Search in sources :

Example 41 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project ma-core-public by infiniteautomation.

the class ExceptionCsvMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        CsvSchema schema;
        if (object instanceof AbstractRestV2Exception) {
            schema = this.restExceptionSchema;
            object = new CsvRestException((AbstractRestV2Exception) object);
        } else {
            schema = this.exceptionSchema;
        }
        writePrefix(generator, object);
        ObjectWriter objectWriter = this.objectMapper.writer().with(schema);
        objectWriter.writeValue(generator, object);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 42 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project repseqio by repseqio.

the class VDJCGeneTest method jsonTestNoCurrentLibrary1.

@Test
public void jsonTestNoCurrentLibrary1() throws Exception {
    VDJCLibrary library = VDJCLibraryRegistry.getDefaultLibrary("hs");
    VDJCGene gene = library.getSafe("TRBV12-3*00");
    ObjectWriter writer = GlobalObjectMappers.PRETTY.writerFor(VDJCGene.class);
    ObjectReader reader = GlobalObjectMappers.PRETTY.readerFor(VDJCGene.class);
    String str = writer.writeValueAsString(gene);
    assertTrue(str.endsWith("/TRBV12-3*00\""));
    Object geneDeserialized = reader.readValue(str);
    assertTrue(geneDeserialized == gene);
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Test(org.junit.Test)

Example 43 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project repseqio by repseqio.

the class GenerateClonesAction method go.

@Override
public void go(ActionHelper helper) throws Exception {
    GCloneModel model = GModels.getGCloneModelByName(params.getModelName());
    GCloneGenerator generator = model.create(new Well19937c(params.getSeed()), VDJCLibraryRegistry.getDefault());
    VDJCLibrary library = VDJCLibraryRegistry.getDefault().getLibrary(model.libraryId());
    try (BufferedOutputStream s = new BufferedOutputStream(params.getOutput().equals(".") ? System.out : new FileOutputStream(params.getOutput()), 128 * 1024)) {
        s.write(GlobalObjectMappers.toOneLine(model.libraryId()).getBytes());
        s.write('\n');
        ObjectWriter writer = GlobalObjectMappers.ONE_LINE.writerFor(new TypeReference<GClone>() {
        }).withAttribute(VDJCGene.JSON_CURRENT_LIBRARY_ATTRIBUTE_KEY, library);
        OUTER: for (int i = 0; i < params.numberOfClones; i++) {
            GClone clone = generator.sample();
            for (GGene g : clone.genes.values()) {
                NucleotideSequence cdr3 = g.getFeature(GeneFeature.CDR3);
                if (params.isInFrame())
                    if (cdr3.size() % 3 != 0) {
                        --i;
                        continue OUTER;
                    }
                if (params.isNoStops())
                    if (AminoAcidSequence.translateFromCenter(cdr3).containStops()) {
                        --i;
                        continue OUTER;
                    }
            }
            writer.writeValue(new CloseShieldOutputStream(s), clone);
            s.write('\n');
        }
    }
}
Also used : GCloneGenerator(io.repseq.gen.dist.GCloneGenerator) GGene(io.repseq.gen.GGene) FileOutputStream(java.io.FileOutputStream) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) VDJCLibrary(io.repseq.core.VDJCLibrary) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) GCloneModel(io.repseq.gen.dist.GCloneModel) Well19937c(org.apache.commons.math3.random.Well19937c) BufferedOutputStream(java.io.BufferedOutputStream) GClone(io.repseq.gen.GClone) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 44 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project repseqio by repseqio.

the class ExportCloneSequencesAction method parseExtractor.

public static DescriptionExtractor parseExtractor(String str, final VDJCLibrary library) {
    Matcher matcher = extractorPatternFeatureGene.matcher(str);
    if (matcher.matches())
        return new DescriptionExtractorSeq(GeneFeature.parse(matcher.group(2)), null, matcher.group(1).equals("AA"));
    matcher = extractorPatternFeatureClone.matcher(str);
    if (matcher.matches())
        return new DescriptionExtractorSeq(GeneFeature.parse(matcher.group(3)), matcher.group(2), matcher.group(1).equals("AA"));
    if (str.equalsIgnoreCase("JSONClone"))
        return new DescriptionExtractor() {

            final ObjectWriter writer = GlobalObjectMappers.ONE_LINE.writerFor(new TypeReference<GClone>() {
            }).withAttribute(VDJCGene.JSON_CURRENT_LIBRARY_ATTRIBUTE_KEY, library);

            @Override
            public String extract(GClone clone, GGene gene, String chain) {
                try {
                    return writer.writeValueAsString(clone);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    if (str.equalsIgnoreCase("JSONGene"))
        return new DescriptionExtractor() {

            final ObjectWriter writer = GlobalObjectMappers.ONE_LINE.writerFor(new TypeReference<GGene>() {
            }).withAttribute(VDJCGene.JSON_CURRENT_LIBRARY_ATTRIBUTE_KEY, library);

            @Override
            public String extract(GClone clone, GGene gene, String chain) {
                try {
                    return writer.writeValueAsString(gene);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    if (str.equalsIgnoreCase("chain"))
        return new DescriptionExtractor() {

            @Override
            public String extract(GClone clone, GGene gene, String chain) {
                return chain;
            }
        };
    matcher = extractorPatternField.matcher(str);
    if (matcher.matches()) {
        final boolean isGene = matcher.group(1).equalsIgnoreCase("Gene");
        final String fieldName = matcher.group(2);
        return new DescriptionExtractor() {

            final ObjectWriter writer = isGene ? GlobalObjectMappers.ONE_LINE.writerFor(new TypeReference<GGene>() {
            }).withAttribute(VDJCGene.JSON_CURRENT_LIBRARY_ATTRIBUTE_KEY, library) : GlobalObjectMappers.ONE_LINE.writerFor(new TypeReference<GClone>() {
            }).withAttribute(VDJCGene.JSON_CURRENT_LIBRARY_ATTRIBUTE_KEY, library);

            @Override
            public String extract(GClone clone, GGene gene, String chain) {
                try {
                    String str = writer.writeValueAsString(isGene ? gene : clone);
                    JsonNode tree = GlobalObjectMappers.ONE_LINE.readTree(str);
                    JsonNode targetNode = tree.get(fieldName);
                    return targetNode == null ? "" : targetNode.asText();
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }
    throw new IllegalArgumentException("Can't parse description extractor: " + str);
}
Also used : Matcher(java.util.regex.Matcher) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 45 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project drill by apache.

the class TestQueryProfiles method runQuery.

private int runQuery(QueryWrapper query) throws IOException {
    ObjectWriter writer = mapper.writerFor(QueryWrapper.class);
    String json = writer.writeValueAsString(query);
    String url = String.format("http://localhost:%d/query.json", portNumber);
    Request request = new Request.Builder().url(url).post(RequestBody.create(json, JSON_MEDIA_TYPE)).build();
    try (Response response = httpClient.newCall(request).execute()) {
        return response.code();
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)61 IOException (java.io.IOException)31 Test (org.junit.Test)30 File (java.io.File)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)15 ArrayList (java.util.ArrayList)12 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)11 JavaType (com.fasterxml.jackson.databind.JavaType)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 StringWriter (java.io.StringWriter)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6