FastJSON个性化序列化

前端之家收集整理的这篇文章主要介绍了FastJSON个性化序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

FastJSON个性化序列化



publicclassJstJobGoodsimplementsSerializable{
@JSONField(serialize=false)
privateLongid;//ID
@JSONField(name="shop_id")
privateStringshopId;//店铺编号
@JSONField(name="i_id")
privateLongiId;//商品款号,商品ID
@JSONField(name="shop_i_id")
privateStringshopIId;//外部款号
@JSONField(name="name")
privateStringname;//名称
@JSONField(name="sale_price")
privateLongsalePrice;//销售价格
@JSONField(name="enabled")
privateIntegerenabled;//是否启用
@JSONField(name="brand_name")
privateStringbrandName;//品牌名称
@JSONField(name="market_price")
privateLongmarketPrice;//市场价格
@JSONField(serialize=false)
privateintrealSkuQt=0;//真实SKU数量
@JSONField(serialize=false)
privateStringdigit;//数据MD5(不含id和uptime计算所得)
@JSONField(serialize=false)
privatejava.util.Dateuptime;//更新时间

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

@JSONField(serialize=false)
privateOptopt=Opt.DO_NOTHING;

privatestaticPatternpnv=Pattern.compile("(.+):(.+)");
publicstaticSerializeFilter[]serializeFilters=newSerializeFilter[]{
newValueFilter(){
@Override
publicObjectprocess(Objectobject,Stringname,Objectvalue){
if(objectinstanceofJstJobGoods){
JstJobGoodsgoods=(JstJobGoods)object;
if(name.equals("sale_price"))
returnnumberFormat2((long)value/100);
if(name.equals("market_price"))
returnnumberFormat2((long)value/100);
}elseif(objectinstanceofJstJobGoodsSku){
JstJobGoodsSkusku=(JstJobGoodsSku)object;
if(name.equals("sale_price"))
returnnumberFormat2((long)value/100);
if(name.equals("market_price"))
returnnumberFormat2((long)value/100);
}
returnvalue;
}
},newBeforeFilter(){
@Override
publicvoidwriteBefore(Objectobject){
//尺码:22,颜色:黑色
if(objectinstanceofJstJobGoods){
JstJobGoodsgoods=(JstJobGoods)object;
goods.setDigit(goods.getDigit());
}elseif(objectinstanceofJstJobGoodsSku){
JstJobGoodsSkusku=(JstJobGoodsSku)object;
sku.setDigit(sku.getDigit());
Stringproperties=sku.getProperties();
if(properties==null||properties.trim().length()==0)return;
String[]nvs=properties.split(",");
StringBufferpropertiesNames=newStringBuffer();
StringBufferpropertiesValues=newStringBuffer();
for(Stringnv:nvs){
Matcherm=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());
}
}

privatevoidwipeEndSeparator(StringBuffersb,charseparator){
if(sb.length()>0&&sb.charAt(sb.length()-1)==separator)
sb.deleteCharAt(sb.length()-1);
}
}
};


//getterandsetter...
publicstaticDoublenumberFormat2(doublevalue){
DecimalFormatdf=newDecimalFormat("0.00");
df.setRoundingMode(RoundingMode.DOWN);
returnDouble.parseDouble(df.format(value));
}


Stringreq=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":"","pic":"","pic_big":"","properties_name":"尺码,颜色","properties_value":"21,红色","shop_sku_id":"v_1572_001","sku_id":"r_1572"
			},{
				"brand_name":"",黑色","shop_sku_id":"v_1573_001","sku_id":"r_1573"
			}
		]
	}
]


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


再给个例子:

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

importjava.util.ArrayList;
importjava.util.List;


publicclassFastJSONTest{
publicstaticvoidmain(String[]args){
Foof0=newFoo("asan",1100,24);
System.out.println("原始的Foo:\n"+f0);
Stringjson1=JSON.toJSONString(f0,newValueFilter(){
@Override
publicObjectprocess(Objectobject,Objectvalue){
switch(name){
case"_val":return(Integer)value/10;
case"age":return(Integer)value+1;
default:returnvalue;
}
}
});
System.out.println("第1次序列化后的Foo:\n"+f0);
System.out.println(">>>JSON1"+json1);


Listlist=newArrayList();
list.add(f0);
Stringjson2=JSON.toJSONString(f0,newSerializeFilter[]{
newBeforeFilter(){
@Override
publicvoidwriteBefore(Objectobject){
if(!(objectinstanceofFoo))return;
Foof=(Foo)object;
f.setName(f.getName()+"++");
f.setAge(f.getAge()+100);
}
},newAfterFilter(){
@Override
publicvoidwriteAfter(Objectobject){
//if(!(objectinstanceofFoo))return;
//Foof=(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);
}

}

classFoo{
privateStringname;
@JSONField(name="_val",serialize=true)
privateintval;
privateintage;

publicFoo(Stringname,intval,intage){
this.name=name;
this.val=val;
this.age=age;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicintgetVal(){
returnval;
}

publicvoidsetVal(intval){
this.val=val;
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

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

Processfinishedwithexitcode0


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


fastjson SerializerFeature详解

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

原文链接:https://www.f2er.com/json/288657.html

猜你在找的Json相关文章