首页 > 代码库 > Gson转换时,Double转式化

Gson转换时,Double转式化

 1 package com.mall.core; 2  3 import java.lang.reflect.Type; 4 import java.text.DecimalFormat; 5  6 import com.google.gson.JsonElement; 7 import com.google.gson.JsonPrimitive; 8 import com.google.gson.JsonSerializationContext; 9 import com.google.gson.JsonSerializer;10 11 /**12  * Gson转换时,Double转式化13  * 14  *15  */16 public class JsonDoubleSerializer implements JsonSerializer<Double> {17     18     private String doublePattern;19     20     public JsonDoubleSerializer(String doublePattern) {21         this.doublePattern = doublePattern;22     }23         24     public JsonElement serialize(Double arg0, Type arg1,25             JsonSerializationContext arg2) {26         return new JsonPrimitive(new DecimalFormat(doublePattern).format(arg0));27     }28 }

 

Gson转换时,Double转式化