-2

I have a Map<String, Object> map that was deserialized from a simple JSON string {"field1":"val1", "field2":"val2", "isReal":true}. I am trying to construct a Java object MyObject with the above fields.

I can do it using com.fasterxml.jackson.databind.ObjectMapper like so:

public static MyObject load(Map<String, Object> map) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(new ObjectMapper().writeValueAsString(map), MyObject.class);
    }

I was wondering if anyone knows how to do the same using com.jsoniter library?

I have tried to use JsonIterator.deserialize, but that doesn't take a Map as input.

I have also seen ReflectionEncoderFactory usage on the library website, but I don't fully grasp as to how I would use it to construct an object MyObject

QuirkyBit
  • 654
  • 7
  • 20
  • What have you tried and what errors are you getting? – Scary Wombat Jan 07 '20 at 01:21
  • @ScaryWombat I don't know what API to use to convert `Map map` to POJO using `com.jsoniter`. I know that `ObjectMapper` from `jackson.databind.ObjectMapper` works, but what would be the equivalent using `jsoniter` library? – QuirkyBit Jan 07 '20 at 01:27
  • Did you look at http://jsoniter.com/ ? On the front page they have examples using `deserialize` - did you try this? – Scary Wombat Jan 07 '20 at 01:31
  • @ScaryWombat, I did, I don't understand how to use it... can you please explain? – QuirkyBit Jan 07 '20 at 01:56
  • why don't you post what you have tried? It looks very similar in concept to jackson – Scary Wombat Jan 07 '20 at 02:03
  • @ScaryWombat I only see how to `deserialize` a JSON string or bytes[] to an object or Iterator, I would like to `deserialize` a map to an object of class `MyObject`. I don't see a `deserialize` methods that takes a `Map map` as input. – QuirkyBit Jan 07 '20 at 02:06
  • @ScaryWombat I've updated the question to say what I have tried, but I must be not understanding the examples at jsoniter.com because I wasn't able to construct an object like I can with jackson. – QuirkyBit Jan 07 '20 at 02:11
  • See this [answer](https://stackoverflow.com/a/36934134/2310289) Basically uou are wanting to convert a map to json with no dependancies (otherwise use jackson) – Scary Wombat Jan 07 '20 at 02:24
  • @ScaryWombat I can convert it without dependencies, and I also know how to do it with jackson. My questions are: 1) Is it possible to do it with jsoniter? 2) How? Any help would be appreciated. Thank you! – QuirkyBit Jan 07 '20 at 02:29
  • @ScaryWombat I am also NOT trying to get a JSON string back, I am trying to map a `Map map` to an object using jsoniter library. – QuirkyBit Jan 07 '20 at 02:31
  • 1
    Is not the intermediate step `new ObjectMapper().writeValueAsString(map)` converting to json? Once you have json you can use jsoniter to `deserialize` to your object - no? Anyway, that is all I can add. – Scary Wombat Jan 07 '20 at 02:37
  • The equivalent would be: `public static PulsarS3SourceConfig load(Map map) throws IOException { JsonStream.serialize(map); return JsonIterator.deserialize(JsonStream.serialize(map), PulsarS3SourceConfig.class); }` – QuirkyBit Jan 07 '20 at 02:51
  • @ScaryWombat I don't need it twice, it was a typo. Yes, you have set me in the right direction. – QuirkyBit Jan 07 '20 at 02:59

1 Answers1

4

As per @Scary Wombat's explanation:

First it is necessary to convert the Map back to JSON string and then convert the JSON string to an object:

    public static MyObject load(Map<String, Object> map) throws IOException {
        return JsonIterator.deserialize(JsonStream.serialize(map), MyObject.class);
    }

JsonStream.serialize(map) is the same new ObjectMapper().writeValueAsString(map)

QuirkyBit
  • 654
  • 7
  • 20