title: Deserialising string list in GSON?
body: <p>I am trying to save a string list in a jsonobject and deserialise it on load. When I am saving the list, I am creating a JsonObject and adding it as a property by using the GSON.toJson. I have created a String arraylist serialiser to serialise the item and when I check my mongo database it appears to be saving as a jsonArray but when I try to deserialise it, it tells me that it is not a JsonArray.</p>
  <p>Here is my code:</p>
  <p>Order object:</p>
  <pre><code>import lombok.Getter;
  import lombok.RequiredArgsConstructor;
  import lombok.Setter;
  
  import java.time.LocalDate;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.UUID;
  
  @Getter
  @Setter
  @RequiredArgsConstructor
  public class Order {
  
      private final UUID id;
      private final String table;
  
      private short numberOfPeople;
      private LocalDate timeOfOrder = LocalDate.now();
  
      private String staffInCharge = &quot;&quot;;
  
      private List&lt;String&gt; order = new ArrayList&lt;&gt;();
      private String note = &quot;&quot;;
  }
  </code></pre>
  <p>The @RequiredArgumentsConstructor just creates a constructor for me so I dont have to do public Order() etc</p>
  <p>Imports of the Serialise and Deserialise class:</p>
  <pre><code>import com.google.gson.*;
  import lombok.Getter;
  import lombok.RequiredArgsConstructor;
  import me.oscar.orderbird.OrderBird;
  import me.oscar.orderbird.gson.GsonUtil;
  import me.oscar.orderbird.mongo.MongoUtils;
  import me.oscar.orderbird.profile.food.Food;
  import me.oscar.orderbird.profile.order.Order;
  import me.oscar.orderbird.profile.staff.Staff;
  import me.oscar.orderbird.profile.table.Table;
  import org.bson.Document;
  
  import java.util.*;
  </code></pre>
  <p>Serialise:</p>
  <pre><code>        if (!this.orders.isEmpty()){
              JsonArray orderArray = new JsonArray();
              this.orders.values().forEach(order -&gt; {
                  JsonObject ordersObect = new JsonObject();
                  ordersObect.addProperty(&quot;id&quot;, order.getId().toString());
                  ordersObect.addProperty(&quot;name&quot;, order.getTable());
                  ordersObect.addProperty(&quot;note&quot;, order.getNote());
                  ordersObect.addProperty(&quot;numberOfPeople&quot;, order.getNumberOfPeople());
                  ordersObect.addProperty(&quot;staffInCharge&quot;, order.getStaffInCharge());
                  ordersObect.addProperty(&quot;orderItems&quot;, GsonUtil.GSON.toJson(order.getOrder()));
                  orderArray.add(ordersObect);
              });
              document.put(&quot;orders&quot;, orderArray.toString());
  
          }
  </code></pre>
  <p>Deserialise:</p>
  <pre><code>        if (document.containsKey(&quot;orders&quot;)) {
              if (document.get(&quot;orders&quot;) instanceof String) {
                  JsonArray ordersArray = PARSER.parse(document.getString(&quot;orders&quot;)).getAsJsonArray();
  
                  for (JsonElement jsonElement : ordersArray) {
                      JsonObject jsonObject = jsonElement.getAsJsonObject();
                      try {
                          Order order = new Order(UUID.fromString(jsonObject.get(&quot;id&quot;).getAsString()), jsonObject.get(&quot;name&quot;).getAsString());
                          order.setNote(jsonObject.get(&quot;note&quot;).getAsString());
                          order.setNumberOfPeople(jsonObject.get(&quot;numberOfPeople&quot;).getAsShort());
                          order.setStaffInCharge(jsonObject.get(&quot;staffInCharge&quot;).getAsString());
                          order.setOrder(GsonUtil.GSON.fromJson(jsonObject.get(&quot;orderItems&quot;).getAsJsonObject(), ArrayList.class));
                          this.orders.put(jsonObject.get(&quot;name&quot;).getAsString(), order);
  
                      } catch (Exception ex) {
                          ex.printStackTrace();
                      }
                  }
              }
          }
  </code></pre>
  <p>ArrayListAdapter:</p>
  <pre><code>package me.oscar.orderbird.gson.impl;
  
  import com.google.gson.*;
  
  import java.lang.reflect.Type;
  import java.util.ArrayList;
  import java.util.List;
  
  public class StringArrayAdapter implements JsonDeserializer&lt;List&lt;String&gt;&gt;, JsonSerializer&lt;List&lt;String&gt;&gt; {
      @Override
      public List&lt;String&gt; deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
          JsonArray jsonArray = jsonElement.getAsJsonArray();
          List&lt;String&gt; values = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; jsonArray.size(); i++){
              values.set(i, jsonArray.get(i).getAsJsonPrimitive().getAsString());
          }
          return values;
      }
  
      @Override
      public JsonElement serialize(List&lt;String&gt; stringList, Type type, JsonSerializationContext jsonSerializationContext) {
          JsonArray jsonArray = new JsonArray();
          for (String string : stringList) {
              jsonArray.add(new JsonPrimitive(string));
          }
          return jsonArray;
      }
  }
  </code></pre>
  <p>When I attempt to set the order I am getting the error:</p>
  <pre><code>java.lang.IllegalStateException: Not a JSON Object: &quot;[\n  \&quot;Steak\&quot;\n]&quot;
  </code></pre>