1

First, note that my issue is very similar to: Jackson xml and json root element but differs only slightly where I only want a single root element for JSON.

Here is my UserList class:

@XmlRootElement(name = "users")
@JsonRootName(value = "users")
@JsonTypeName(value = "users")
public class UserList {

  // Tried all of these:
  // @JacksonXmlElementWrapper(localName = "user")
  // @JacksonXmlProperty(localName = "user")
  // @JsonUnwrapped
  // @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NONE /**.NAME */)
  // @JsonProperty("users") // <-- Sets both XML and JSON to 'users'
  @JacksonXmlElementWrapper(useWrapping = false) // <-- This gets rid of duplicate 'users' in XML
  public List<User> user = new ArrayList<User>();

  public UserList() {}

}

Here is my User class:

@XmlRootElement(name = "user")
@JsonRootName(value = "user")
@JsonInclude(Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

  private int                  userId;
  private String               userName;
  private String               password;
  private long                 passwordUpdated;
  private long                 passwordExpire;
  private String               sessionKey;

  public User () {}

  getters and setters here ...
}

Here is my desired JSON (currently I get "user" instead of "users" with the current test code):

{
  "users": [{
    "userId": 1,
    "userName": "test1@user.com",
    "passwordUpdated": 0,
    "passwordExpire": 0,
    "sessionKey": "key"
  }, {
    "userId": 2,
    "userName": "test2@user.com",
    "passwordUpdated": 0,
    "passwordExpire": 0,
    "sessionKey": "key"
  }]
}

Here is my desired XML (which is what I get with the current test code):

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <userId>1</userId>
    <userName>test1@user.com</userName>
    <passwordUpdated>0</passwordUpdated>
    <passwordExpire>0</passwordExpire>
    <sessionKey>key</sessionKey>
  </user>
  <user>
    <userId>2</userId>
    <userName>test2@user.com</userName>
    <passwordUpdated>0</passwordUpdated>
    <passwordExpire>0</passwordExpire>
    <sessionKey>key</sessionKey>
  </user>
</users>

Here is my test code:

{

  User user1 = new User();
  user1.setUserId(1);
  user2.setUserName("test1@user.com");
  user1.setPasswordExpire(0);
  user1.setPasswordUpdated(0);
  user1.setSessionKey("key");

  User user2 = new User();
  user2.setUserId(1);
  user2.setUserName("test2@user.com");
  user2.setPasswordExpire(0);
  user2.setPasswordUpdated(0);
  user2.setSessionKey("key");

  UserList userList = new UserList();
  userList.user.add(user1);
  userList.user.add(user2);

  String json = MapperUtils.modelToJson(userList);

  String xml = MapperUtils.modelToXml(userList);

}

public class MapperUtils {

  final static ObjectMapper jsonMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  final static ObjectMapper xmlMapper = new XmlMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  public static String modelToJson(final Object object) throws IOException {
    return jsonMapper.writeValueAsString(object);
  }

  public static String modelToXml(final Object object) throws IOException {
     return xmlMapper.writer().writeValueAsString(object);
  }

}

My issue is I would like the root element of my JSON to be "users" and not "user". Any help would be appreciated.

TomGardner
  • 13
  • 4

1 Answers1

1

@JacksonXmlProperty is how you can rename a property in xml. Then let name for JSON be the field name that I changed to users below. You can also remove most of the annotations you experimented with. This will produce expected XML and JSON:

@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
class User {
    private int userId;
    private String userName;
    private String password;
    private long passwordUpdated;
    private long passwordExpire;
    private String sessionKey;
}

@JsonRootName(value = "users")
class UserList {
    @JacksonXmlProperty(localName = "user")
    @JacksonXmlElementWrapper(useWrapping = false)
    List<User> users = new ArrayList<>();
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82