Using Type in JsonMapper
See original GitHub issueThis is a comment by @unoexperto from an issue (https://github.com/tipsy/javalin/issues/1445#issuecomment-1007619810)
@tipsy Sorry for asking here, David, but I didn’t want to create another issue for a question. I’m using Moshi for json serialization. I see that
io.javalin.plugin.json.JsonMapper.toJsonString()
doesn’t carry type information about the object. As result I can’t serialize generic types such asList<Mytype<String>>
. Ideally I’d like to seejava.lang.reflect.Type
to be an argument oftoJsonString()
andContext.json()
become reified. Do you think it’s good idea ?And perhaps existing Context functions that use
Class<T>
should useType
as well because it’s base type for bothParameterizedType
andClass<T>.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Intro to the Jackson ObjectMapper - Baeldung
This tutorial focuses on understanding the Jackson ObjectMapper class and how to serialize Java objects into JSON and deserialize JSON ...
Read more >Jackson - ObjectMapper Class - Tutorialspoint
typer) - Method for enabling automatic inclusion of type information, using specified handler object for determining which types this affects, as well as ......
Read more >How to get field type using Jackson ObjectMapper?
Any idea how to retrieve Java type for given json path based on Jackson's ObjectMapper object and particular Class? Let's say we have...
Read more >ObjectMapper (jackson-databind 2.7.0 API) - FasterXML
Method for enabling automatic inclusion of type information, using specified handler object for determining which types this affects, as well as details of...
Read more >Jackson ObjectMapper - Jenkov.com
This Jackson ObjectMapper tutorial explains how to use the Jackson ObjectMapper ... A primitive type in Java cannot have the value null ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tipsy Hi David. Unfortunately I was pressured with deadline at the time so I ended up writing thin routing wrapper around reactor-netty-http for my restful api implementation. I’ll give javalin another try in a month or so.
I’ve made a prototype of this feature in:
Like I said, it’s not that beauty because we’ve made handling experience a little bit worse due to casts & unsafe generic signatures, but in the end it works.
As a side note, I think Moshi will be quite painful to use, because:
ctx.json
call and it could impact your performance if it does sth expensive under the hood.java.util.List
you’ll more likely end up with explicit implementation type likeArrayList/LinkedList/etc
+ their immutable wrappers and Moshi by default supports only interface types. As far as I’ve seen there’s no convenient solution, so you’ll need to useTypes.newParameterizedType
anyway for such scenarios instead of reified type, but it should work properly for regular records/data classes– Edit Looks like we can’t add extra reified
ctx.json
, because it’ll be shadowed by basectx.json
anyway, so we could only expose theType
injson
function to make it possible specify this parametrized type by hand. I mean you could write utility function likectx.reifiedJson
and use it on your own, but I’m talking in terms of official API.