FastJson对数字(Long等)类型转换到String类型Utils

在其他博客上看到的方法

SerializeConfig serializeConfig =new SerializeConfig();
serializeConfig.put(Number.class, ToStringSerializer.instance);
serializeConfig.put(Long.class,ToStringSerializer.instance);
serializeConfig.put(Long.TYPE,ToStringSerializer.instance);
serializeConfig.put(BigDecimal.class,ToStringSerializer.instance);
String s = JSONObject.toJSONString(jsonObject, serializeConfig, SerializerFeature.PrettyFormat);
JSON.parseObject(s);
        

我觉得这个方法太费性能,自己写了一个类,然后对比了一下,下面是两种方法的对比,可以看看,耗时应该是大幅度降低了的。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Map;

public class JsonUtils {

    public static <T> T setNumberValueToString(T json){
        if(json instanceof JSONObject){
            JSONObject newJSON = new JSONObject();
            JSONObject jsonObject =(JSONObject) json;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                Object value = entry.getValue();
                if(value instanceof Number){
                    newJSON.put(entry.getKey(),entry.getValue().toString());
                }
                else if(value instanceof JSONObject){
                    newJSON.put(entry.getKey(),setNumberValueToString(value));
                }
                else if(value instanceof JSONArray){
                    newJSON.put(entry.getKey(),setNumberValueToString(value));
                }else {
                    newJSON.put(entry.getKey(),entry.getValue());
                }
            }
            return (T) newJSON;
        }
        if(json instanceof JSONArray){
            JSONArray newJSONArray = new JSONArray();
            JSONArray valueArray =(JSONArray) json;
            for (Object o : valueArray) {
                newJSONArray.add(setNumberValueToString(o));
            }
            return (T) newJSONArray;
        }
        if(json instanceof Number){
            return (T) json.toString();
        }
        return json;
    }

    public static void main(String[] args) {

        SerializeConfig serializeConfig =new SerializeConfig();
        serializeConfig.put(Number.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class,ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE,ToStringSerializer.instance);
        serializeConfig.put(BigDecimal.class,ToStringSerializer.instance);
        serializeConfig.put(Integer.class,ToStringSerializer.instance);
        JSONObject jsonObject= JSONObject.parseObject("{\n" +
                "   \"min_position\": 7,\n" +
                "   \"has_more_items\": true,\n" +
                "   \"items_html\": \"Car\",\n" +
                "   \"new_latent_count\": 1,\n" +
                "   \"data\": {\n" +
                "      \"length\": 21,\n" +
                "      \"text\": \"xixingya\"\n" +
                "   },\n" +
                "   \"numericalArray\": [\n" +
                "      31,\n" +
                "      23,\n" +
                "      24,\n" +
                "      29,\n" +
                "      33\n" +
                "   ],\n" +
                "   \"StringArray\": [\n" +
                "      \"Nitrogen\",\n" +
                "      \"Carbon\",\n" +
                "      \"Nitrogen\",\n" +
                "      \"Nitrogen\"\n" +
                "   ],\n" +
                "   \"multipleTypesArray\": 3,\n" +
                "   \"objArray\": [\n" +
                "      {\n" +
                "         \"class\": \"middle\",\n" +
                "         \"age\": 8\n" +
                "      },\n" +
                "      {\n" +
                "         \"class\": \"upper\",\n" +
                "         \"age\": 9\n" +
                "      },\n" +
                "      {\n" +
                "         \"class\": \"lower\",\n" +
                "         \"age\": 7\n" +
                "      },\n" +
                "      {\n" +
                "         \"class\": \"middle\",\n" +
                "         \"age\": 7\n" +
                "      },\n" +
                "      {\n" +
                "         \"class\": \"middle\",\n" +
                "         \"age\": 5\n" +
                "      }\n" +
                "   ]\n" +
                "}");
        long current = System.currentTimeMillis();

        //FastJson转换
        for (int i = 0; i < 100000; i++) {
            String s = JSONObject.toJSONString(jsonObject, serializeConfig, SerializerFeature.PrettyFormat);
            JSON.parseObject(s);
        }
        System.out.println("use time ="+(System.currentTimeMillis()-current));
        current = System.currentTimeMillis();

        //Util转换
        for (int i = 0; i < 100000; i++) {
            JsonUtils.setNumberValueToString(jsonObject);
        }
        System.out.println("use time ="+(System.currentTimeMillis()-current));
        String s = JSONObject.toJSONString(jsonObject, serializeConfig,SerializerFeature.PrettyFormat);
        JSONObject jsonObject1 = JSON.parseObject(s);
        System.out.println(jsonObject1);
        System.out.println(JsonUtils.setNumberValueToString(jsonObject));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值