0

I got a flat JSON string like

{"ccy":"EUR", 
 "value1":500, 
 "value2":200, 
 "date":"2017-07-25", 
 "type":"", 
 ... <many other pairs>}

The JSON string shall be deserialized in Java using Jackson:

public class Data
{
  @JsonProperty("ccy")
  private String currency;

  private Amount value1;

  private Amount value2;

  @JsonProperty("date")
  private String date;

  @JsonProperty("type")
  private String type;

  ... <many other members>
}

with

public class Amount
{
  private double value;

  private String currency;

  public Amount(double value, String currency)
  {
    this.value = value;
    this.currency = currency;
  }
}

What is the correct use of Jackson annotations to fill the value1 and value2 fields in my Data class?

I tried custom setters like:

@JsonSetter("value1")
private void setValue1(double value1)
{
  this.value1 = new Amount(value1, this.currency);
}

@JsonSetter("value2")
private void setValue2(double value2)
{
  this.value2 = new Amount(value2, this.currency);
}

But this only works if this.currency is deserialized first (what I cannot rely on).

Is there a neat solution that does not use a custom constructor as Data(@JsonProperty("value1") double value1, (@JsonProperty("value2") double value2, (@JsonProperty("ccy") String currency) {...} ?

edit: An approach that uses Jackson would be preferred.

Max
  • 274
  • 3
  • 13

1 Answers1

1

You can use GSON library.
It's very easy approach. Assume your Class is "User"

Gson gson = new Gson();
String jsonInString = "{\"userId\":\"1\",\"userName\":\"Yasir\"}";
User user= gson.fromJson(jsonInString, User.class);

add it using this dependency

<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
</dependency>

Hope this helps

Rahal Kanishka
  • 720
  • 13
  • 27
  • for more details https://stackoverflow.com/questions/38636254/how-to-convert-json-to-java-object-using-gson – Rahal Kanishka Jul 25 '17 at 14:00
  • Thanks for your reply. I am not sure how GSON is applied correctly in my example. Can you provide some code? – Max Jul 25 '17 at 14:23
  • use the same approach. When you defyning your two instances use my approach on the 3rd line of the example provided(as the Atomic Class User is instantiated) – Rahal Kanishka Jul 25 '17 at 14:27
  • Just try it on when instantiating your Class Data and by using System.out or Debugging you can see your variables are filled with incoming data. But you have to make sure that your Json should be 100% correctly formatted according to the Class Data – Rahal Kanishka Jul 25 '17 at 14:33
  • I cannot see how this could work. I do not have direct access to the JSON string in my `Data` class. Also where should I put the information which fields belong to `Amount value1` and which to `Amount value2`? – Max Jul 25 '17 at 14:52
  • Thats the beauty of GSON. you don't have to set each and every value for your variables in the new instance. if the provided data and the format of the incoming JSON this will work perfectly.And follow that reference I provided and you will get a better understandment of how this works :) – Rahal Kanishka Jul 25 '17 at 15:03
  • here is another example which can help you out. HashMap result = new Gson().fromJson(new JsonParser().parse(frame).getAsJsonArray().get(0).toString(), HashMap.class); this used to put the incoming data of the Json into a Hashmap instance called result – Rahal Kanishka Jul 25 '17 at 15:07