Defining interface implementations

28 views
Skip to first unread message

Jeremy Wagemans

unread,
Apr 12, 2016, 4:46:09 AM4/12/16
to Genson user group
Hi everyone,

Some of my classes have associations with other classes. Generally, these association attributes are typed as interfaces.

class PersonImpl implements Person {


   
private String name;
   
private Country country; // Country is actually an interface
   
...


}


Using Genson, I try to deserialize a JSON string into a pojo of type Person.

Object entity = factory.build(Person.class); // Creates an instance of Person
Genson genson = new GensonBuilder().create();
genson
.deserializeInto(str, entity);


However when Genson tries to deserialize the country property it fails finding a constructor as it doesn't know the implementation of the interface.

I see two different approaches to deal with this problem:
  1. Genson should use my factory to build instances.
  2. Genson should us a sort of "type adapter" which returns the implementation class of any interface.
In your opinion, which of the two approaches would suit my needs and how would you implement it in Genson ?

Thanks a lot in advance for your help!!

Eugen Cepoi

unread,
Apr 12, 2016, 1:33:04 PM4/12/16
to gen...@googlegroups.com
Hi Jeremy,

In your case do the interfaces have a single implementation class? Will it always remain this way?
For the general case of polymorphism, if the code is being serialized by genson then it can deserialize it back.
It can be enabled with useClassMetadata(true) on GensonBuilder.

Eugen

--
You received this message because you are subscribed to the Google Groups "Genson user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genson+un...@googlegroups.com.
To post to this group, send email to gen...@googlegroups.com.
Visit this group at https://groups.google.com/group/genson.
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/2699467f-65b8-4746-9807-db01c6df3207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ömer Korkmaz

unread,
Sep 7, 2017, 9:00:48 AM9/7/17
to Genson user group
Hi Jeremy did you solved this problem.

12 Nisan 2016 Salı 11:46:09 UTC+3 tarihinde Jeremy Wagemans yazdı:
Reply all
Reply to author
Forward
0 new messages