Marshal java.util.Map <String, String>

The next question is for my calm JSON service.

import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Martin Burchard
 * 
 */
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
    private String id;
    private String nickname;
    private String email;
    private String password;
    private Map<String, String> user_attributes;

}

The service currently provides the following JSON (indented for better reading):

{
    "user" : {
        "id" : "9bdf40ea-6d25-4bc3-94ad-4a3d38d2c3ca",
        "email" : "test.user@test.de",
        "password" : "xXpd9Pl-1pFBFuX9E0hAYGSDTyJQPYkOtXGvRCrEtMM",
        "user_attributes" : {
            "entry" : [{
                    "key" : "num",
                    "value" : 123
                }, {
                    "key" : "type",
                    "value" : "nix"
                }
            ]
        }
    }
}

The funny thing is that the internal number 123 is java.lang.String ...

I do not understand what is explained here http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues

I like this JSON:

{
    "user" : {
        "id" : "9bdf40ea-6d25-4bc3-94ad-4a3d38d2c3ca",
        "email" : "test.user@test.de",
        "password" : "xXpd9Pl-1pFBFuX9E0hAYGSDTyJQPYkOtXGvRCrEtMM",
        "user_attributes" : {
            "num" : "123",
            "type" : "nix"
        }
    }
}

I changed the JSON provider to Jackson. Now my JSON looks like I like ...

+5
source share
2 answers

The only thing that comes to my mind is to use the JAXB XmlAdapter. You can determine how a given object (in your case Map) will be displayed in a JSON string.

+1

JSON, Jackson

0

All Articles