-1

I have a json string like:

{
   "name": "abc",
   "type": "type1",
   "artist": {
       "name": "ally"
   },
   "other_part": "{\"id\":\"ee50abd7\",\"metadata\":"...\"}"
}

Like you see, "other_part" looks like in lenient format. It presents with quotation mark.

Here, I just want to convert it to a POJO class. But other_part gives parse error. Any suggestion.

POJO Class:

class Data
{
   private String name;
   private String type;
   private Artist artist;
   private Other other_part;
   ...getters
} 
class Artist
{
   private String name;
   ...getters
} 
class Other
{
   private String id;
   private String metadata;
   ...getters
} 

and restTemplate:

restTemplate.exchange(requestEntity, Data.class);

Error:

(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value...

Thanks guys solved with JsonDeserialize:

public static class OtherConverter extends StdConverter<String, Other>
{

    @Override
    public Other convert(String value)
    {
        return new Gson().fromJson(value, Other.class);
    }
}

class Data
{
   private String name;
   private String type;
   private Artist artist;

   @JsonDeserialize( converter = OtherConverter.class )
   private Other other_part;
   ...getters
}
Sha
  • 921
  • 17
  • 46
  • Why are you mixing Jackson and Gson? – terrorrussia-keeps-killing Apr 19 '21 at 15:57
  • @fluffy thanks for your review. Yes it can be better to get rid of one of them. Is there any suggestion for better solution? – Sha Apr 19 '21 at 19:23
  • Sure. Gson has `@JsonAdapter` annotation. You can bind a type adapter factory into the annotation, and the type adapter factory should do the following: 1) obtain the original type adapter factory by using `gson.getDelegateAdapter(this, typeToken)` in the `create` method; 2) return a new type adapter whose `read` method reads a string from `JsonReader` using the `nextString()` method and delegates the string to the delegate adapter by invoking its `fromJson(String)` method overload. That's it. – terrorrussia-keeps-killing Apr 19 '21 at 20:36

3 Answers3

1

You will probably need to create a custom deserializer.

   @JsonDeserialize(using = CustomDateDeserializer.class) 
   private Other other_part;

Inside the custom serializer you will receive the string information. Then you can transform this string using a library of choice.

Example: https://www.tutorialspoint.com/jackson_annotations/jackson_annotations_jsondeserialize.htm

Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
0

Did you try to provide Setters and empty constructor for classes , in order to serialize Jackson(default data serializer in spring) require this one from what I know.

Misa D.
  • 183
  • 10
-1

It seems that the other_part is just a String value but not the JSON Object.

If there is no way to change the input then I would suggest you create custom setter/parser for the otherPart field:

@JsonProperty("other_part")
private void unpackOtherPart(String otherPart) {
   // create Other from otherPart string
}