Serialize a collection of POJOs objects to JSON struct with Morphia?

53 views
Skip to first unread message

Chris

unread,
May 16, 2013, 7:29:12 AM5/16/13
to mor...@googlegroups.com

Hi, I am using Morphia (0.99) for mapping POJO to MongoDB (2.0.6). Now I want to return a collection / list as a JSON struct from my data-store with including IDs for each object to be sent with jQuery to the browser client.

I retrieve the POJO list from the data-store and then convert each object to a DBObject MongoDB object. This works fine except that the id of the object is not included in the print-out of the object. This could be due to the the ID is inherited from the Class A. The POJO B has a lot of property members, so one idea was to make a simple proxy POJO and then Serialize.

How can I make a JSON struct from the list of DBObject(with Morphia or MongoDB driver) and how can I include the id for each element in the struct?

PS. I've bean looking at the BasicBSONList MongoDB driver object or using the GSON (Google) lib, but I am lacking the pattern for how to do this and I want to use core Morphia if possible.

POJO Classes:

Class A {
    @Id
    private ObjectId id;
}

Class B extends A {
    ...

    @Override    
    public ObjectId getId() {
        return super.getId();
    }

    @Override
    public void setId(ObjectId id) {
        super.setId(id);
    }
    ...
}
Reply all
Reply to author
Forward
0 new messages