Madhur Ahuja
Madhur Ahuja

Reputation: 22661

Serialize using Jackson ObjectMapper

I am trying to serialize the below ArrayList of GAccount objects using Jackson library with following code:

List<Gaccount> gAccounts;
ObjectMapper mapper=new ObjectMapper();
json=mapper.writeValueAsString(gAccounts);

However, I have noticed that only Id and Name fields are serialized but not properties. Sorry, but I am new to Jackson library. Do I have to manually serialize that field ?

package in.co.madhur.ganalyticsdashclock;

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

public class GAccount
{
    private String Id;
    private String Name;
    private List<GProperty> properties=new ArrayList<GProperty>();

    public GAccount(String Id, String Name)
    {
        this.Id=Id;
        this.Name=Name;
    }

    public String getName()
    {
        return Name;
    }
    public void setName(String name)
    {
        Name = name;
    }
    public String getId()
    {
        return Id;
    }
    public void setId(String id)
    {
        Id = id;
    }

    List<GProperty> getProperties()
    {
        return properties;
    }

    void setProperties(List<GProperty> properties)
    {
        this.properties = properties;
    }

    @Override
    public String toString()
    {
        return Name;
    }

}

Upvotes: 1

Views: 1470

Answers (2)

user2869016
user2869016

Reputation: 11

I am using jackson 2.9.0. The default visibility is always 'false' to all the members. In this case, we alway need to use a different visibility, otherwise the result json string will be empty. Here is the code extracted from JsonAutoDetect

public boolean isVisible(Member m) {
            switch(this) {
            case ANY:
                return true;
            ...
            case PUBLIC_ONLY:
                return Modifier.isPublic(m.getModifiers());
            default:
                return false;
            }
        }

Upvotes: 0

tom
tom

Reputation: 2714

The default visibility is to use all public getter methods and all public properties. If you make the getter this:

public List<GProperty> getProperties()

it should work.

You could also change the auto-detection defaults, but it's overkill here. See http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html for more info.

Upvotes: 3

Related Questions