1

I am trying to convert a JAVA POJO object with a JsonObject to a String as following:

   class MyPojo {
      public MyPojo(String sName, JsonObject obj) 
      {
         this.sName = sName;
         this.tJsonObj = obj;
      }
      public String sName;
      public javax.json.JsonObject tJsonObj;
   };

    javax.json.JsonObject jsonObj = JsonUtils.toJsonObject("{\"key\": 123}");
    ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper();
    JsonNode node = mapper.valueToTree(new MyPojo("myname", jsonObj));
    String jsonStr = node.toString();

I am getting jsonStr value as:

{"sName":"myname","tJsonObj":{"key":{"integral":true,"valueType":"NUMBER"}}}

How can I get jsonStr value as:

{"sName":"myname","tJsonObj":{"key":123}}

JsonUtils.toJsonObject is my own utility method to get JsonObject from String.

mkag
  • 106
  • 12
  • 2
    There are multiple `JsonObject ` , you didn't specified which one. It will be better to use fasterxml's `ObjectNode` instead of whatever `JsonObject` you are using – Hemant Patel Sep 16 '20 at 12:50
  • It is `javax.json.JsonObject` , I can not change the POJO class. – mkag Sep 21 '20 at 06:17
  • 1
    If you are confident of your JsonUtils.toJsonObject then do not use com.fasterxml.jackson.databind.ObjectMapper – Alexandr Ivanov Oct 12 '20 at 11:25
  • sorry, I didn't get you. My problem is simple to get the json string from JAVA POJO object which may have javax.json.JsonObject as member variable. If not to use com.fasterxml.jackson.databind.ObjectMapper, then what else. I don't want my own implementation of toString() in side my JAVA POJO. – mkag Oct 17 '20 at 11:46

0 Answers0