Deserialising Enum implements Interface?

36 views
Skip to first unread message

Peter Davidson

unread,
Apr 9, 2018, 4:41:03 PM4/9/18
to Genson user group
So I have an Enum type that implements an Interface, and whilst serialising the JSON turns out fine it can't seem to deserialise it, giving the error "No constructor has been found for type interface." How do I get around this? I put a constructor in the offending Enum subtype but that doesn't seem to help. It doesn't serialise the metadata of that interface field so in the JSON it just looks like a String.

Eugen Cepoi

unread,
Apr 9, 2018, 4:48:13 PM4/9/18
to gen...@googlegroups.com
Seems that you are trying to deserialize to the interface type instead of the concrete enum type. Genson doesn't know what the concrete type of your interface is.
You can enable support for polymorphic types though this has some limitations http://owlike.github.io/genson/Documentation/UserGuide/#polymorphic-types

2018-04-09 13:41 GMT-07:00 Peter Davidson <yoursta...@hotmail.com>:
So I have an Enum type that implements an Interface, and whilst serialising the JSON turns out fine it can't seem to deserialise it, giving the error "No constructor has been found for type interface." How do I get around this? I put a constructor in the offending Enum subtype but that doesn't seem to help. It doesn't serialise the metadata of that interface field so in the JSON it just looks like a String.

--
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+unsubscribe@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/b79ee56a-4f9d-400e-acc5-eaa3c5cc50c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Davidson

unread,
Apr 9, 2018, 5:18:45 PM4/9/18
to Genson user group
Is there a way to force Genson to serialise the concrete type in the JSON? It seems like it can do it for objects but not for enums.


On Monday, April 9, 2018 at 9:48:13 PM UTC+1, eugen wrote:
Seems that you are trying to deserialize to the interface type instead of the concrete enum type. Genson doesn't know what the concrete type of your interface is.
You can enable support for polymorphic types though this has some limitations http://owlike.github.io/genson/Documentation/UserGuide/#polymorphic-types
2018-04-09 13:41 GMT-07:00 Peter Davidson <yoursta...@hotmail.com>:
So I have an Enum type that implements an Interface, and whilst serialising the JSON turns out fine it can't seem to deserialise it, giving the error "No constructor has been found for type interface." How do I get around this? I put a constructor in the offending Enum subtype but that doesn't seem to help. It doesn't serialise the metadata of that interface field so in the JSON it just looks like a String.

--
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.

Eugen Cepoi

unread,
Apr 9, 2018, 5:38:23 PM4/9/18
to gen...@googlegroups.com
Currently no. I started working on a variant that allows to serialize this information for any type (by basically enclosing literals into a json object) but never finished it...
Someone had a similar need in the past and here is the solution I was proposing https://stackoverflow.com/questions/33568019/genson-polymorphic-generic-serialization

Though if this is the only case where you need that, maybe you should just write a custom Converter?



To unsubscribe from this group and stop receiving emails from it, send an email to genson+unsubscribe@googlegroups.com.

To post to this group, send email to gen...@googlegroups.com.
Visit this group at https://groups.google.com/group/genson.

Peter Davidson

unread,
Apr 9, 2018, 5:46:39 PM4/9/18
to Genson user group
Hmm, unfortunate. I was trying out Gson with the extended RuntimeAdapterFactory that supported polymorphism but that ran into major issues with deserialisation that I still cannot solve. I could try writing a custom Converter but the issue is that in changing my parsing system from XML to JSON I ended up using a lot more interface based abstraction which resulted in many more classes being written. Can I write a Converter that would just instantiate with reflection based on a serialised classname, or would I need some kind of switch statement for every possible type?


On Monday, April 9, 2018 at 10:38:23 PM UTC+1, eugen wrote:
Currently no. I started working on a variant that allows to serialize this information for any type (by basically enclosing literals into a json object) but never finished it...
Someone had a similar need in the past and here is the solution I was proposing https://stackoverflow.com/questions/33568019/genson-polymorphic-generic-serialization

Though if this is the only case where you need that, maybe you should just write a custom Converter?


2018-04-09 14:18 GMT-07:00 Peter Davidson <yoursta...@hotmail.com>:
Is there a way to force Genson to serialise the concrete type in the JSON? It seems like it can do it for objects but not for enums.

On Monday, April 9, 2018 at 9:48:13 PM UTC+1, eugen wrote:
Seems that you are trying to deserialize to the interface type instead of the concrete enum type. Genson doesn't know what the concrete type of your interface is.
You can enable support for polymorphic types though this has some limitations http://owlike.github.io/genson/Documentation/UserGuide/#polymorphic-types

2018-04-09 13:41 GMT-07:00 Peter Davidson <yoursta...@hotmail.com>:
So I have an Enum type that implements an Interface, and whilst serialising the JSON turns out fine it can't seem to deserialise it, giving the error "No constructor has been found for type interface." How do I get around this? I put a constructor in the offending Enum subtype but that doesn't seem to help. It doesn't serialise the metadata of that interface field so in the JSON it just looks like a String.

--
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/b79ee56a-4f9d-400e-acc5-eaa3c5cc50c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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.

Eugen Cepoi

unread,
Apr 9, 2018, 5:52:54 PM4/9/18
to gen...@googlegroups.com
Well maybe some of these interfaces are not necessary in the first place, that would simplify things.
The code I linked you to in stackoverflow is supposed to do it all for you, I think it can be worth trying especially if you have abstractions all over the place (as you will end up needing to write custom converters for all of them).



To unsubscribe from this group and stop receiving emails from it, send an email to genson+unsubscribe@googlegroups.com.

To post to this group, send email to gen...@googlegroups.com.
Visit this group at https://groups.google.com/group/genson.
Reply all
Reply to author
Forward
0 new messages