Fastjson介绍
Fastjson是一个Java语言编写的高性能功能完善的JSON库。由阿里巴巴
高性能
fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson。并且还超越了google的二进制协议protocol buf。
支持标准
-
Fastjson完全支持http://json.org的标准,也是官方网站收录的参考实现之一。
功能强大
-
支持各种JDK类型。包括基本类型、JavaBean、Collection、Map、Enum、泛型等。
-
支持循环引用
无依赖
-
不需要例外额外的jar,能够直接跑在JDK上。
支持范围广
-
支持JDK 5、JDK 6、Android、阿里云手机等环境。
开源
-
Apache License 2.0
-
代码托管在github.org上,项目地址是 https://github.com/AlibabaTech/fastjson
测试充分
-
fastjson有超过1500个testcase,每次构建都会跑一遍,丰富的测试场景保证了功能稳定。
下载
http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/
性能测试:
JavaJSON技术框架选型与实例
JSONJSON英文全称为JavaScriptObject Natation,采用key:value键值对的方式存贮数据,与xml格式相比,JSON是一种轻量级的数据交换格式;不要被Javascript这个单词迷惑,实际上JSON只是一种数据格式,与具体语言并无关系。JSON已被广泛应用于业界,比如目前NoSQL数据库存贮大都采用key:value存贮结构,以Mongo为例,其脚本语法甚至直接使用Javascript;在数据传输时,采用JSON格式也被广泛应用,大部分开放API都开放JSON模式的数据输出;在ajax请求数据时,json格式也被广泛推荐。json更多信息的可以查看json官方网站http://json.org。 Java transient关键字JAVA规范原文The transient marker is not fully specified by the Java LanguageSpecification but is used in object serialization to mark member variables thatshould not be serialized.为了方便存贮和网络传输,java有系列化对象机制,transient可用来指定当前不想被系列化的成员对象。举个例子说明transient的应用,在Mongo+Morphia开源项目下,如果对Java PO的成员指定transient,那么该成员数据将不会被存入Mongo数据库。另外一种应用场景就是这里要讲到的JSON,如果JAVA PO使用了Refrence(Mongo的Refrence)或者LazyLoading(可以理解成Hibernate LazyLoading概念),那么大部分的开源JAVA JSON相关项目,会自动加载这些Refrence、LazyLoading对象,如果PO形成相互引用,那就会形成死循环,即使没有形成死循环,大量不必要的数据被输出到客户端对资源的浪费也不容小觑。加上transient是一种解决办法。 基于JAVA的JSON主要开源项目及其对比Json开源项目非常多,如org.json、 JSON-Lib、jsontool、Jackson、Gson、SimpleJSON等等,后来专门查看了几种json开源测试数据对比后,决定采用fastjson。展示两组测试数据。首先来看大侠wangym(原博客http://wangym.iteye.com/blog/738933)对Jackson、JSON-Lib、Gson的测试结果 JSON转Bean,5个线程并发,约200字节对象,1千万次转换:
Bean转JSON,5个线程并发,约200字节对象,1千万次转换:
显而易见,无论是哪种形式的转换,Jackson > Gson > Json-lib。 Jackson的处理能力甚至高出Json-lib有10倍左右
然后再拿温少的fastjson与JSON-Lib、Simple-JSON、Jackson性能测试对比数据
可以看到Fastjson在性能方面,超越目前的所有java json proccesor,包括jackson。 |
------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
详细:fastjson 项目介绍
问题解决
背景:刚接触这个开源项目不久。遇到问题也不少。不过通过各方询问、及源码探究开源得到解决。最近使用FastJson结合hibernate做项目,发现关于对象的级联属性的过滤上用的不是很顺。当然简单的属性过滤 @温少 已经提供了 SimplePropertyPreFilter 使用,使用方法有详细说明的。这里我针对级联属性的过滤对该类做了补充。(当然你也可以使用注解实现)
ok、上代码:
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package
com
.
example
.
util
.
fastjson
;
import
java
.
util
.
Date
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
import
com
.
alibaba
.
fastjson
.
JSON
;
import
com
.
alibaba
.
fastjson
.
serializer
.
JSONSerializer
;
import
com
.
alibaba
.
fastjson
.
serializer
.
PropertyPreFilter
;
import
com
.
alibaba
.
fastjson
.
serializer
.
SerializerFeature
;
import
com
.
suncompass
.
example
.
auth
.
entity
.
AuthEmployee
;
import
com
.
suncompass
.
example
.
auth
.
entity
.
AuthMenu
;
import
com
.
suncompass
.
framework
.
base
.
entity
.
BaseEntity
;
/**
* @author :晨风²º¹³ <br>
* @Comment : fastjson 针对类型的属性选择过滤器(可以跨层级) <br>
*/
public
class
ComplexPropertyPreFilter
implements
PropertyPreFilter
{
private
Map
<
Class
<?
>
,
String
[
]
>
includes
=
new
HashMap
<>
(
)
;
private
Map
<
Class
<?
>
,
String
[
]
>
excludes
=
new
HashMap
<>
(
)
;
static
{
JSON
.
DEFAULT_GENERATE_FEATURE
|=
SerializerFeature
.
DisableCircularReferenceDetect
.
getMask
(
)
;
}
public
ComplexPropertyPreFilter
(
)
{
}
public
ComplexPropertyPreFilter
(
Map
<
Class
<?
>
,
String
[
]
>
includes
)
{
super
(
)
;
this
.
includes
=
includes
;
}
public
boolean
apply
(
JSONSerializer
serializer
,
Object
source
,
String
name
)
{
//对象为空。直接放行
if
(
source
==
null
)
{
return
true
;
}
// 获取当前需要序列化的对象的类对象
Class
<?
>
clazz
=
source
.
getClass
(
)
;
// 无需序列的对象、寻找需要过滤的对象,可以提高查找层级
// 找到不需要的序列化的类型
for
(
Map
.
Entry
<
Class
<?
>
,
String
[
]
>
item
:
this
.
excludes
.
entrySet
(
)
)
{
// isAssignableFrom(),用来判断类型间是否有继承关系
if
(
item
.
getKey
(
)
.
isAssignableFrom
(
clazz
)
)
{
String
[
]
strs
=
item
.
getValue
(
)
;
// 该类型下 此 name 值无需序列化
if
(
isHave
(
strs
,
name
)
)
{
return
false
;
}
}
}
// 需要序列的对象集合为空 表示 全部需要序列化
if
(
this
.
includes
.
isEmpty
(
)
)
{
return
true
;
}
// 需要序列的对象
// 找到不需要的序列化的类型
for
(
Map
.
Entry
<
Class
<?
>
,
String
[
]
>
item
:
this
.
includes
.
entrySet
(
)
)
{
// isAssignableFrom(),用来判断类型间是否有继承关系
if
(
item
.
getKey
(
)
.
isAssignableFrom
(
clazz
)
)
{
String
[
]
strs
=
item
.
getValue
(
)
;
// 该类型下 此 name 值无需序列化
if
(
isHave
(
strs
,
name
)
)
{
return
true
;
}
}
}
return
false
;
}
/*
* 此方法有两个参数,第一个是要查找的字符串数组,第二个是要查找的字符或字符串
*/
public
static
boolean
isHave
(
String
[
]
strs
,
String
s
)
{
for
(
int
i
=
0
;
i
<
strs
.
length
;
i
++
)
{
// 循环查找字符串数组中的每个字符串中是否包含所有查找的内容
if
(
strs
[
i
]
.
equals
(
s
)
)
{
// 查找到了就返回真,不在继续查询
return
true
;
}
}
// 没找到返回false
return
false
;
}
public
Map
<
Class
<?
>
,
String
[
]
>
getIncludes
(
)
{
return
includes
;
}
public
void
setIncludes
(
Map
<
Class
<?
>
,
String
[
]
>
includes
)
{
this
.
includes
=
includes
;
}
public
Map
<
Class
<?
>
,
String
[
]
>
getExcludes
(
)
{
return
excludes
;
}
public
void
setExcludes
(
Map
<
Class
<?
>
,
String
[
]
>
excludes
)
{
this
.
excludes
=
excludes
;
}
public
static
void
main
(
String
[
]
args
)
{
// use instanceOf,用来判断对象是否是类的实例
// use isAssignableFrom(),用来判断类型间是否有继承关系
// use isInstance(),用来判断对象是否是类的实例
Class
<
?
>
intClass
=
Integer
.
class
;
// Create various objects.
String
str
=
"Hello"
;
Date
date
=
new
Date
(
)
;
Integer
i
=
new
Integer
(
10
)
;
// Is str an instance of class Integer?
boolean
check1
=
intClass
.
isInstance
(
str
)
;
System
.
out
.
println
(
"str is an Integer? "
+
check1
)
;
// Is date an instance of class Integer?
boolean
check2
=
intClass
.
isInstance
(
date
)
;
System
.
out
.
println
(
"date is an Integer? "
+
check2
)
;
// Is i an instance of class Integer?
boolean
check3
=
intClass
.
isInstance
(
i
)
;
System
.
out
.
println
(
"i is an Integer? "
+
check3
)
;
System
.
out
.
println
(
BaseEntity
.
class
.
isInstance
(
new
AuthEmployee
(
)
)
)
;
System
.
out
.
println
(
AuthEmployee
.
class
.
isInstance
(
new
AuthMenu
(
)
)
)
;
System
.
out
.
println
(
BaseEntity
.
class
.
isAssignableFrom
(
AuthEmployee
.
class
)
)
;
}
}
|
1
|
|
测试代码:
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
|
package
com
.
example
.
auth
.
test
.
fastjson
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
List
;
import
com
.
alibaba
.
fastjson
.
JSON
;
import
com
.
example
.
util
.
fastjson
.
ComplexPropertyPreFilter
;
public
class
A
{
private
Integer
aid
;
private
B
b
;
private
List
<
C
>
c
=
new
ArrayList
<>
(
)
;
public
A
(
)
{
super
(
)
;
}
public
static
void
main
(
String
[
]
args
)
{
A
a
=
new
A
(
)
;
a
.
setAid
(
1
)
;
B
b
=
new
B
(
)
;
b
.
setBid
(
2
)
;
a
.
setB
(
b
)
;
b
.
setA
(
a
)
;
C
c
=
new
C
(
)
;
c
.
setId
(
3
)
;
a
.
getC
(
)
.
add
(
c
)
;
b
.
getC
(
)
.
add
(
c
)
;
c
.
setA
(
a
)
;
c
.
setB
(
b
)
;
ComplexPropertyPreFilter
filter
=
new
ComplexPropertyPreFilter
(
)
;
filter
.
setExcludes
(
new
HashMap
<
Class
<
?
>
,
String
[
]
>
(
)
{
private
static
final
long
serialVersionUID
=
-
8411128674046835592L
;
{
put
(
A
.
class
,
new
String
[
]
{
"aid"
}
)
;
put
(
B
.
class
,
new
String
[
]
{
"bid"
,
"a"
}
)
;
put
(
C
.
class
,
new
String
[
]
{
"a"
,
"b"
}
)
;
}
}
)
;
System
.
out
.
println
(
JSON
.
toJSONString
(
a
,
filter
)
)
;
}
public
Integer
getAid
(
)
{
return
aid
;
}
public
void
setAid
(
Integer
aid
)
{
this
.
aid
=
aid
;
}
public
B
getB
(
)
{
return
b
;
}
public
void
setB
(
B
b
)
{
this
.
b
=
b
;
}
public
List
<
C
>
getC
(
)
{
return
c
;
}
public
void
setC
(
List
<
C
>
c
)
{
this
.
c
=
c
;
}
}
|
1
|
|
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
|
package
com
.
example
.
auth
.
test
.
fastjson
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
B
{
private
Integer
bid
;
private
A
a
;
private
List
<
C
>
c
=
new
ArrayList
<>
(
)
;
public
B
(
)
{
super
(
)
;
}
public
Integer
getBid
(
)
{
return
bid
;
}
public
void
setBid
(
Integer
bid
)
{
this
.
bid
=
bid
;
}
public
A
getA
(
)
{
return
a
;
}
public
void
setA
(
A
a
)
{
this
.
a
=
a
;
}
public
List
<
C
>
getC
(
)
{
return
c
;
}
public
void
setC
(
List
<
C
>
c
)
{
this
.
c
=
c
;
}
}
|
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
|
package
com
.
example
.
auth
.
test
.
fastjson
;
public
class
C
{
private
Integer
id
;
private
A
a
;
private
B
b
;
public
C
(
)
{
super
(
)
;
}
public
A
getA
(
)
{
return
a
;
}
public
void
setA
(
A
a
)
{
this
.
a
=
a
;
}
public
B
getB
(
)
{
return
b
;
}
public
void
setB
(
B
b
)
{
this
.
b
=
b
;
}
public
Integer
getId
(
)
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
}
|
1
|
ok
,代码已贴好,测试方法在
A类。
|
- 本文固定链接: http://www.aijava.cn/597.html