1

I tried to map LinkedHashMap in JSON in my kotlin testcase. while I am converting json to object using jackson-objectmapper, I have got below issue, Kindly help me to solve this.

construct instance of java.util.LinkedHashMap (although at least one 
Creator exists): no String-argument constructor/factory method to 
deserialize from String value ('{e123f1dd-6c54-4b01-90e6- 
d701748f0851=class MyClass {

My Kotlin testcase below

fun `testcase success`() {

val mapId = UUID.fromString("e123f1dd-6c54-4b01-90e6-d701748f0851");
  val Idno = UUID.randomUUID();

  val order = MyClass();

  val map : LinkedHashMap<String, MyClass> = LinkedHashMap();

  order.collectiveSignature=true
  order.docId="5436576"
  order.status=OnboardOrderPoaStatusTPL.NOT_SIGNED
  order.createdAt=createdDate
  order.updatedAt=updatedDate
  order.portingAt=OffsetDateTime.parse("2018-09-21T14:22:24.682+02:00");
  order.fastPorting=true
  order.owner=MyAnotherClass();
  order.owner.company="test"
  order.owner.firstName="test"
  order.owner.lastName="test"
  order.owner.city="test"
  order.owner.zip="5436"
  order.owner.street="test"
  order.owner.house="test"


  map["e123f1dd-6c54-4b01-90e6-d701748f0851"] = order;

  val body = """{

          "mapId": "$mapId",
          "Idno": "$Idno",
          "name": "Abishek",
          "poas":"$map"

         }""".trimIndent()

  val mapper = ObjectMapper()
  mapper.findAndRegisterModules()

  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
  mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, false);


  val input = mapper.readValue<MainClass>(body)


}

1 Answer 1

1

The problem is poas value contains String representation of Map, which contains String representation of MyClass. Indeed, insead of creating Json representation you called MyClass.toString() and purpose of toString is show debug information.

How to fix it?

Using ObjectMapper you should convert Map to json, instead of using toString()

  val body = """{

          "mapId": "$mapId",
          "Idno": "$Idno",
          "name": "Abishek",
          "poas": "${mapper.writeValue(map)}"

         }""".trimIndent()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.