question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Type in JsonMapper

See original GitHub issue

This 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 as List<Mytype<String>>. Ideally I’d like to see java.lang.reflect.Type to be an argument of toJsonString() and Context.json() become reified. Do you think it’s good idea ?

And perhaps existing Context functions that use Class<T> should use Type as well because it’s base type for both ParameterizedType and Class<T>.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
unoexpertocommented, Jun 21, 2022

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

0reactions
dzikoyskcommented, Aug 30, 2022

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:

  • It requires dedicated adapter for each signature, so you may need to create some kind of cache of adapters or you’ll pay a cost of creating a new adapter per each ctx.json call and it could impact your performance if it does sth expensive under the hood.
  • Another thing is that it won’t work that with reified functions for e.g. lists, because you don’t have control over type generated by Kotlin compiler, so instead of java.util.List you’ll more likely end up with explicit implementation type like ArrayList/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 use Types.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 base ctx.json anyway, so we could only expose the Type in json function to make it possible specify this parametrized type by hand. I mean you could write utility function like ctx.reifiedJson and use it on your own, but I’m talking in terms of official API.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found