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

com.alogic.remote.call.BuilderFactory Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.alogic.remote.call;

import java.util.List;
import java.util.Map;

/**
 * 序列化工具工厂
 * 
 * @author duanyy
 *
 * @since 1.2.9
 * 
 * @version 1.6.8.13 [duanyy 20170427] 
* - 从alogic-remote中迁移过来
*/ public class BuilderFactory { /** * 基于Double类的序列化 */ public static final Builder DOUBLE = new Builder(){ public Object serialize(String id, Double o) { return o; } public Double deserialize(String id, Object json) { if (json instanceof Number){ return ((Number)json).doubleValue(); } if (json instanceof String){ try { return Double.parseDouble((String)json); }catch (Exception ex){ } } return null; } }; /** * 基于Float类的序列化 */ public static final Builder FLOAT = new Builder(){ public Object serialize(String id, Float o) { return o; } public Float deserialize(String id, Object json) { if (json instanceof Number){ return ((Number)json).floatValue(); } if (json instanceof String){ try { return Float.parseFloat((String)json); }catch (Exception ex){ } } return null; } }; /** * 基于Long类的序列化 */ public static final Builder LONG = new Builder(){ public Object serialize(String id, Long o) { return o; } public Long deserialize(String id, Object json) { if (json instanceof Number){ return ((Number)json).longValue(); } if (json instanceof String){ try { return Long.parseLong((String)json); }catch (Exception ex){ } } return null; } }; /** * 基于Integer类的序列化 */ public static final Builder INTEGER = new Builder(){ public Object serialize(String id, Integer o) { return o; } public Integer deserialize(String id, Object json) { if (json instanceof Number){ return ((Number)json).intValue(); } if (json instanceof String){ try { return Integer.parseInt((String)json); }catch (Exception ex){ } } return null; } }; /** * 基于Boolean类的序列化 */ public static final Builder BOOLEAN = new Builder(){ public Object serialize(String id, Boolean o) { return o; } public Boolean deserialize(String id, Object json) { if (json instanceof Number){ return ((Number)json).longValue() == 1; } if (json instanceof String){ try { return Boolean.parseBoolean((String)json); }catch (Exception ex){ } } return null; } }; /** * 基于String类的序列化 */ public static final Builder STRING = new Builder(){ public Object serialize(String id, String o) { return o; } public String deserialize(String id, Object json) { if (json instanceof String){ return (String)json; } return json.toString(); } }; /** * 基于List类的序列化 */ public static final Builder> LIST = new Builder>(){ public Object serialize(String id, List o) { return o; } @SuppressWarnings("unchecked") public List deserialize(String id, Object json) { if (json instanceof List){ return (List)json; } return null; } }; /** * 基于Map类的序列化 */ public static final Builder> MAP = new Builder>(){ public Object serialize(String id, Map o) { return o; } @SuppressWarnings("unchecked") public Map deserialize(String id, Object json) { if (json instanceof Map){ return (Map)json; } return null; } }; }