CacheObject.java
1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.yoho.search.cache.model;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.Serializable;
/**
* 暂时只支持JSONObject和jsonArray
*
* @author hugufei
*
*/
public class CacheObject implements Serializable {
private static final long serialVersionUID = -3949382156604252137L;
private String type;
private Object value;
public CacheObject() {
}
public CacheObject(Object object) {
super();
this.type = "Object";
this.value = object;
}
public CacheObject(JSONObject jsonObject) {
super();
this.type = "JSONObject";
this.value = JSON.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public JSONObject toJSONObject() {
try {
if (value == null) {
return null;
}
if ("JSONObject".equals(type)) {
return JSON.parseObject(value.toString());
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Object toObject() {
try {
if (value == null) {
return null;
}
if ("Object".equals(type)) {
return value;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}