2

I've a JSON something like this:

objects:[{
   id:"p452365",
   type:"photo",
   link:"http://xxx.zz"
},
{
   id:"v7833",
   type:"video",
   link:"http://xxx.yy",
   length:"4.12"
}
]

In superclass Entity, there're 2 instance variables: id and type. In my extended XmlAdapter class I tried to cast my Entity instances to a subtype for ex. Photo

public HashMap<String, List<Column>> unmarshal(Feeds f) throws Exception {
        for(Feed feed : f.getFeeds()){
            System.out.println("Entity id for feed : " + feed.getId());
            for(Entity e:feed.getObjects()){
                if (e instanceof Photo){
                    // Of course it's not
                }
            }
        }
        return (HashMap<String, List<Column>>)fm.map(f.getFeeds());
    }

Of course e isn't an instanceof Photo, I took a shot there.:) What I wanna do is to interfere the JAXB process sometime and unmarshall according to the type value in JSON.I wonder where and how.

barand
  • 174
  • 1
  • 9

1 Answers1

0

One of my previous answers to a similar question may help here. Essentially it is using the @XmlDescrimatorNode in EclipseLink JAXB (MOXy). Note I'm the MOXy tech lead.

You could also do this with an XmlAdapter. AdaptedEntity would have all the properties from Entity and it's subclasses.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Before I change the JAXB impl, I'd really like to give the second option a try.Thanks for the answer. – barand Feb 24 '11 at 13:53
  • I've changed my mind.Now I'm working with MOXy.I put the jaxb.properties file into the package where my domain classes locate. But I'm getting this exception: java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory.Do you have any idea?Btw I'm currently using RestEasy. – barand Feb 25 '11 at 09:52