Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With GSON, assuming that the class you are deserializing into has fields for all the names that appear in the JSON, the fields not found in the JSON will just be left null:</p> <p><a href="https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects" rel="nofollow">https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects</a></p> <p>"While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null"</p> <p>Things get a little more complicated if arbitrary field names are allowed in the JSON - for example, if Point allows c1, c2, ... cn. But you can handle this with a custom deserializer.</p> <p><a href="https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer" rel="nofollow">https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer</a></p> <p><strong>Edit:</strong></p> <p>Here's how you might write a custom deserializer for Point:</p> <pre><code>private class DateTimeDeserializer implements JsonDeserializer&lt;Point&gt; { public Point deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { List&lt;PointPart&gt; parts = Lists.newArrayList(); for(Map.Entry&lt;String,JsonElement&gt; entry : json.getAsJsonObject().entrySet()) { char type = ; int index = Integer.parseInt(entry.getKey().substring(1)) - 1; while(parts.size() &lt;= index) { parts.add(new PointPart()); } PointPart part = parts.get(index); switch(entry.getKey().charAt(0)) { case 'c': part.c = entry.getValue().getAsBoolean(); break; case 'k': part.k = entry.getValue().getAsInt(); break; } } return new Point(parts); } } class Point { List&lt;PointPart&gt; parts; Point(List&lt;PointPart&gt; parts) { this.parts = parts; } } class PointPart { boolean c; int k; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload