Polymorphic Serialization of Enums

瀏覽次數:58 次
跳到第一則未讀訊息

Eero Aaltonen

未讀,
2015年11月6日 上午8:41:502015/11/6
收件者:Genson user group
I have a use case where I have a mixed enums with generics. It looks like the problem might be solvable using a custom Converter, but I am unsure how exactly to achieve that.
I put the full on question on Stack Overflow

because of the formatting, search and rep features.

Cheers,
Eero

Eugen Cepoi

未讀,
2015年11月6日 下午6:45:412015/11/6
收件者:gen...@googlegroups.com
Hi Eero!

I just answered on SO with a solution to your problem.
The solution is generic enough to work with all kind of other situations, which should be great for you.

I would like to include it in Genson but don't have very much time right now and want to involve more the library users to become contributors.
If you are interested in contributing it would be great if you could take the time to think about my solution and open a PR so we make it part of Genson.
This would imply making sure it works well with the other features (useClassMetadataWithStaticType to false, runtime types etc), is configurable enough (disabled by default and enabled via a new option in the builder) and is disabled for primitive types (we don't need type info for them, this is already part of JSON format).
If you need help for this I can provide some.
Otherwise I will probably do it later on my self.

Thanks!
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 http://groups.google.com/group/genson.
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/e60d508b-dfc6-4ecd-92b8-87bdd9cb04a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eero Aaltonen

未讀,
2015年11月11日 上午11:30:482015/11/11
收件者:Genson user group
Hi Eugen!

I implemented a solution where I registered a custom Converter only for enum types. It solves my immediate problem.
I agree with your design points. Ideally the decision between JSON object and raw value serialization could be made on whether the current context provides static type information. Is something like this currently available or would it also need be implemented?

BR,
Eero

Eugen Cepoi

未讀,
2015年11月16日 下午1:48:362015/11/16
收件者:gen...@googlegroups.com
It is accessible in some way (when providing the converters using a factory for example) but the thing is that when we use runtime ser/de the actual type is the concrete one and we loose track of the static type. So this would need to be handled in some way.
I will have a look later on how to handle this.

BTW, why did you use a custom converter for enums instead of the one I posted on SO? If it is because it didn't work for you I'd really like to know, as I am thinking of using something very similar if I have to implement this feature.

Thanks,
Eugen

Eero Aaltonen

未讀,
2015年11月17日 凌晨4:48:582015/11/17
收件者:Genson user group
On Monday, November 16, 2015 at 8:48:36 PM UTC+2, eugen wrote:

BTW, why did you use a custom converter for enums instead of the one I posted on SO? If it is because it didn't work for you I'd really like to know, as I am thinking of using something very similar if I have to implement this feature.

Mostly because I did not understand it. However comparing the two the difference is that what I wrote writes the metadata in "@class".
The actual serialized form that I ended up with is:

"value":{
    "@class":"RainState",
    "value":"LIGHT_RAIN"
}

Regards,
Eero
回覆所有人
回覆作者
轉寄
0 則新訊息