本篇博客主要记录如何将一个类转化为json时,按指定需要转化一些属性。
例如:有如下一个类
public static class EMojElement {
private float fontSize;
private int fontAlign;
private int type;
private boolean borderArrow;
private int borderStyle;
private int font;
private String text;
public String imageurl;
public Frame frame;
public Transform_flip transform_flip;
public Transform transform;
public BorderColor borderColor;
public FontColor fontColor;
}
然后服务器返回如下一段json
{
"id":"emoji_1AC97CE2-A264-4848-9A81-8B609327A1F6",
"emojiElements":[
{
"type":2001,
"frame":{
"y":99.66742,
"width":106.8228,
"x":99.11677,
"height":119.8278
},
"transform":{
"d":0.9999757,
"b":0.006961379,
"ty":0,
"c":-0.006961379,
"a":0.9999757,
"tx":0
},
"transform_flip":{
"d":1,
"b":0,
"ty":0,
"c":0,
"a":1,
"tx":0
}
},
{
"fontSize":24.74611,
"frame":{
"y":44.26067,
"width":138.3485,
"x":87.0689,
"height":55.5
},
"fontAlign":1,
"fontColor":{
"r":0.3411765,
"b":0.1764706,
"g":0,
"a":1
},
"type":1001,
"borderArrow":true,
"font":1,
"text":"静静的看着你XX",
"borderColor":{
"r":1,
"b":1,
"g":1,
"a":1
},
"transform":{
"d":0.9998971,
"b":0.01434911,
"ty":0,
"c":-0.01434911,
"a":0.9998971,
"tx":0
},
"borderStyle":1,
"transform_flip":{
"d":1,
"b":0,
"ty":0,
"c":0,
"a":1,
"tx":0
}
}
]
}
你会发现同一个同一个json中一个object只有四个属性,另一个object一个有十几个属性,解析这样一段json并不困难,设置一个
@JsonIgnoreProperties(ignoreUnknown = true)
就可以轻松解决。
但是如果需求是:你要上传一个类似的json到服务器,就有点麻烦了。
上面的类中有个type属性,假如type =1001时代表一个textview,需要所有的属性值,type = 2001时代表一个imageview,只需要四个属性值。
如果是用如下的方式,必定会有多余的参数:
EMojElement emElement = new EMojElement();
// frame
Frame frame = new Frame();
frame.setX(rectF.left / density);
frame.setY(rectF.top / density);
frame.setWidth((rectF.right - rectF.left) / density);
frame.setHeight((rectF.bottom - rectF.top) / density);
emElement.setFrame(frame);
一般如果按照这个方式来设置属性值,即使你只为imageview设置了四个属性,使用gson.toJson方法同样会将有值的参数添加到json中(基本数据类型会默认赋值0,包装类设置为null就不会解析到json中,但是这样做不合适,EMojElement 是以其他类作为一个属性,赋值null也不会解析到json中)。
解决方案:使用Map(这只是我目前使用的解决方案)
List<Map<String, Object>> bitMaps = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
// frame
Frame frame = new Frame();
frame.setX(rectF.left / density);
frame.setY(rectF.top / density);
frame.setWidth((rectF.right - rectF.left) / density);
frame.setHeight((rectF.bottom - rectF.top) / density);
map.put("frame", frame);
//你所需要的其他的属性,可以逐一在这里添加,然后将整个map作为一个对象,放入bitmaps集合中
bitMaps.add(map);
使用map对象来代替之前的emElement 对象,可以使指定参数转化成json很好的实现。
public static void getImageJson(List<ElementsItem.EMojElement> emList,
List<Map<String, Object>> bitMaps,
ImageViewDrawableOverlay mImageView, List<String> urList,
Context context) {
// 为保持一致,获取的大小都除以屏幕密度,用1为标准
float density = ZTDeviceUtil.getDensity(context);
for (int i = 0; i < mImageView.getmOverlayViews().size(); i++) {
ElementsItem.EMojElement emElement = new ElementsItem.EMojElement();
Map<String, Object> map = new HashMap<String, Object>();
// frame
RectF rectF = mImageView.getmOverlayViews().get(i).computeLayout();
Frame frame = new Frame();
frame.setX(rectF.left / density);
frame.setY(rectF.top / density);
frame.setWidth((rectF.right - rectF.left) / density);
frame.setHeight((rectF.bottom - rectF.top) / density);
emElement.setFrame(frame);
map.put("frame", frame);
emElement.imageurl = urList.get(i);
// tarnsform -- 旋转的角度
float rotation = mImageView.getmOverlayViews().get(i).getRotation();
Transform transform = new Transform();
transform.setD((float) Math.cos(rotation));
transform.setB((float) Math.sin(rotation));
transform.setC(-(float) Math.sin(rotation));
transform.setA((float) Math.cos(rotation));
transform.setTx(0f);
transform.setTy(0f);
emElement.setTransform(transform);
map.put("transform", transform);
// transform_flip -- 对称变换,变换d\a
Transform_flip transform_flip = new Transform_flip();
int ltr = mImageView.getmOverlayViews().get(i).getLeftToRight();
int utd = mImageView.getmOverlayViews().get(i).getUpToDown();
transform_flip.setD(utd);
transform_flip.setB(0);
transform_flip.setC(0);
transform_flip.setA(ltr);
transform_flip.setTx(0);
transform_flip.setTy(0);
emElement.setTransform_flip(transform_flip);
map.put("transform_flip", transform_flip);
// 设置类型
emElement.setType(2001);
map.put("type", 2001);
// 添加到集合中
emList.add(emElement);
bitMaps.add(map);
}
}
这里使用了两种方式来获取要转化为json的对象集合,都可以满足转化为json的要求,并且map方式转化的json只有在这里设置了的,其余没有设置的,即使有默认值,也不会转化到json中。