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.

Unable to use GSON to serialize anonymous class

See original GitHub issue

Actual behavior (the bug) When attempting to configure Javalin to utilize Gson instead of Jackson for JSON serialization/deserialization, instead of having the configured JSON response body being generated, I receive the string null.

Expected behavior I would expect to see the configured JSON response.

To Reproduce Use the https://github.com/Jitsusama/javalin-gson-example repository as an example. Run the single test in the single class defined in that project and you’ll see what I mean.

Additional context My example project has DEBUG logging turned on by default in case it helps with diagnosis. Attached is an example log. The most pertinent entries from the log are as so:

Request method:	GET
Request URI:	http://localhost:8888
Proxy:			<none>
Request params:	<none>
Query params:	<none>
Form params:	<none>
Path params:	<none>
Headers:		Accept=*/*
Cookies:		<none>
Multiparts:		<none>
Body:			<none>

HTTP/1.1 500 Server Error
Date: Thu, 12 Jul 2018 11:52:17 GMT
Server: Javalin
Content-Type: application/x.my.custom.error+json
Content-Length: 4

null

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Jitsusamacommented, Jul 12, 2018

It’s possible I’m just stupid, but also, it might be possible that some other user could get bitten by this issue. Would you mind if I put through a PR to update the documentation example for Gson integration to call out this scenario?

0reactions
tipsycommented, Jul 12, 2018

You’re welcome. If you really hate dependencies and need a simple mapOf method, you can use TemplateUtil.model():

Map<String, Object> myMap = model(
    "key1", value1,
    "key2", value2,
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing anonymous classes with Gson - java - Stack Overflow
1 Answer 1 ... Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their...
Read more >
Unable to use GSON to serialize anonymous class · Issue #288
Actual behavior (the bug) When attempting to configure Javalin to utilize Gson instead of Jackson for JSON serialization/deserialization, ...
Read more >
Gson User Guide - Google Sites
Gson can work with arbitrary Java objects including pre-existing objects that you ... However, Gson can not automatically deserialize the pure inner classes...
Read more >
Serializing and Deserializing a List with Gson - Baeldung
In this tutorial, we'll explore a few advanced serialization and deserialization cases for List using Google's Gson library.
Read more >
Deserialization problem - Google Groups
I have no problem serializing. I can deserialize using json-lib (net.sf.json.JSONSerializer.toJSON) but not using gson. //create a list of myObect's
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