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.

JsonClassPathSource support generics type?

See original GitHub issue

Hello. I’ve tried the demo below but it doesn’t work. I think JsonClassPathSource can’t deserialize json array to java list. Is there any other way to implement it? Thanks.

Java code:

@JsonClassPathSource("test.json")
void test(List<Person> persons){
  log.info("{}",persons)
}

Json file:

[
  {
    "name": "a",
    "age": 23
  },
  {
    "name": "a",
    "age": 23
  }
]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Michael1993commented, May 21, 2022

Oh, yeah, I see where the problem is - you pass a list of objects to your test that expects a list of lists. See, when you execute a parameterized test, it expects an input (a list) which it converts to single parameters. In your example your parameter is a list, so the test tries to convert your input to a list but it can not do that because you supplied a simple list not a list of lists.

To demonstrate with an example, this should work:

@ParameterizedTest
@JsonClasspathSource(JEDI_LIST)
void listObject(List<Jedi> jedisList){
	System.out.println("jedisList = " + jedisList);
}

Jedi list:

[
  [
    {
      "name": "Luke",
      "height": 172
    },
    {
      "name": "Yoda",
      "height": 66
    }
  ]
]

Note that I used two square brackets [ ] in my JSON file - because I want to pass a list of lists.

0reactions
sherlockEixcommented, May 21, 2022

i want use @JsonClasspathSource to prepare my integrate test data. i would like to write data to json file. it’s a complex struct. maybe single object or list. then. i will use injected object(list) to do test. my first idea is i write a utils to load data and deserialize to object. but i found @JsonClasspathSource so i use it to prepare my complex test data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON and Generics in Java - Type safety warning
it extends HashMap but doesn't support parameter type, if its definition is public class JSONObject<K,V> extends HashMap<K,V>.
Read more >
Super Type Tokens in Java Generics - Baeldung
In this tutorial, we're going to get familiar with super type tokens and see how they can help us to preserve generic type...
Read more >
Help me write a Play Json format for generic (bounded types ...
I am trying to write a play Json formatter for the enum and classes below. I want to de-serialize a Json file to...
Read more >
Using generics to load any kind of Codable data
We added a Bundle extension for loading one specific type of JSON data from our app bundle, but now we have a second...
Read more >
Example Using Generic (JSON) Resource Objects - Bloomreach
Resource is the primary object representation to solve all the complex integration problems in a generic way. So, the interface was designed to...
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