title: Custom deserialization into a Map using Gson
body: <p>I have the JSON below:</p>
  
  <pre><code>{
      "abc": {
          "linkedTo": "count",
          // possibly more data...
      },
      "plmtq": {
          "linkedTo": "title",
          "decode": "true",
          // possibly more data...
      }
  }
  </code></pre>
  
  <p>I need to load this JSON into a <code>Map&lt;String, Holder&gt;</code>, with keys <code>"abc"</code> and <code>"plmtq"</code>.<br>
  Below is my <code>Holder</code> class:</p>
  
  <pre><code>public class Holder {
    private final Map&lt;String, String&gt; data;
  
    public Holder(Map&lt;String, String&gt; data) {
      this.data = data;
    }
  
    // More methods in this class...
  }
  </code></pre>
  
  <p><strong>The JSON does not match my class structure</strong>, but I cannot change the JSON or the classes.<br>
  What I need is a way to customize the deserialization so I can parse the JSON into a <code>Map&lt;String, Holder&gt;</code>.<br>
  Is there any way to do this?</p>
  
  <hr>
  
  <p>Below is the code which does that, but it looks too complex and there must be a simpler way to do it...</p>
  
  <pre><code>private static Map&lt;String, Holder&gt; getMap(String jsonLine)
  {
      Map&lt;String, Holder&gt; holder = new HashMap&lt;&gt;();
      JsonElement jelement = new JsonParser().parse(jsonLine);
      JsonObject jobject = jelement.getAsJsonObject();
  
      for (Map.Entry&lt;String, JsonElement&gt; entry : jobject.entrySet())
      {
          Map&lt;String, String&gt; metadata = new HashMap&lt;&gt;();
          JsonObject element = entry.getValue().getAsJsonObject();
  
          for (Map.Entry&lt;String, JsonElement&gt; entry2 : element.entrySet())
          {
              metadata.put(entry2.getKey(), entry2.getValue().getAsString());
          }
  
          holder.put(entry.getKey(), new Holder(metadata));
      }
      return holder;
  }
  </code></pre>