Search in sources :

Example 1 with DoubleSerializer

use of com.alibaba.fastjson.serializer.DoubleSerializer in project fastjson by alibaba.

the class DoubleTest_custom method test_1.

public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(Double.class, new DoubleSerializer("###.###"));
    Assert.assertEquals("1.123", JSON.toJSONString(1.123456789D, config));
}
Also used : DoubleSerializer(com.alibaba.fastjson.serializer.DoubleSerializer) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig)

Example 2 with DoubleSerializer

use of com.alibaba.fastjson.serializer.DoubleSerializer in project fastjson by alibaba.

the class DoubleTest_custom2 method test_0.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_0() throws Exception {
    Map values = new HashMap();
    Double v = 9.00;
    values.put("double", v);
    SerializeConfig config = new SerializeConfig();
    config.put(Double.class, new DoubleSerializer(new DecimalFormat("###.00")));
    Assert.assertEquals("{\"double\":9.00}", JSON.toJSONString(values, config));
}
Also used : DoubleSerializer(com.alibaba.fastjson.serializer.DoubleSerializer) HashMap(java.util.HashMap) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) DecimalFormat(java.text.DecimalFormat) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with DoubleSerializer

use of com.alibaba.fastjson.serializer.DoubleSerializer in project fastjson by alibaba.

the class DoubleTest_custom method test_0.

public void test_0() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.put(Double.class, new DoubleSerializer(new DecimalFormat("###.##")));
    Assert.assertEquals("1.12", JSON.toJSONString(1.123456789D, config));
}
Also used : DoubleSerializer(com.alibaba.fastjson.serializer.DoubleSerializer) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) DecimalFormat(java.text.DecimalFormat)

Aggregations

DoubleSerializer (com.alibaba.fastjson.serializer.DoubleSerializer)3 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)3 DecimalFormat (java.text.DecimalFormat)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1