简体   繁体   中英

Map a Jsonpath output to a list of POJOs

I'm trying to map directly the output of a Jsonpath to a list of POJO. I'm using Jackson as a mapping provider.

Jsonpath output :

{
  "actions" : [
    {
      "parameterDefinitions" : [
        {
          "defaultParameterValue" : {
            "name" : "PARAM1",
            "value" : ""
          },
          "description" : "Type String",
          "name" : "PARAM1",
          "type" : "StringParameterDefinition"
        },
        {
          "defaultParameterValue" : {
            "name" : "PARAM3",
            "value" : ""
          },
          "description" : "Type String",
          "name" : "PARAM3",
          "type" : "StringParameterDefinition"
        }
      ]
    }
  ]
}

JobParameter.java (the POJO in which I'd like to map):

public class JobParameter {

   private String description;
   private String name;
   private String type;

   // public getters & setters

Jsonpath initialization :

Configuration conf = Configuration
   .builder()
   .mappingProvider(new JacksonMappingProvider())
   .build();

List<JobParameter> jobParameters = JsonPath
   .using(conf)
   .parse(jsonpathOutput)
   .read("$.actions[0].parameterDefinitions[0:]", List.class);

Using the above code, I always get a Map. See below the result of a toString() on this map:

[{defaultParameterValue={name=PARAM1, value=}, description=Type String, name=PARAM1, type=StringParameterDefinition}, {defaultParameterValue={name=PARAM3, value=}, description=Type String, name=PARAM3, type=StringParameterDefinition}]

Note that when I try to map the Jsonpath output to a single object, the deserialization works fine:

Configuration conf = Configuration
   .builder()
   .mappingProvider(new JacksonMappingProvider())
   .build();

JobParameter singleJobParameter = JsonPath
   .using(conf)
   .parse(jsonpathOutput)
   .read("$.actions[0].parameterDefinitions[0]", JobParameter .class);

In the example above, the singleJobParameter instance is well created and filled.

Am I missing something? Thanks!

You must use a TypeRef for this. In your case you must also use the @JsonIgnoreProperties annotation.

   Configuration conf = Configuration
            .builder()
            .mappingProvider(new JacksonMappingProvider())
            .jsonProvider(new JacksonJsonProvider())
            .build();


    TypeRef<List<JobParameter>> type = new TypeRef<List<JobParameter>>(){};

    List<JobParameter> jobParameters = JsonPath
            .using(conf)
            .parse(json)
            .read("$.actions[0].parameterDefinitions[0:]", type);

Note that this is not supported by all JsonMappingProviders.

<dependency>
    <groupId>com.github.jsurfer</groupId>
    <artifactId>jsurfer-simple</artifactId>
    <version>1.2.2</version>
</dependency>

With JsonSurfer, you can achieve this by simply two lines:

JsonSurfer jsonSurfer = JsonSurfer.jackson();
Collection<JobParameter> parameters = jsonSurfer.collectAll(json, JobParameter.class, "$.actions[0].parameterDefinitions[*]");

And don't forget to ignore the unused "defaultParameterValue" in your POJO.

@JsonIgnoreProperties({"defaultParameterValue"})
private static class JobParameter {

    private String description;
    private String name;
    private String type;

Using a wrapper POJO solved the problem.

@JsonIgnoreProperties(ignoreUnknown = true)
public class JobParametersWrapper {

   private List<JobParameter> parameterDefinitions;

   public List<JobParameter> getParameterDefinitions() {
      return parameterDefinitions;
   }

   public void setParameterDefinitions(List<JobParameter> parameterDefinitions) {
      this.parameterDefinitions = parameterDefinitions;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM