Pottsiex5 Pottsiex5 - 5 months ago 11
JSON Question

JSON is duplicated when writing from object to file

I am using Jackson to convert JSON into an object and vice versa. However, when writing back the object back as JSON it is duplicated, like so:

{
"Users": [
{
"name": "Steve",
"buckets": [
{
"bucketName": "stevesbucket",
"permissions": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
],
"owner": "Steve"
},
{
"bucketName": "NEW BUCKET 2",
"permissions": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
],
"owner": "Steve"
}
]
},
{
"name": "Jeff",
"buckets": [
{
"bucketName": "jeffsbucket",
"permissions": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
],
"owner": "Jeff"
},
{
"bucketName": "stevesbucket",
"permissions": [
"READ"
],
"owner": "Steve"
}
]
}
],
"users": [
{
"name": "Steve",
"buckets": [
{
"bucketName": "stevesbucket",
"permissions": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
],
"owner": "Steve"
},
{
"bucketName": "NEW BUCKET 2",
"permissions": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
],
"owner": "Steve"
}
]
},
{
"name": "Jeff",
"buckets": [
{
"bucketName": "jeffsbucket",
"permissions": [
"CREATE",
"READ",
"UPDATE",
"DELETE"
],
"owner": "Jeff"
},
{
"bucketName": "stevesbucket",
"permissions": [
"READ"
],
"owner": "Steve"
}
]
}
]
}


Where there should only be one "Users" field. I have tried playing with the visibility settings of my object mapper with this:

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE);


However this hasn't made a difference. I think something in my
users.java
file may be causing an issue, as I have extra methods such as addBucket:

public static class Bucket
{
private String bucketName;
private String[] permissions;
private String owner;

public void setBucket(String bucketName, String[] permissions, String owner)
{
this.bucketName = bucketName;
this.permissions = permissions;
this.owner = owner;
}

public String getBucketName()
{
return bucketName;
}

public String[] getPermissions()
{
return permissions;
}

public String getOwner()
{
return owner;
}
}

public static class User
{
private String name;
private Bucket[] buckets;

public String getName()
{
return name;
}

public Bucket[] getBuckets()
{
return buckets;
}

@JsonIgnore
public void addBucket(String bucketName, String[] permissions, String owner)
{
Bucket[] temp = new Bucket[buckets.length+1];
for(int i = 0; i < buckets.length; ++i)
{
temp[i] = buckets[i];
}
temp[temp.length-1] = new Bucket();
temp[temp.length-1].setBucket(bucketName, permissions, owner);
buckets = temp;
}
}

public User[] Users;

public User[] getUsers()
{
return Users;
}

public void setUsers(User[] newUsers)
{
Users = newUsers;
}


Are there some properties I need to add to some things in
users.java
? Or are there other visibility settings I should be using with my
ObjectMapper
?

Answer
public User[] Users;

public User[] getUsers()
{
    return Users;
}

Jackson is serializing the public member Users as "Users" and the getUsers function as "users". Users being private should fix this, and it is good practice for it to be so