title: Gson [java] - when parsing json string, I just want to add some characters to it
body: <p>I define "Response", "Person", "Course" class :</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>class Response {
      private int countA; // record the length of data
      private List&lt;Person&gt; data;
  
      // getter &amp; setter
  }
  
  class Person {
      private String name;
      private int age;
      private int countB; // record the length of list
      private List&lt;Course&gt; list;
  
      // getter &amp; setter
  }
  
  class Course {
      private String name;
  
      // getter &amp; setter
  }
  </code></pre>
  
  <p>And I have a json string (return from some server) like this :</p>
  
  <pre class="lang-java prettyprint-override"><code>{
     "countA":1,
     "data":
     {
        "name":"peter",
        "age":18,
        "countB":1,
        "list":
        {
           "name":"math"
        }
     }
  }
  </code></pre>
  
  <p>But when the "countA" > 1 or "countB" > 1 , it will be like this :</p>
  
  <pre class="lang-java prettyprint-override"><code>{
     "countA":2,
     "data":
     [                                // when countA &gt; 1, it have this one '['
        {
           "name":"amy",
           "age":17,
           "countB":1,
           "list":
           {
              "name":"music"
           }
        },
        {
           "name":"david",
           "age":16,
           "countB":2,
           "list":
           [                          // when countB &gt; 1, it have this one '['
              {
                 "name":"music"
              },
              {
                 "name":"computer"
              }
           ]                          // when countB &gt; 1, it have this one ']'
        }
     ]                                // when countA &gt; 1, it have this one ']'
  }
  </code></pre>
  
  <p>So, I want to customize my deserializer which just doing add two characters ' [ ' &amp; ' ] ' to data or list when countA or countB = 1 respectively :</p>
  
  <pre class="lang-java prettyprint-override"><code>class MyDeserializer implements JsonDeserializer&lt;List&gt;{
  
      public List deserialize(JsonElement json, Type typeOfT,
          JsonDeserializationContext context) throws JsonParseException {
  
  
          String jsonString = json.toString();
  
          // fix json string to my expected format
          if(!(jsonString.startsWith("[") &amp;&amp; jsonString.endsWith("]")))
              jsonString = "["+jsonString+"]";
  
          // get the XXX type from List&lt;XXX&gt;
          Type valueType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
  
          // call default deserializer
          return context.deserialize(new Gson().fromJson(jsonString,JsonElement.class), valueType);
      }
  }
  </code></pre>
  
  <p>In my main() :</p>
  
  <pre class="lang-java prettyprint-override"><code>String jsonStrng = ...; // return from some server
  Response bean = (Response) fromJson(jsonString, Response.class); // get the following error
  </code></pre>
  
  <p>Method fromJson() :</p>
  
  <pre class="lang-java prettyprint-override"><code>@SuppressWarnings({ "unchecked", "rawtypes" })
  public static Object fromJson(String data, Class clazz) {
  
      Gson gson = new GsonBuilder().registerTypeAdapter(List.class, new MyDeserializer()) .create();
  
      Object obj = gson.fromJson(data, clazz);
  
      gson = null;
      return obj;
  }
  </code></pre>
  
  <p>But I have the error :</p>
  
  <pre class="lang-java prettyprint-override"><code>Expected BEGIN_OBJECT but was BEGIN_ARRAY
  </code></pre>
  
  <p>I know the reason why I get this error :</p>
  
  <ol>
  <li>json string have two level List type</li>
  <li>in my deserializer, finally I call the default deserializer</li>
  </ol>
  
  <p>But instead of calling the default deserializer, if I try to call my deserializer again, it causes the infinite loop.</p>
  
  <p>My question is how do I fix it ?</p>
  
  <p>I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.</p>
  
  <p>Thanks in advance.</p>