title: How to build a protocol buffer message from a json using Gson?
body: <p>I've been trying to generate protocol buffer messages from a json string using gson. Does anyone know how if it is possible to do it?</p>
  
  <p>I have already tried:</p>
  
  <pre><code>Gson gson = new Gson();
  Type type = new TypeToken&lt;List&lt;PROTOBUFFMESSAGE.Builder&gt;&gt;() {}.getType();
  List&lt;PROTOBUFFMESSAGE.Builder&gt; list = (List&lt;PROTOBUFFMESSAGE.Builder&gt;) gson.fromJson(aJsonString, type);
  </code></pre>
  
  <p><strike>and</p>
  
  <pre><code>Gson gson = new Gson();
  Type type = new TypeToken&lt;List&lt;PROTOBUFFMESSAGE&gt;&gt;() {}.getType();
  List&lt;PROTOBUFFMESSAGE&gt; list = (List&lt;PROTOBUFFMESSAGE&gt;) gson.fromJson(aJsonString, type);
  </code></pre>
  
  <p></strike></p>
  
  <p>The message inside the json uses the same names as in the protocol buffer i.e:</p>
  
  <pre><code>message PROTOBUFFMESSAGE {
     optional string this_is_a_message = 1;
     repeated string this_is_a_list = 2;
  }
  </code></pre>
  
  <p>will lead to a json:</p>
  
  <pre><code>[
      {
          "this_is_a_message": "abc",
          "this_is_a_list": [
              "123",
              "qwe"
          ]
      },
      {
          "this_is_a_message": "aaaa",
          "this_is_a_list": [
              "foo",
              "bar"
          ]
      }
  ]
  </code></pre>
  
  <p>Although a list with the correct number of PROTOBUFFMESSAGE gets generated, they contain all their fields to null, so I'm not sure if this is a problem with the mapping, the reflection system not detecting protobuffs fields or something else. If anyone know how to do this it would be great. Btw I'm talking about java here.</p>
  
  <h1>EDIT:</h1>
  
  <p>changing the names in the json to:</p>
  
  <pre><code>        {
              "thisIsAMessage_": "abc",
              "thisIsAList_": [
                  "123",
                  "qwe"
              ]
          }
  </code></pre>
  
  <p>Makes the de serialization happen. And it does work except for the list that throws:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Can not set com.google.protobuf.LazyStringList field Helper$...etc big path here...$PROTOBUFFMESSAGE$Builder.thisIsAList_ to java.util.ArrayList
  </code></pre>