title: Genson serialization Issue with byte and Date fields
body: <p>Hi I am using Genson for ser/de for my POJO classes. But there is a problem while serializing a POJO which has a byte field. I haven't tried deserialization of same object. So I don't know whether that's also working.
  Also there is problem in date field. </p>
  
  <p>Here is my POJO</p>
  
  <pre><code>public class User implements Serializable{
  
      private String userId;
      private String emailId;
      private Date lastLogin;
      private Byte attempts;
  
      //geters and setters, toString methods.
  }
  </code></pre>
  
  <p>I got object with values and when I print I got like following.</p>
  
  <pre><code>User [userId=sss, emailId=ssss@gmail.com, lastLogin=2014-12-21 23:24:46.0, attempts=9]
  </code></pre>
  
  <p>I tried the following code for serialization with Genson (Sometimes I need to serialize by removing some fields, so I'm using the following code).</p>
  
  <pre><code>public String serialize(Object object, Class cls,
              String[] ignoreList) {
          GensonBuilder builder = new Genson.Builder();
          if (null != ignoreList) {
              for (String field : ignoreList) {
                  builder.exclude(field, cls);
              }
          }
          Genson genson = builder.create();
          return genson.serialize(object);
      }
  </code></pre>
  
  <p>But I get the following JSON string, byte field value is different. Date field value is without time stamp.</p>
  
  <pre><code>{"emailId":"sss@gmail.com","lastLogin":"21 Dec, 2014","attempts":"CQ==","userId":"sss"}
  </code></pre>
  
  <p>I see the byte problem is fixed already with <a href="https://github.com/owlike/genson/issues/38" rel="nofollow">this Issue in Github</a>. But How do I get the updated code in my project? I'm using Maven and I'm using latest version of Genson-1.2.</p>
  
  <p>Should I try <code>builder.useByteAsInt(true);</code> in my code?</p>
  
  <p>Also for Date field. I see two options</p>
  
  <pre><code>builder.useDateAsTimestamp(true);
  builder.useDateFormat(dateFormat);
  </code></pre>
  
  <p>I don't want to Set these things. Because sometimes the field may be coming with timestamp and sometimes it will be without timestamp and date formates can be different for different objects. I'm lookiung for a solution which will serialize date field and convert to string as it is.</p>
  
  <p>What is the best way to get the proper JSON string? I'm looking for a solution which can serialize deserialize any POJO class. </p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I have created ByteConverter and Date Converter.</p>
  
  <p>These are my converters
  <strong>ByteConverter.java</strong> </p>
  
  <pre><code>@HandleClassMetadata
  @HandleBeanView
  public final class ByteConverter implements Converter&lt;Byte&gt; {
      public final static ByteConverter instance = new ByteConverter();
  
      private ByteConverter() {
      }
  
      public void serialize(Byte obj, ObjectWriter writer, Context ctx) {
          writer.writeValue(obj.byteValue());
      }
  
      public Byte deserialize(ObjectReader reader, Context ctx) {
          return (byte) reader.valueAsInt();
      }
  }
  </code></pre>
  
  <p><strong>DateConverter.java</strong></p>
  
  <pre><code>@HandleClassMetadata
  @HandleBeanView
  public class DateConverter implements Converter&lt;Date&gt; {
      private DateFormat dateFormat;
      private final boolean asTimeInMillis;
  
      public DateConverter() {
          this(SimpleDateFormat.getDateInstance(), false);
      }
  
      public DateConverter(DateFormat dateFormat, boolean asTimeInMillis) {
          if (dateFormat == null)
              dateFormat = SimpleDateFormat.getDateInstance();
          this.dateFormat = dateFormat;
          this.asTimeInMillis = asTimeInMillis;
      }
  
      public void serialize(Date obj, ObjectWriter writer, Context ctx) {
          if (asTimeInMillis)
              writer.writeValue(obj.getTime());
          else
              writer.writeUnsafeValue(format(obj));
      }
  
      protected synchronized String format(Date date) {
          return dateFormat.format(date);
      }
  
      public Date deserialize(ObjectReader reader, Context ctx) {
          try {
              if (asTimeInMillis)
                  return new Date(reader.valueAsLong());
              else
                  return read(reader.valueAsString());
          } catch (ParseException e) {
              throw new JsonBindingException("Could not parse date "
                      + reader.valueAsString(), e);
          }
      }
  
      protected synchronized Date read(String dateString) throws ParseException {
          return dateFormat.parse(dateString);
      }
  }
  </code></pre>
  
  <p>In my method I tried following code</p>
  
  <pre><code>public void serialize(User user) {
      Converter&lt;Byte&gt; byteConverter = ByteConverter.instance;
                  Converter&lt;Date&gt; dateConverter = new DateConverter();
                  Converter[] converters = { byteConverter, dateConverter };
                  GensonBuilder builder = new GensonBuilder();
                  builder.withConverters(converters);
                  Genson genson = builder.create();
                  String str = genson.serialize(user);
                  System.out.println(str);
  }
  </code></pre>
  
  <p>I get the following JSON string</p>
  
  <pre><code>{"emailId":"sss@gmail.com","lastLogin":"21 Dec, 2014","attempts":"9","userId":"sss"}
  </code></pre>
  
  <p>Byte problem is solved. But Date problem is still there. I tried following derialize method of DateConverter class. But it also didn't work.</p>
  
  <pre><code>public Date deserialize(ObjectReader reader, Context ctx) {
          try {
              if (reader.getValueType() == ValueType.INTEGER) 
                   return new Date(reader.valueAsLong());
              else 
                   return dateFormat.parse(reader.valueAsString());
          } catch (ParseException e) {
              throw new JsonBindingException("Could not parse date "
                      + reader.valueAsString(), e);
          }
      }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>My Updated DateConverter.java. DateUtils is from Apache commons. I haven't tested deserialize. But Serialize is working. I wanted the format with Timezone also. So I did in my customized way.</p>
  
  <pre><code>@HandleClassMetadata
  @HandleBeanView
  public class DateConverter implements Converter&lt;Date&gt; {
  
      private static final String YYYY_MM_DD_HH_MM_SS_Z = "yyyy-MM-dd HH:mm:ss z";
      private static final String YYYY_MM_DD_SLASH = "yyyy/MM/dd";
      private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
      private static final String YYYY_MM_DD_HH_MM_SS_ZZZ = "yyyy-MM-dd HH:mm:ss zzz";
      private static final String DD_MMM_YY_HH_MM_SS_SSSSSS_AAA = "dd-MMM-yy hh.mm.ss.SSSSSS aaa";
      private static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
      private static final String YYYY_MM_DD = "yyyy-MM-dd";
      private static final String YYYY_MM_DD_HH_MM_SS_0 = "yyyy-MM-dd HH:mm:ss.'0'";
      private static final String EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY = "EEE MMM dd HH:mm:ss zzz yyyy";
  
      String[] dateFormats = { EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY,
              YYYY_MM_DD_HH_MM_SS_0, YYYY_MM_DD, YYYY_MM_DD_HH_MM_SS_SSS,
              DD_MMM_YY_HH_MM_SS_SSSSSS_AAA, YYYY_MM_DD_HH_MM_SS_ZZZ,
              YYYY_MM_DD_HH_MM_SS, YYYY_MM_DD_SLASH };
  
      private DateFormat dateFormat;
      private final boolean asTimeInMillis;
  
      public DateConverter() {
          this(SimpleDateFormat.getDateInstance(), false);
      }
  
      public DateConverter(DateFormat dateFormat, boolean asTimeInMillis) {
          if (dateFormat == null)
              dateFormat = SimpleDateFormat.getDateInstance();
          this.dateFormat = dateFormat;
          this.asTimeInMillis = asTimeInMillis;
      }
  
      public void serialize(Date obj, ObjectWriter writer, Context ctx) {
          if (asTimeInMillis)
              writer.writeValue(obj.getTime());
          else
              writer.writeUnsafeValue(format(obj));
      }
  
      protected synchronized String format(Date date) {
          return getDateTime(YYYY_MM_DD_HH_MM_SS_Z, date);
          // return dateFormat.format(date);
      }
  
      public Date deserialize(ObjectReader reader, Context ctx) {
          try {
              if (asTimeInMillis)
                  return new Date(reader.valueAsLong());
              else
                  return read(reader.valueAsString());
          } catch (ParseException e) {
              throw new JsonBindingException("Could not parse date "
                      + reader.valueAsString(), e);
          }
      }
  
      protected synchronized Date read(String dateString) throws ParseException {
          return DateUtils.parseDate(dateString, dateFormats);
  //      return dateFormat.parse(dateString);
      }
  
      public String getDateTime(String aMask, Date aDate) {
          SimpleDateFormat df = null;
          String returnValue = "";
  
          if (aDate != null) {
              df = new SimpleDateFormat(aMask);
              returnValue = df.format(aDate);
          }
          return returnValue;
      }
  }
  </code></pre>