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

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

Introduction

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

Prototype

public ObjectMapper configure(JsonGenerator.Feature f, boolean state) 

Source Link

Document

Method for changing state of an on/off JsonGenerator feature for JsonFactory instance this object mapper uses.

Usage

From source file:com.asimihsan.handytrowel.cli.Main.java

public void doMain(String[] args)
        throws SAXException, CmdLineException, TimeoutException, BoilerpipeProcessingException, IOException {
    CmdLineParser parser = new CmdLineParser(this);
    parser.setUsageWidth(80);//from w w  w  .j  av  a  2s .c o m
    try {
        parser.parseArgument(args);
        if (arguments.isEmpty())
            throw new CmdLineException(parser, "No arguments were given");
    } catch (final CmdLineException e) {
        System.err.println(e.getMessage());
        System.err.println("handytrowel [URL]");
        parser.printUsage(System.err);
        System.err.println();
        throw e;
    }
    String url = arguments.get(0);

    HTMLFetcher htmlFetcher = new HTMLFetcherBuilder().timeoutMillis(30 * 10000).build();
    String pageSource = null;
    try {
        pageSource = htmlFetcher.getPageSource(url);
    } catch (final TimeoutException e) {
        e.printStackTrace();
        throw e;
    }

    String extractedBody = null;
    List<String> links = null;
    try {
        final HTMLDocument htmlDoc = new HTMLDocument(pageSource);
        final TextDocument doc = new BoilerpipeSAXInput(htmlDoc.toInputSource()).getTextDocument();
        ArticleExtractor.INSTANCE.process(doc);
        final InputSource is = htmlDoc.toInputSource();
        links = LinkExtractor.INSTANCE.process(doc, is);
        /*
         * working article sentences extractor
         * !!AI do I have to call this again, or can I piggy back on LinkExtractor?
         */
        extractedBody = ArticleExtractor.INSTANCE.getText(pageSource);

    } catch (BoilerpipeProcessingException e) {
        e.printStackTrace();
        throw e;
    }

    TextAnalyzer analyzer = new TextAnalyzerBuilder().body(extractedBody).build().analyze();
    List<String> tokens = analyzer.getTokens();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    Map<String, Object> articleData = new HashMap<>();
    articleData.put("extractedBody", extractedBody);
    articleData.put("links", links);
    articleData.put("tokens", tokens);
    try {
        mapper.writeValue(System.out, articleData);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
        throw e;
    } catch (JsonMappingException e) {
        e.printStackTrace();
        throw e;
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:uk.co.caprica.bootlace.BootlaceApplication.java

/**
 * Configure the Jackson JSON {@link ObjectMapper} bean.
 * <p>//from   w  ww  .  j a v  a 2  s. c om
 * This configuration optionally enables pretty-printing of the generated JSON.
 *
 * @return object-mapper component
 */
@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    logger.debug("prettyJson={}", prettyJson);
    if (prettyJson) {
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    }
    return objectMapper;
}

From source file:com.github.jknack.handlebars.Jackson2HelperTest.java

@Test
public void toJSONAliasViewExclusive() throws IOException {
    Handlebars handlebars = new Handlebars();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

    handlebars.registerHelper("@json", new Jackson2Helper(mapper).viewAlias("myView", Public.class));

    Template template = handlebars.compileInline("{{@json this view=\"myView\"}}");

    CharSequence result = template.apply(new Blog("First Post", "..."));

    assertEquals("{\"title\":\"First Post\"}", result);
}

From source file:com.github.jknack.handlebars.Jackson2HelperTest.java

@Test(expected = HandlebarsException.class)
public void jsonViewNotFound() throws IOException {
    Handlebars handlebars = new Handlebars();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

    handlebars.registerHelper("@json", new Jackson2Helper(mapper));

    Template template = handlebars.compileInline("{{@json this view=\"missing.ViewClass\"}}");

    CharSequence result = template.apply(new Blog("First Post", "..."));

    assertEquals("{\"title\":\"First Post\"}", result);
}

From source file:com.github.jknack.handlebars.Jackson2HelperTest.java

@Test
public void toJSONViewExclusive() throws IOException {
    Handlebars handlebars = new Handlebars();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

    handlebars.registerHelper("@json", new Jackson2Helper(mapper));

    Template template = handlebars//ww  w.jav  a2s  .co  m
            .compileInline("{{@json this view=\"com.github.jknack.handlebars.Blog$Views$Public\"}}");

    CharSequence result = template.apply(new Blog("First Post", "..."));

    assertEquals("{\"title\":\"First Post\"}", result);
}

From source file:org.wildfly.swarm.plugin.fractionlist.FractionListMojo.java

protected void generateJavascript(Set<FractionMetadata> fractions) {
    ObjectMapper mapper = new ObjectMapper();

    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);

    File outFile = new File(this.project.getBuild().getOutputDirectory(), "fraction-list.js");

    try (FileWriter writer = new FileWriter(outFile)) {

        writer.write("fractionList = ");
        writer.flush();/*  w  ww .jav  a2  s  . c  o m*/

        mapper.writeValue(writer, fractions);

        writer.write(";");
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    org.apache.maven.artifact.DefaultArtifact artifact = new org.apache.maven.artifact.DefaultArtifact(
            this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(), "compile", "js",
            "", new DefaultArtifactHandler("js"));

    artifact.setFile(outFile);
    this.project.addAttachedArtifact(artifact);
}

From source file:com.nestorledon.employeedirectory.config.ApplicationConfig.java

/**
 * Needed for Jackson2 to read HATEOAS/HAL JSON.
 * //from  ww  w  .j a  v  a  2 s  . c om
 * Example 1:
 * String resultBody = restTemplate.getForObject(link.getHref(), String.class);
 * Resource<EmployeeDTO> resource = objectMapper.readValue(resultBody, EmployeeDTO.class);
 * 
 * Example 2:
 * MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
 * converter.setObjectMapper(objectMapper);
 * restTemplate.setMessageConverters(Collections.<HttpMessageConverter<?>> singletonList(converter));
 * Resource<EmployeeDTO> resource = restTemplate.getForObject(link.getHref(), EmployeeDTO.class);
 * 
 * @see http://stackoverflow.com/questions/25812776/proper-way-to-convert-spring-hateoas-link-to-object/25922483#25922483
 * 
 * @return
 */
@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}

From source file:com.todo.backend.config.WebConfiguration.java

@Bean
@Primary//from  w  w  w  .j a  v  a 2  s  . c  om
public ObjectMapper objectMapper() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return objectMapper;
}

From source file:com.parworks.androidlibrary.response.ImageOverlayInfo.java

private OverlayConfiguration parseOverlayContent() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    OverlayConfiguration conf = null;/*  ww  w .  j  a v  a 2 s  .  co m*/
    try {
        conf = content == null ? new OverlayConfiguration()
                : mapper.readValue(content, OverlayConfiguration.class);
    } catch (IOException e) {
        // when failing to parse the overlay content,
        // generate an empty object and use default for everything
        Log.e(TAG, e.getMessage());
        conf = new OverlayConfiguration();
    }
    return conf;
}

From source file:com.liato.bankdroid.banking.banks.bitcoin.Bitcoin.java

public Urllib login() throws LoginException, BankException {
    urlopen = new Urllib(context);

    try {//w ww  .  j av  a  2  s.c  o  m
        String response = urlopen.open(API_URL + username);
        if (response == null || "".equals(response)) {
            throw new LoginException(res.getText(R.string.invalid_username_password).toString());
        }
        ObjectMapper vObjectMapper = new ObjectMapper();
        vObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        vObjectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
        BlockchainResponse r = vObjectMapper.readValue(urlopen.open(API_URL + username),
                BlockchainResponse.class);
        Account a = new Account("Bitcoin",
                new BigDecimal(r.getFinalBalance()).divide(BigDecimal.valueOf(100000000)), "1");
        a.setCurrency("BTC");
        accounts.add(a);
        setCurrency("BTC");
    } catch (JsonParseException e) {
        throw new BankException(res.getText(R.string.invalid_bitcoin_address).toString());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        throw new BankException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new BankException(e.getMessage());
    }
    return urlopen;
}