中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

FastJSON個性化序列化

發布時間:2020-07-21 11:32:07 來源:網絡 閱讀:8567 作者:leizhimin 欄目:軟件技術

FastJSON個性化序列化



public class JstJobGoods implements Serializable {
    @JSONField(serialize = false)
    private Long id;          //ID
    @JSONField(name = "shop_id")
    private String shopId;          //店鋪編號
    @JSONField(name = "i_id")
    private Long iId;          //商品款號,商品ID
    @JSONField(name = "shop_i_id")
    private String shopIId;          //外部款號
    @JSONField(name = "name")
    private String name;          //名稱
    @JSONField(name = "sale_price")
    private Long salePrice;          //銷售價格
    @JSONField(name = "enabled")
    private Integer enabled;          //是否啟用
    @JSONField(name = "brand_name")
    private String brandName;          //品牌名稱
    @JSONField(name = "market_price")
    private Long marketPrice;          //市場價格
    @JSONField(serialize = false)
    private int realSkuQt = 0;          //真實SKU數量
    @JSONField(serialize = false)
    private String digit;          //數據MD5(不含id和uptime計算所得)
    @JSONField(serialize = false)
    private java.util.Date uptime;          //更新時間

    @JSONField(name = "skus")
    private List<JstJobGoodsSku> goodsSkuList;

    @JSONField(serialize = false)
    private Opt opt = Opt.DO_NOTHING;

    private static Pattern pnv = Pattern.compile("(.+):(.+)");
    public static SerializeFilter[] serializeFilters = new SerializeFilter[]{
            new ValueFilter() {
                @Override
                public Object process(Object object, String name, Object value) {
                    if (object instanceof JstJobGoods) {
                        JstJobGoods goods = (JstJobGoods) object;
                        if (name.equals("sale_price"))
                            return numberFormat2((long) value / 100);
                        if (name.equals("market_price"))
                            return numberFormat2((long) value / 100);
                    } else if (object instanceof JstJobGoodsSku) {
                        JstJobGoodsSku sku = (JstJobGoodsSku) object;
                        if (name.equals("sale_price"))
                            return numberFormat2((long) value / 100);
                        if (name.equals("market_price"))
                            return numberFormat2((long) value / 100);
                    }
                    return value;
                }
            },
            new BeforeFilter() {
                @Override
                public void writeBefore(Object object) {
                    //尺碼:22,顏色:黑色
                    if (object instanceof JstJobGoods) {
                        JstJobGoods goods = (JstJobGoods) object;
                        goods.setDigit(goods.getDigit());
                    } else if (object instanceof JstJobGoodsSku) {
                        JstJobGoodsSku sku = (JstJobGoodsSku) object;
                        sku.setDigit(sku.getDigit());
                        String properties = sku.getProperties();
                        if (properties == null || properties.trim().length() == 0) return;
                        String[] nvs = properties.split(",");
                        StringBuffer propertiesNames = new StringBuffer();
                        StringBuffer propertiesValues = new StringBuffer();
                        for (String nv : nvs) {
                            Matcher m = pnv.matcher(nv);
                            if (m.find()) {
                                propertiesNames.append(m.group(1).trim()).append(',');
                                propertiesValues.append(m.group(2).trim()).append(',');
                            }
                        }
                        wipeEndSeparator(propertiesNames, ',');
                        wipeEndSeparator(propertiesValues, ',');
                        sku.setPropertiesName(propertiesNames.toString());
                        sku.setPropertiesValue(propertiesValues.toString());
                    }
                }

                private void wipeEndSeparator(StringBuffer sb, char separator) {
                    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == separator)
                        sb.deleteCharAt(sb.length() - 1);
                }
            }
    };
    
    
    //getter and setter...
public static Double numberFormat2(double value) {
    DecimalFormat df = new DecimalFormat("0.00");
    df.setRoundingMode(RoundingMode.DOWN);
    return Double.parseDouble(df.format(value));
}


String req = JSON.toJSONString(needSynGoodsList, JstJobGoods.serializeFilters, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
[
	{
		"brand_name":"",
		"enabled":1,
		"i_id":1174,
		"market_price":188.0,
		"name":"皮豆豆6620真皮戰斗短靴大棉",
		"sale_price":158.0,
		"shop_i_id":"1174_001",
		"shop_id":"10039844",
		"skus":[
			{
				"brand_name":"",
				"color":"",
				"enabled":1,
				"market_price":188.0,
				"name":"皮豆豆6620真皮戰斗短靴大棉",
				"pic":"",
				"pic_big":"",
				"properties_name":"尺碼,顏色",
				"properties_value":"21,紅色",
				"sale_price":158.0,
				"shop_sku_id":"v_1572_001",
				"sku_id":"r_1572"
			},
			{
				"brand_name":"",
				"color":"",
				"enabled":1,
				"market_price":188.0,
				"name":"皮豆豆6620真皮戰斗短靴大棉",
				"pic":"",
				"pic_big":"",
				"properties_name":"尺碼,顏色",
				"properties_value":"21,黑色",
				"sale_price":158.0,
				"shop_sku_id":"v_1573_001",
				"sku_id":"r_1573"
			}
		]
	}
]


{"code":0,"issuccess":true,"msg":null}


再給個例子:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.AfterFilter;
import com.alibaba.fastjson.serializer.BeforeFilter;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.ValueFilter;

import java.util.ArrayList;
import java.util.List;


public class FastJSONTest {
    public static void main(String[] args) {
        Foo f0 = new Foo("asan", 1100,24);
        System.out.println("原始的Foo:\n"+f0);
        String json1 = JSON.toJSONString(f0, new ValueFilter() {
            @Override
            public Object process(Object object, String name, Object value) {
                switch (name){
                    case "_val": return (Integer) value / 10;
                    case "age": return (Integer) value +1;
                    default: return value;
                }
            }
        });
        System.out.println("第1次序列化后的Foo:\n"+f0);
        System.out.println(">>>JSON1"+json1);


        List  list = new ArrayList();
        list.add(f0);
        String json2 = JSON.toJSONString(f0,new SerializeFilter[]{
                new BeforeFilter() {
                    @Override
                    public void writeBefore(Object object) {
                        if(!(object instanceof Foo))return;
                        Foo f = (Foo)object;
                        f.setName(f.getName()+"++");
                        f.setAge(f.getAge()+100);
                    }
                },
                new AfterFilter() {
                    @Override
                    public void writeAfter(Object object) {
//                        if(!(object instanceof Foo))return;
//                        Foo f = (Foo)object;
//                        f.setName(f.getName().substring(0,f.getName().length()-2));
//                        f.setAge(f.getAge()-100);
                    }
                }
        });
        System.out.println("第2次序列化后的Foo:\n"+f0);

        System.out.println(">>>JSON2"+json2);
    }

}

class Foo {
    private String name;
    @JSONField(name = "_val",serialize = true)
    private int val;
    private int age;

    public Foo(String name, int val, int age) {
        this.name = name;
        this.val = val;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Foo{" +
                "name='" + name + '\'' +
                ", val=" + val +
                ", age=" + age +
                '}';
    }
}
原始的Foo:
Foo{name='asan', val=1100, age=24}
第1次序列化后的Foo:
Foo{name='asan', val=1100, age=24}
>>>JSON1{"_val":110,"age":25,"name":"asan"}
第2次序列化后的Foo:
Foo{name='asan++', val=1100, age=124}
>>>JSON2{"_val":1100,"age":124,"name":"asan++"}

Process finished with exit code 0


https://www.w3cschool.cn/fastjson/fastjson-api.html


fastjson SerializerFeature詳解

http://blog.csdn.net/u010246789/article/details/52539576


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

五原县| 池州市| 昌邑市| 湟中县| 上饶市| 井研县| 来宾市| 剑川县| 合阳县| 大安市| 观塘区| 龙口市| 桐乡市| 浦城县| 凤台县| 建瓯市| 泰兴市| 和平区| 定西市| 广水市| 高淳县| 赞皇县| 钦州市| 松江区| 瑞丽市| 徐州市| 游戏| 陆川县| 安平县| 宜良县| 旌德县| 高雄市| 正阳县| 佛山市| 吐鲁番市| 巨野县| 曲阳县| 常州市| 太湖县| 南溪县| 宜宾市|