Headline

Processing JSON with the Jackson DataBinding API using a POJO representation

Characteristics

This contribution process a company saved in a JSON-file to a company object. It performs the Features Total , Cut ,Parsing and Unparsing, to demonstrate how to use a POJO parsed from a JSON file.

Illustration

This contribution uses a ObjectMapper for parsing and unparsing. The mapper provides methods to easily map between most kinds of Java object and a JSON file. In this case the mapper maps the JSON directly to a POJO company.

public static Company parse(File file) {
    ObjectMapper mapper = new ObjectMapper();
    Company company = null;
    try {
        company = mapper.readValue(file, Company.class);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return company;
}
The class Unparsing used a ObjectWriter to prettyprint the company to its JSON representation.
    ObjectMapper mapper = new ObjectMapper();
    PrettyPrinter printer = new DefaultPrettyPrinter();
    ObjectWriter writer = mapper.writer(printer);
The cut and total method are implemented without further use of the Jackson API.
public static double total(Company c) {
    double result = 0.0;
    if (c.getDepartments() != null)
        for (Department d : c.getDepartments())
            result += total(d);
    return result;
}

Usage

Use Gradle to build this contribution. For running and editing this contribution supports Eclipse.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.