Domanda

I have a class that holds contact data; wrapped in a respective class. I recently changed my Photo setup from being a simple byte[] to being a wrapped class as well, but the instantitaion is a little different and now won't serialize/wrap properly.

My other classes wrap properly such as "number":{"log.PhoneNumber":{"number":"123-456-7890"}} but if I feed in a new photo (ie: new Photo("DEADBEEF")) I just get "photo":"DEADBEEF". This is causing problems with the deserializer too.

public class ContactInfo {

    @JsonProperty("name") private Name m_name = null;
    @JsonProperty("number") private PhoneNumber m_number = null;
    @JsonProperty("email") private Email m_email = null;
    @JsonProperty("photo") private Photo m_photo = null;

    @JsonCreator
    public ContactInfo(@JsonProperty("name") Name name,
            @JsonProperty("number") PhoneNumber number,
            @JsonProperty("email") Email email,
            @JsonProperty("photo") Photo photo) {
            /** Set vars **/
            }

    @JsonTypeInfo(use=Id.CLASS, include=As.WRAPPER_OBJECT)
    static public class Photo {
        private byte[] m_decodedBase64 = null;

        public Photo(byte[] encodedBase64) {
            m_decodedBase64 = Base64.decodeBase64(encodedBase64);
        }

        @JsonCreator
        public Photo(@JsonProperty("photoData")String encodedBase64) {
            m_decodedBase64 = Base64.decodeBase64(encodedBase64);
        }

        @JsonProperty("photoData")
        public String getEncodedPhoto() {
            return Base64.encodeBase64String(m_decodedBase64);
        }

        public byte[] getDecodedData() {
            return m_decodedBase64;
        }
    }
}

What am I doing wrong?

È stato utile?

Soluzione

Just figured out what it was. In the ContactInfo class there was a simple accessor function to get the encodedData.

public String getPhoto() {
    return m_photo.getEncodedPhoto();
}

By simple putting it on ignore (or simply change it to return the object itself, which I might do),

@JsonIgnore
public String getPhoto() {
    return m_photo.getEncodedPhoto();
}

The serializer stopped trying to read from it. I wish there was a way to set the serializer engine to be more "explicit declaration" for properties instead of "serialize everything that seems to match the member variables."

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top