title: Kotlin UnrecognizedPropertyException with Jackson's ObjectMapper.readValue()
body: <p>Hi I'm trying out Kotlin, and normally I would be able to use the ObjectMapper in Java. What I'm trying to do is store some KeyBind Array that contains data on a keybind and what it should to e.g. pressing the combination CTRL+SHIFT+X should "EXIT" but in Kotlin I seem to get:</p>
  
  <pre><code>com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "binds" (class Overlay.KeyBinds$Companion), not marked as ignorable (0 known properties: ])
  at [Source: (File); line: 1, column: 11] (through reference chain: Overlay.KeyBinds$Companion["binds"])
  
  at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
  at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
  at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
  at Overlay.KeyBinds$Companion.importer(KeyBinds.kt:51)
  at Overlay.KeyBindIOTest.importer(KeyBindIOTest.java:45)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>
  
  <p>The keybinds file that should be generated by the test seems to produce the output I want it to produce:</p>
  
  <pre><code>{"binds":[{"keyState":{"shiftHeld":true,"ctrlHeld":false,"altHeld":false,"key":30},"function":"EXIT"},{"keyState":{"shiftHeld":false,"ctrlHeld":false,"altHeld":false,"key":1},"function":"OPEN"}]}
  </code></pre>
  
  <p><strong>So, I'm not sure why it is failing on importing the Object from the file, anyone have any clues?</strong></p>
  
  <p>My Kotlin code:</p>
  
  <pre><code>data class KeyBind(var keyState: KeyState, var function: String)
  
  data class KeyBinds(var binds: Array&lt;KeyBind&gt;)
  {
  override fun equals(other: Any?): Boolean
  {
      if (this === other) return true
      if (javaClass != other?.javaClass) return false
  
      other as KeyBinds
  
      if (!Arrays.equals(binds, other.binds)) return false
  
      return true
  }
  
  override fun hashCode(): Int
  {
      return Arrays.hashCode(binds)
  }
  
  fun exporter(location: String)
  {
      var kb_file = File(location)
  
      if (kb_file.exists())
          kb_file.delete()
  
      var objectMapper = ObjectMapper()
      objectMapper.writeValue(kb_file, this)
  }
  
  companion object
  {
      fun importer(location: String): Companion?
      {
          var kb_file = File(location)
  
          if (kb_file.exists())
          {
              var objectMapper = ObjectMapper()
              return objectMapper.readValue(kb_file, KeyBinds.javaClass)
  
          }
          return null
      }
  }
  }
  </code></pre>
  
  <p>My JUnit test:</p>
  
  <pre><code>public class KeyBindIOTest
  {
  private KeyBinds keyBinds = new KeyBinds(new KeyBind[]
  {
          new KeyBind(new KeyState(true, false, false, 30), "EXIT"),
          new KeyBind(new KeyState(false, false, false, 1), "OPEN")
  });
  static final String TEST_LOCATION = System.getenv("LOCALAPPDATA") + File.separator + "TEST" + File.separator + "keybinds";
  
  @Before
  public void before()
  {
      // Create Test Dir if it doesn't exist.
      File f = new File(System.getenv("LOCALAPPDATA") + File.separator + "TEST");
      if (!f.exists())
          f.mkdir();
  }
  
  @Test
  public void exporter()
  {
      keyBinds.exporter(TEST_LOCATION);
  
      // Verify file was created
      File f = new File(TEST_LOCATION);
      assertTrue(f.exists());
  }
  
  @Test
  public void importer()
  {
      assertEquals(keyBinds, KeyBinds.Companion.importer(TEST_LOCATION));
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  I fixed this particular error by changing:</p>
  
  <pre><code>return objectMapper.readValue(kb_file, KeyBinds.javaClass)
  </code></pre>
  
  <p>to:</p>
  
  <pre><code> return objectMapper.readValue(kb_file, KeyBinds::class.java)
  </code></pre>
  
  <p>It seems using .javaClass will tell the ObjectMapper the class is Companion instead of what you wanted it to be.</p>
  
  <p>Unfortunatelly that's not the end of the errors now I end up with:</p>
  
  <pre><code>com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `Overlay.KeyBinds` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
   at [Source: (File); line: 1, column: 2]
  
  at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
  at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
  at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
  at Overlay.KeyBinds$Companion.importer(KeyBinds.kt:53)
  at Overlay.KeyBindTest.importer(KeyBindTest.java:44)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>