Richard G
Richard G

Reputation: 5683

Jackson JSON ObjectMapper adding a 'new' property

I'm just trialling the Jackson XML ObjectMapper for my application.

I wrote this small class. So at the end it prints back the deserialized object. Which is okay, except for the objects at each level it prints a new property called "new": true.

I'm not sure where it's coming from. The object is a spring jpa entity.Is this property something that Spring introduces? It's definitely not a property on the class as I can't read the object string back in until I remove the new property. e.g

lastModifiedDate":null,
"createdBy":null,
"new":true
},

This is the code:

/**
* Updates the layout from a JSON String in the format for the layout definition file
* @return
*/
public Layout updateFromJSONString(String jsonString)
{
    logger.entry(jsonString);

    Layout layout = null;
    try {
        layout = new ObjectMapper().readValue(jsonString, Layout.class);
        logger.exit(new ObjectMapper().writeValueAsString(layout));

    } catch (IOException e) {
        throw new RuntimeException(e)
    }


    return layout;
}

And the base entity which, if any, would be causing the problem - there are 2 other subclasses with just simple properties, all objects including the nested objects inherit from this base class:

public class AbstractAuditableEntity extends AbstractPersistable<Long> implements Auditable<SystemUser, Long> {

    @Column(length = 30)
    private String externalIdentifier;

    @Basic
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @org.springframework.format.annotation.DateTimeFormat(pattern = DateConstants.DEFAULT_TIME_FORMAT)
    private DateTime lastUpdated;

    @OneToOne(fetch = FetchType.LAZY)
    private SystemUser lastUpdateUser;

    @Basic
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @org.springframework.format.annotation.DateTimeFormat(pattern = DateConstants.DEFAULT_TIME_FORMAT)
    private DateTime created;

    @OneToOne(fetch = FetchType.LAZY)
    private SystemUser createUser;



    /**
     * Gets created by audit user.
     */
    @Override
    public SystemUser getCreatedBy() {
        return createUser;
    }

    /**
     * Sets created by audit user.
     */
    @Override

    public void setCreatedBy(SystemUser createdBy) {
        this.createUser = createdBy;
    }

    /**
     * Gets create audit date.
     */
    public DateTime getCreated() {
        return created;
    }

    /**
     * Sets create audit date.
     */
    public void setCreated(DateTime creationDate) {
        this.created = creationDate;
    }

    /**
     * Gets last modified by audit user.
     */
    public SystemUser getLastUpdateUser() {
        return lastUpdateUser;
    }

    /**
     * Sets last modified by audit user.
     */
    public void setLastUpdateUser(SystemUser lastUpdateUser) {
        this.lastUpdateUser = lastUpdateUser;
    }

    /**
     * Gets last modified audit date.
     */
    public DateTime getLastUpdated() {
        return lastUpdated;
    }

    /**
     * Sets last modified audit date.
     */
    public void setLastUpdated(DateTime lastModifiedDate) {
        this.lastUpdated = lastModifiedDate;
    }

    @Override
    public DateTime getCreatedDate() {
        return lastUpdated;
    }

    @Override
    public void setCreatedDate(DateTime dateTime) {
        setCreated(dateTime);

    }

    @Override
    public SystemUser getLastModifiedBy() {
        return lastUpdateUser;
    }

    @Override
    public void setLastModifiedBy(SystemUser systemUser) {
        lastUpdateUser = systemUser;

    }

    @Override
    public DateTime getLastModifiedDate() {
        return lastUpdated;
    }

    @Override
    public void setLastModifiedDate(DateTime dateTime) {
        lastUpdated = dateTime;

    }

    public String getExternalIdentifier() {
        return externalIdentifier;
    }

    public void setExternalIdentifier(String externalIdentifier) {
        this.externalIdentifier = externalIdentifier;
    }

    public void setId(Long id)
    {
        super.setId(id);
    }

    public Long getId()
    {
        return super.getId();
    }
}

Upvotes: 1

Views: 1145

Answers (1)

JB Nizet
JB Nizet

Reputation: 691625

Look at the javadoc of AbstractPersistable: it has a method isNew() returning a boolean. That's why Jackson puts a new attribute in your JSON.

You could easily ihnore this property by overriding the isNew() method and annotating it with @JsonIgnore.

Upvotes: 1

Related Questions