Example usage for com.fasterxml.jackson.databind ObjectMapper getFactory

List of usage examples for com.fasterxml.jackson.databind ObjectMapper getFactory

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper getFactory.

Prototype

@Override
public JsonFactory getFactory() 

Source Link

Document

Method that can be used to get hold of JsonFactory that this mapper uses if it needs to construct JsonParser s and/or JsonGenerator s.

Usage

From source file:com.amazon.feeds.SampleFeedGenerator.java

/**
 * The method for generating sample feeds.
 *
 * @param format The class containing the format specifications.
 * @param items The number of items to generate.
 * @param ext File extension./* w w w .ja  v a 2 s  . co m*/
 */
public void createSampleFeed(IFeedFormat format, int items, String ext) throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    mapper.getFactory().setCharacterEscapes(format.getEscapeRules());

    // create output file
    String out = format.getFeedFormat() + "-" + items + "." + ext;
    // TODO: add XML support

    File outFile = new File(SAMPLE_PATH, out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }

    // populate sample feed
    System.out.println("Generating " + items + (items == 1 ? " item" : " items") + " for "
            + format.getProvider() + " feed at " + outFile.getAbsolutePath());
    format.populate(items);

    // write JSON to file
    if (format.usePrettyPrint()) {

        DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("   ", DefaultIndenter.SYS_LF);
        DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
        printer.indentObjectsWith(indenter);
        printer.indentArraysWith(indenter);
        mapper.writer(printer).writeValue(outFile, format);
    } else {
        mapper.writeValue(outFile, format);
    }
}

From source file:org.dswarm.xsd2jsonschema.model.JSRoot.java

public void render(final ObjectMapper mapper, final File file, final JsonEncoding encoding) throws IOException {
    render(mapper.getFactory(), file, encoding);
}

From source file:ren.hankai.cordwood.jackson.DateTimeDeserializerTest.java

@Test
public void testDeserializeInvalidDate() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateTimeDeserializer des = new DateTimeDeserializer();
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"20180901 13:23|12\"}");

    String val = null;
    do {/*from  w  ww.  j a  v  a 2s .c  o  m*/
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    Assert.assertNull(date);
}

From source file:ren.hankai.cordwood.jackson.DateTimeDeserializerTest.java

@Test
public void testDeserialize() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateTimeDeserializer des = new DateTimeDeserializer();
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018-09-01 13:23:12\"}");

    String val = null;
    do {/*from   www.  j a va 2 s  .co m*/
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy-MM-dd HH:mm:ss");
    Assert.assertTrue(DateUtils.truncatedEquals(date, expDate, Calendar.SECOND));
}

From source file:ren.hankai.cordwood.jackson.DateDeserializerTest.java

@Test
public void testDeserializeWithCustomFormat() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateDeserializer des = new DateDeserializer("yyyy|MM|dd");
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018|09|01\"}");

    String val = null;
    do {/* www .j av  a  2  s. c  o m*/
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy|MM|dd");
    Assert.assertTrue(DateUtils.isSameDay(date, expDate));
}

From source file:org.oncoblocks.centromere.web.util.FilteringJackson2HttpMessageConverter.java

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    ObjectMapper objectMapper = getObjectMapper();
    JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(outputMessage.getBody());

    try {//from   w  w  w.j a va 2 s.  c  om

        if (this.prefixJson) {
            jsonGenerator.writeRaw(")]}', ");
        }

        if (object instanceof ResponseEnvelope) {

            ResponseEnvelope envelope = (ResponseEnvelope) object;
            Object entity = envelope.getEntity();
            Set<String> fieldSet = envelope.getFieldSet();
            Set<String> exclude = envelope.getExclude();
            FilterProvider filters = null;

            if (fieldSet != null && !fieldSet.isEmpty()) {
                if (entity instanceof ResourceSupport) {
                    fieldSet.add("content"); // Don't filter out the wrapped content.
                }
                filters = new SimpleFilterProvider()
                        .addFilter("fieldFilter", SimpleBeanPropertyFilter.filterOutAllExcept(fieldSet))
                        .setFailOnUnknownId(false);
            } else if (exclude != null && !exclude.isEmpty()) {
                filters = new SimpleFilterProvider()
                        .addFilter("fieldFilter", SimpleBeanPropertyFilter.serializeAllExcept(exclude))
                        .setFailOnUnknownId(false);
            } else {
                filters = new SimpleFilterProvider()
                        .addFilter("fieldFilter", SimpleBeanPropertyFilter.serializeAllExcept())
                        .setFailOnUnknownId(false);
            }

            objectMapper.setFilterProvider(filters);
            objectMapper.writeValue(jsonGenerator, entity);

        } else if (object == null) {
            jsonGenerator.writeNull();
        } else {
            FilterProvider filters = new SimpleFilterProvider().setFailOnUnknownId(false);
            objectMapper.setFilterProvider(filters);
            objectMapper.writeValue(jsonGenerator, object);
        }

    } catch (JsonProcessingException e) {
        e.printStackTrace();
        throw new HttpMessageNotWritableException("Could not write JSON: " + e.getMessage());
    }

}

From source file:org.n52.tamis.core.test.json.deserialize.ProcessesDeserializer_Test.java

/**
 * Parses the example document located at
 * "src/test/resources/extendedProcesses_example.json" and deserializes its
 * content into an instance of {@link Processes_Tamis}
 *//*  ww  w .ja v  a2 s.  co m*/
@Test
public void test() {
    try {

        input = this.getClass().getResourceAsStream(EXTENDED_PROCESSES_EXAMPLE_JSON);

        ObjectMapper objectMapper = new ObjectMapper();
        JsonFactory jsonFactory = objectMapper.getFactory();

        this.jsonParser = jsonFactory.createParser(input);

        Processes_Tamis processes_short = processesDeserializer.deserialize(jsonParser, null);

        /*
         * Assert that values of the intantiated Processes_Tamis object
         * match the expected parameters from the example document
         */
        Assert.assertNotNull(processes_short);

        List<ProcessDescription_forProcessList> processDescriptions = processes_short.getProcesses();

        Assert.assertEquals(2, processDescriptions.size());

        ProcessDescription_forProcessList firstProcessDescription = processDescriptions.get(0);
        ProcessDescription_forProcessList secondProcessDescription = processDescriptions.get(1);

        /*
         * assertions for first process description
         */
        // in the example document there was no "abstract" element, thus,
        // description should be equal to label
        Assert.assertEquals(firstProcessDescription.getLabel(), firstProcessDescription.getDescription());
        Assert.assertEquals("org.n52.tamis.algorithm.interpolation", firstProcessDescription.getId());
        Assert.assertEquals("TAMIS Interpolation Process", firstProcessDescription.getLabel());

        /*
         * assertions for second process description
         */
        Assert.assertEquals(secondProcessDescription.getLabel(), secondProcessDescription.getDescription());
        Assert.assertEquals("org.n52.tamis.algorithm.soakageregressionmodel", secondProcessDescription.getId());
        Assert.assertEquals("TAMIS Soakage Regression Model", secondProcessDescription.getLabel());

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.anrisoftware.simplerest.oanda.rest.NamedListParseResponse.java

private <T> JsonParser createParser(ObjectMapper mapper, HttpEntity entity)
        throws IOException, JsonParseException, JsonMappingException {
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(entity.getContent());
    return parser;
}

From source file:org.dswarm.xsd2jsonschema.model.JSRoot.java

public void render(final ObjectMapper mapper, final OutputStream out, final JsonEncoding encoding)
        throws IOException {
    render(mapper.getFactory(), out, encoding);

}

From source file:ren.hankai.cordwood.jackson.DateTimeDeserializerTest.java

@Test
public void testDeserializeWithCustomFormat() throws Exception {
    final ObjectMapper om = new ObjectMapper();
    final DateTimeDeserializer des = new DateTimeDeserializer("yyyy|MM|dd HH|mm|ss");
    final JsonParser jp = om.getFactory().createParser("{\"date\": \"2018|09|01 13|23|12\"}");

    String val = null;
    do {/* w  w  w . j  a va2 s. co m*/
        val = jp.nextTextValue();
    } while (val == null);

    final Date date = des.deserialize(jp, om.getDeserializationContext());
    final Date expDate = DateUtils.parseDate(jp.getText(), "yyyy|MM|dd HH|mm|ss");
    Assert.assertTrue(DateUtils.truncatedEquals(date, expDate, Calendar.SECOND));
}