title: How to deserialize a JSON string to a non-public class using JSON-B?
body: <p>I've created a trival Java 9 Maven app with two classes to test the serialization and deserialization of JSON using JSON-B. Here's the code:</p>
  
  <pre><code>package com.jsonbdemos;
  import javax.json.bind.Jsonb;
  import javax.json.bind.JsonbBuilder;
  import javax.json.bind.JsonbConfig;
  
  public class App {
  
      public static void main(String[] args) {
          Jsonb jsonb = JsonbBuilder.create(new JsonbConfig());
          String jsonData = "{\"creationDate\":\"2018-01-05\"}";
  
          // Create Widget object from JSON string.
          Widget widget = jsonb.fromJson(jsonData, Widget.class);
          System.out.println("JSON =&gt; object: " + widget.toString());
  
          // Serialize Widget object to JSON string.
          String jsonFromObject = jsonb.toJson(widget);
          System.out.println("object =&gt; JSON: " + jsonFromObject);
      }
  }
  
  package com.jsonbdemos;
  import java.time.LocalDate;
  
  public class Widget { // IllegalAccessException if "public" is removed.
      private LocalDate creationDate;
      public Widget() {}
  
      @Override
      public String toString() { return "creationDate=" + creationDate; }
      public LocalDate getCreationDate() { return creationDate; }
      public void setCreationDate(LocalDate creationDate) { this.creationDate = creationDate; }
  }
  </code></pre>
  
  <p>There is a dependency for the latest version of the reference implementation of JSON-B (<a href="https://projects.eclipse.org/projects/ee4j.yasson" rel="nofollow noreferrer"><em>Eclipse Yasson</em></a>) in pom.xml:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.glassfish&lt;/groupId&gt;
    &lt;artifactId&gt;javax.json&lt;/artifactId&gt;
    &lt;version&gt;[1.1.2,)&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;javax.json.bind&lt;/groupId&gt;
    &lt;artifactId&gt;javax.json.bind-api&lt;/artifactId&gt;
    &lt;version&gt;[1.0,)&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.eclipse&lt;/groupId&gt;
    &lt;artifactId&gt;yasson&lt;/artifactId&gt;
    &lt;version&gt;[1.0.0,)&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>The app runs fine, but if I change the access level of class <strong>Widget</strong> from <strong>public</strong> to nothing (i.e. "package private") an
  IllegalAccessException is thrown when calling <strong>Jsonb.fromJson()</strong>:</p>
  
  <blockquote>
    <p>Exception in thread "main" javax.json.bind.JsonbException: Can't
    create instance   at
    org.eclipse.yasson.internal.ReflectionUtils.lambda$createNoArgConstructorInstance$1(ReflectionUtils.java:191)
    at java.base/java.security.AccessController.doPrivileged(Native
    Method)   at
    org.eclipse.yasson.internal.ReflectionUtils.createNoArgConstructorInstance(ReflectionUtils.java:186)
    at
    org.eclipse.yasson.internal.serializer.ObjectDeserializer.getInstance(ObjectDeserializer.java:92)
    at
    org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:62)
    at
    org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:57)
    at
    org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:50)
    at
    org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
    at
    org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:52) 
    at com.jsonbdemos.App.main(App.java:15) <strong>Caused by:
    java.lang.IllegalAccessException: class
    org.eclipse.yasson.internal.ReflectionUtils cannot access a member of
    class com.jsonbdemos.Widget with modifiers "public"</strong>   at
    java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
    at
    java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:589)
    at
    java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:479)
    at
    org.eclipse.yasson.internal.ReflectionUtils.lambda$createNoArgConstructorInstance$1(ReflectionUtils.java:189)
    ... 9 more</p>
  </blockquote>
  
  <p>I don't see anything in the spec (<a href="https://jcp.org/en/jsr/detail?id=367" rel="nofollow noreferrer">JSR 367:"JSON-B: Java™ API for JSON Binding"</a>) (in section <strong>3.7 Java Class</strong>) requiring a public class for deserialization. </p>
  
  <p>Any suggestions on how to deserialize to a class instance which isn't public using JSON-B?</p>
  
  <p><strong>Update (5/2/18):</strong> </p>
  
  <p>JSR 367 states that the "<em>any instance passed to a deserialization operation must have a public or protected no-argument constructor</em>", yet the same error also occurs if the constructor is <strong>protected</strong> rather than <strong>public</strong>. </p>
  
  <p>I have reported that issue: <a href="https://github.com/eclipse-ee4j/yasson/issues/118" rel="nofollow noreferrer">Deserialization still not working with a protected no-arg constructor #118</a></p>