All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsonex.jsoncoder.coder.CoderDate Maven / Gradle / Ivy

There is a newer version: 0.1.27
Show newest version
/*************************************************************
 Copyright 2018-2019 eBay Inc.
 Author/Developer: Jianwu Chen

 Use of this source code is governed by an MIT-style
 license that can be found in the LICENSE file or at
 https://opensource.org/licenses/MIT.
 ************************************************************/

package org.jsonex.jsoncoder.coder;

import org.jsonex.core.factory.InjectableInstance;
import org.jsonex.jsoncoder.BeanCoderContext;
import org.jsonex.jsoncoder.BeanCoderException;
import org.jsonex.jsoncoder.ICoder;
import org.jsonex.jsoncoder.JSONCoderOption;
import org.jsonex.treedoc.TDNode;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.Date;

public class CoderDate implements ICoder {
  public static final InjectableInstance it = InjectableInstance.of(CoderDate.class);
  public static CoderDate get() { return it.get(); }

  @Override public Class getType() { return Date.class; }
  
  @Override public TDNode encode(Date obj, Type type, BeanCoderContext ctx, TDNode target) {
    String dateFormat = ctx.getOption().getDateFormat();
    return target.setValue(dateFormat == null ? obj.getTime() : ctx.getCachedDateFormat(dateFormat).format(obj));
  }

  @Override public Date decode(TDNode jsonNode, Type type, Object targetObj, BeanCoderContext ctx) { return _decode(jsonNode.getValue(), ctx); }
  
  static Date _decode(Object obj, BeanCoderContext ctx) {
    if (obj instanceof Number)
      return new Date(((Number) obj).longValue());

    JSONCoderOption opt = ctx.getOption();
    String dateStr = (String) obj;
    try {
      return ctx.getCachedDateFormat(opt.getDateFormat()).parse(dateStr);
    } catch(ParseException e) {
      try { 
        return opt.parseDateFullback(dateStr);
      } catch (ParseException e1) {
        throw new BeanCoderException(e1);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy