0

I've read countless articles about parsing Java objects to JSONs and still have issues...

I know that there are a bunch of frameworks out there and this is where things messed up I guess.

I'm trying to parse a map into a json:

Map<CategoryBean, Double> questionsPercentagePerCategory;

here's how CategoryBean looks like:

@XmlRootElement
public class CategoryBean implements Serializable{
    private static final long serialVersionUID = -7306680546426636719L;
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

questionsPercentagePerCategory is a variable inside a wrapper json called: PrePracticeBean

and this is how it looks:

@XmlRootElement
public class PrePracticeBean implements Serializable {
    private int maxQuestionsAllowedForUser;
    private int maxQuestionsAllowedForUserAfterCreditOver;
    private int questionsInExam;
    @XmlJavaTypeAdapter(XmlGenericMapAdapter.class)
    private Map<CategoryBean, Double> questionsPercentagePerCategory;
    private static final long serialVersionUID = -655358519739911024L;

    public int getMaxQuestionsAllowedForUser() {
        return maxQuestionsAllowedForUser;
    }

    public void setMaxQuestionsAllowedForUser(int maxQuestionsAllowedForUser) {
        this.maxQuestionsAllowedForUser = maxQuestionsAllowedForUser;
    }

    public int getMaxQuestionsAllowedForUserAfterCreditOver() {
        return maxQuestionsAllowedForUserAfterCreditOver;
    }

    public void setMaxQuestionsAllowedForUserAfterCreditOver(int maxQuestionsAllowedForUserAfterCreditOver) {
        this.maxQuestionsAllowedForUserAfterCreditOver = maxQuestionsAllowedForUserAfterCreditOver;
    }

    public int getQuestionsInExam() {
        return questionsInExam;
    }

    public void setQuestionsInExam(int questionsInExam) {
        this.questionsInExam = questionsInExam;
    }

    public Map<CategoryBean, Double> getQuestionsPercentagePerCategory() {
        return questionsPercentagePerCategory;
    }

    public void setQuestionsPercentagePerCategory(Map<CategoryBean, Double> questionsPercentagePerCategory) {
        this.questionsPercentagePerCategory = questionsPercentagePerCategory;
    }
}

as you can see I've marked both beans with @XmlRootElement annotation to get Jeresey's OOB bean to JSON parsing functionality as specified here

Furthermore, here's how the XMLGenericMapAdapter looks like:

public class XmlGenericMapAdapter<K, V> extends XmlAdapter<MapType<K, V>, Map<K, V>> {

    @Override
    public Map<K, V> unmarshal(MapType<K, V> orgMap) throws Exception {
        HashMap<K, V> map = new HashMap<K, V>();

        for (MapEntryType<K, V> mapEntryType : orgMap.getEntries()) {
            map.put(mapEntryType.getKey(), mapEntryType.getValue());
        }
        return map;
    }

    @Override
    public MapType<K, V> marshal(Map<K, V> v) throws Exception {
        MapType<K, V> mapType = new MapType<K, V>();

        for (Map.Entry<K, V> entry : v.entrySet()) {
            MapEntryType<K, V> mapEntryType = new MapEntryType<K, V>();
            mapEntryType.setKey(entry.getKey());
            mapEntryType.setValue(entry.getValue());
            mapType.getEntries().add(mapEntryType);
        }
        return mapType;
    }
}

Well, the end result is what makes me crazy... it's intermittent... when running the code in debug mode, this works flawlessly showing a nested json for the map and each key:value pair is another nested json. However, when invoked in run mode, I get an ugly "memory address" instead of the CategoryBean key...

My only guess is that this is related to class loading matters and that I might be having some other JAR that's having a class which is loaded first in debug mode but not in run mode...

anyways, any suggestions as to how this should be done, would be appreciated.

thanks, GBa.

GBa
  • 976
  • 11
  • 16

1 Answers1

0

Well, solved...

some JAXB/Jersey stuff which I'm not into understanding up to the last bit... but I bet that there's some JAXB guru out there who could give the right explanation why this is the case...

Anyways, bottom line is that the CategoryBean class should not have the annotation @XmlRootElement but instead should have @XmlAccessorType(XmlAccessType.FIELD)

I got the inspiration for that from Serializer for (Hash)Maps for Jersey use?

thanks, GBa.

Community
  • 1
  • 1
GBa
  • 976
  • 11
  • 16