1

i want to serialize a object tree with gson. It works fine, but i don't want to transfer the whole Tree. Especially i want to generate links to child objects.

For Example:

class BookStrore {
  public String name;
  public List<Book> books;
}

class Book {
  public int id;
  public String name;
}

Serializing an instance of BookStore would lead to something like:

{ name : 'bookStoreName', 'books' : [ { id: '3', name: 'blubb' }, ... ] } 

but want to have links like:

{ name: 'bookStoreName', 'books': [ book: 'http://example.com/book/3', ... ] }

Does anyone know how to achieve this with GSON? I'm not sticked to GSON. If anyone have another solution, it's welcome.

2 Answers 2

0

I think you can find a piece of the answer with anotation

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

0

thanks for the link. It made me think again on the problem from another point of view. I finally recognised that the problem is within my approach, because if claim that a "BookStore" has a list of books, there has to be a list of Books and not a list of references. Sure, in Java, a list of books is also a list of references. So i could start generating proxies. For this, i have to modify the serializer/ deserializer. But this would violate the default, in my case java, programming model. Another approach is, which i prefer because its more clear an system independent, to create special data transfer objects. In this case BookLinks and let the BookStore class contain a list of BookLinks. I think thats the right way because it reveals what it is, a link and not an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.