title: Gson and Jackson incompatibility
body: <p>Currently I'm working on two projects: one is using Java 1.5 and Spring 3.2.3, and the other is using Java 1.8 and Spring 4.1.5. As you may know, since Spring 4.x.x default <code>HttpMessageConverter</code> is Gson. Prior to version 4.x.x default was Jackson. I thought this will not influence compatibility, but lo and behold: it does... Specifically I'm talking about <code>java.util.Date</code> parsing/mapping.</p>
  
  <p>Assume that I have a class as below (<code>equals()</code> and <code>hashCode()</code> methods are ommited):</p>
  
  <pre><code>public class FooBar {
      public String str = getRandomStringEncodedInUTF8();
      public Date date = new Date(0); //1970-01-01 00:00:00
  }
  
  public void retrieveFooBars() {
      Type collectionType = new TypeToken&lt;HashSet&lt;FooBar&gt;&gt;(){}.getType();
      Gson gson = new GsonBuilder().
          registerTypeAdapter(FooBar.class, new FooBarDeserializer()).create();
  
      String ipAddress = getIpAddress();
      RestTemplate request = new RestTemplate();
  
      String gsonString;
      try {
          gsonString = request.getForObject("http://"
              + ipAddress+ ":8080/foobars/get", String.class);
      } catch (Exception e) {
          return;
      }
  
      Set&lt;FooBar&gt; fooBars = gson.fromJson(gsonString, collectionType);
  }
  </code></pre>
  
  <p><code>FooBarDeserializer</code> is as follows:</p>
  
  <pre><code>public class FooBarDeserializer implements JsonDeserializer&lt;FooBar&gt; {
  
      private final DateTimeFormatter dateFormatter = DateTimeFormat
          .forPattern("yyyy-MM-dd");
  
      @Override
      public FooBar deserialize(JsonElement element, Type type,
              JsonDeserializationContext context) throws JsonParseException {
  
          FooBar fooBar = new FooBar();
          JsonObject obj = element.getAsJsonObject();
  
          JsonElement str = obj.get("str");
          if (str != null)
          if (!str.isJsonNull()) {
              fooBar.str = originalId.getAsString();
          }
  
          JsonElement date = obj.get("date");
          if (date != null)
          if (!date.isJsonNull()) {
              String temp = date.getAsString();
  
              if (temp != null) {
                  fooBar.date = dateFormatter.parseDateTime(temp).toDate();
              }
          }
          return FooBar;
      }
  }
  </code></pre>
  
  <p>If I don't use <code>FooBarDeserializer</code> I get <code>com.google.gson.JsonSyntaxException: 1970-01-01</code> caused by: <code>java.text.ParseException: Unparseable date: "1970-01-01"</code> So far so good. I can receive data, plus keep my UTF-8 encoding.</p>
  
  <p>Problem is when sending data to server with older version of Spring:</p>
  
  <pre><code>public void distributeFooBars() {
      Gson gson = new GsonBuilder()
          .registerTypeAdapter(FooBar.class, new FooBarSerializer()).create();
  
      String ipAddress = getIpAddress();
      Set&lt;FooBar&gt; newFooBars = getNewFooBars();
  
      RestTemplate request = new RestTemplate();
      String gsonString = gson.toJson(newFooBars);
      try {
          request.postForObject("http://" + ipAddress + ":8080/foobars/set",
              gsonString, Boolean.class);
      } catch (Exception e) {
          return;
      }
  }
  </code></pre>
  
  <p>If I don't use explicitly Gson prior to sending newFooBars to "older" server, and let <code>GsonHttpMessageConverter</code> to do it's job, I preserve my UTF-8 string, but parsing the Date fails since it's written as "jan 1, 1970". If I do explicitly use Gson and converting newFooBars to String prior to sending them, I lose UTF-8 characters (i.e. instead of ČĆĐŠŽ_čćđšž i would receive ?????_?????) but parsing the date doesn't throw an Exception. Customer serializer is:</p>
  
  <pre><code>public class FooBarSerializer implements JsonSerializer&lt;FooBar&gt; {
  
      private final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
  
      @Override
      public JsonElement serialize(FooBar fooBar, Type type,
              JsonSerializationContext context) {
  
          JsonObject obj = new JsonObject();
          obj.addProperty("str", fooBar.str);
  
          String date = null;
          if (fooBar.date != null) {
              date = formatter.print(fooBar.date.getTime());
          }
          obj.addProperty("date", date);
  
          return obj;
      }
  }
  </code></pre>
  
  <p>SIDE NOTE: if using Gson explicitly, on older server controller has the following mapping:</p>
  
  <pre><code>@RequestMapping(value = "/set", method = RequestMethod.POST, consumes="text/plain;charset=UTF-8")
  </code></pre>
  
  <p>else it's:</p>
  
  <pre><code>@RequestMapping(value = "/set", method = RequestMethod.POST, consumes="application/json;charset=UTF-8")
  </code></pre>
  
  <p>Otherwise I get 415 Unsupported media type.</p>
  
  <p>I know it has been a long post, but I would be grateful to anyone that can help me. So my <strong>QUESTION</strong> is:</p>
  
  <p><strong>Is there a way to solve this problem? Like having a system wide Gson serializer but just for <code>java.util.Date</code>. Or setting that forces UTF-8 encoding for plain/text?</strong></p>