Difference between revisions of "Jackson examples"

From DarkWiki
Jump to: navigation, search
(Created page with "===Interface members=== <source lang="java"> package andy.jackson.demo.model.impl; import andy.jackson.demo.model.Person; import com.fasterxml.jackson.databind.annotation.Js...")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
===Interface members===
+
===Fields that are collections of interfaces===
 +
 
 +
Sometimes, an (imposed) interface demands that we return a collection of interfaces from a member. Consider a 'Family' which contains a number of 'Person' (people). As both Family and Person are interfaces, they have concrete implementations FamilyImpl and PersonImpl respectively. This example shows how you can instruct Jackson to deserialise JSON into the object model. As you can't instantiate a Person, you tell it to use the PersonImpl.
  
 
<source lang="java">
 
<source lang="java">
 
package andy.jackson.demo.model.impl;
 
package andy.jackson.demo.model.impl;
  
 +
import andy.jackson.demo.model.Family;
 
import andy.jackson.demo.model.Person;
 
import andy.jackson.demo.model.Person;
 
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Line 10: Line 13:
 
import java.util.List;
 
import java.util.List;
  
public class FamilyImpl implements andy.jackson.demo.model.Family {
+
public class FamilyImpl implements Family {
 +
    /*
 +
        As the interface (andy.jackson.demo.model.Family) requires we emit a List of andy.jackson.demo.model.Person (another interface), we
 +
        make use the JsonDeserialize annotation to tell Jackson to create the list using the PersonImpl concrete implementation class.
 +
    */
 
     @JsonDeserialize(as=List.class,contentAs=PersonImpl.class)
 
     @JsonDeserialize(as=List.class,contentAs=PersonImpl.class)
 
     private List<Person> people = new ArrayList<>();
 
     private List<Person> people = new ArrayList<>();
Line 34: Line 41:
 
}
 
}
 
</source>
 
</source>
 +
 +
It can then be deserialised like this:
 +
 +
<source lang="java">
 +
    /**
 +
    * Note the JsonDeserialize annotation against the 'people' property in the FamilyImpl class. It tells Jackson to use a List of PersonImpl.
 +
    *
 +
    * @throws IOException
 +
    */
 +
    @Test
 +
    public void testDeserialize() throws IOException {
 +
        String json = "{\"people\":[{\"name\":\"Fred\",\"age\":55}],\"surname\":\"Smith\"}";
 +
        ObjectMapper om = new ObjectMapper();
 +
        Family family = om.readValue( json , FamilyImpl.class );
 +
        Assert.assertEquals( "Smith" , family.getSurname() );
 +
        Assert.assertEquals( 1 , family.getPeople().size() );
 +
        Assert.assertEquals( 55 , family.getPeople().get(0).getAge() );
 +
        Assert.assertEquals( "Fred" , family.getPeople().get(0).getName() );
 +
    }
 +
</source>
 +
 +
==Further reading==
 +
 +
An example Java implementation can be seen here: https://bitbucket.org/rabora/darkmine-jackson-example

Latest revision as of 07:01, 22 February 2018

Fields that are collections of interfaces

Sometimes, an (imposed) interface demands that we return a collection of interfaces from a member. Consider a 'Family' which contains a number of 'Person' (people). As both Family and Person are interfaces, they have concrete implementations FamilyImpl and PersonImpl respectively. This example shows how you can instruct Jackson to deserialise JSON into the object model. As you can't instantiate a Person, you tell it to use the PersonImpl.

package andy.jackson.demo.model.impl;

import andy.jackson.demo.model.Family;
import andy.jackson.demo.model.Person;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.util.ArrayList;
import java.util.List;

public class FamilyImpl implements Family {
    /*
        As the interface (andy.jackson.demo.model.Family) requires we emit a List of andy.jackson.demo.model.Person (another interface), we
        make use the JsonDeserialize annotation to tell Jackson to create the list using the PersonImpl concrete implementation class.
     */
    @JsonDeserialize(as=List.class,contentAs=PersonImpl.class)
    private List<Person> people = new ArrayList<>();
    private String surname;

    @Override
    public List<Person> getPeople() {
        return people;
    }

    public void setPeople(List<Person> people) {
        this.people = people;
    }

    @Override
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}

It can then be deserialised like this:

    /**
     * Note the JsonDeserialize annotation against the 'people' property in the FamilyImpl class. It tells Jackson to use a List of PersonImpl.
     *
     * @throws IOException
     */
    @Test
    public void testDeserialize() throws IOException {
        String json = "{\"people\":[{\"name\":\"Fred\",\"age\":55}],\"surname\":\"Smith\"}";
        ObjectMapper om = new ObjectMapper();
        Family family = om.readValue( json , FamilyImpl.class );
        Assert.assertEquals( "Smith" , family.getSurname() );
        Assert.assertEquals( 1 , family.getPeople().size() );
        Assert.assertEquals( 55 , family.getPeople().get(0).getAge() );
        Assert.assertEquals( "Fred" , family.getPeople().get(0).getName() );
    }

Further reading

An example Java implementation can be seen here: https://bitbucket.org/rabora/darkmine-jackson-example