FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/src/test/java/com/jsoniter/TestDemo.java at master · lyBigdata/java · GitHub
lyBigdata
/
java
Public
forked from
json-iterator/java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
src
/
test
/
java
/
com
/
jsoniter
/
TestDemo.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
154 lines (141 loc) · 6.39 KB
Breadcrumbs
java
/
src
/
test
/
java
/
com
/
jsoniter
/
TestDemo.java
Copy path
File metadata and controls
154 lines (141 loc) · 6.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
145
146
147
148
149
150
151
152
153
154
package
com
.
jsoniter
;
import
com
.
jsoniter
.
annotation
.
JsonProperty
;
import
com
.
jsoniter
.
any
.
Any
;
import
com
.
jsoniter
.
fuzzy
.
MaybeEmptyArrayDecoder
;
import
com
.
jsoniter
.
fuzzy
.
MaybeStringLongDecoder
;
import
com
.
jsoniter
.
output
.
EncodingMode
;
import
com
.
jsoniter
.
output
.
JsonStream
;
import
com
.
jsoniter
.
spi
.
Decoder
;
import
com
.
jsoniter
.
spi
.
EmptyExtension
;
import
com
.
jsoniter
.
spi
.
JsoniterSpi
;
import
com
.
jsoniter
.
spi
.
TypeLiteral
;
import
junit
.
framework
.
TestCase
;
import
java
.
io
.
IOException
;
import
java
.
lang
.
reflect
.
Type
;
import
java
.
util
.
Date
;
import
java
.
util
.
List
;
public
class
TestDemo
extends
TestCase
{
public
void
test_bind_api
()
throws
IOException
{
JsonIterator
iter
=
JsonIterator
.
parse
(
"[0,1,2,3]"
);
int
[]
val
=
iter
.
read
(
int
[].
class
);
System
.
out
.
println
(
val
[
3
]);
}
public
void
test_any_api
()
throws
IOException
{
JsonIterator
iter
=
JsonIterator
.
parse
(
"[0,1,2,3]"
);
System
.
out
.
println
(
iter
.
readAny
().
toInt
(
3
));
}
public
void
test_iterator_api
()
throws
IOException
{
JsonIterator
iter
=
JsonIterator
.
parse
(
"[0,1,2,3]"
);
int
total
=
0
;
while
(
iter
.
readArray
()) {
total
+=
iter
.
readInt
();
}
System
.
out
.
println
(
total
);
}
public
static
class
ABC
{
public
Any
a
;
}
public
void
test_abc
()
throws
IOException
{
JsonIterator
iter
=
JsonIterator
.
parse
(
"{'a': {'b': {'c': 'd'}}}"
.
replace
(
'\''
,
'"'
));
ABC
abc
=
iter
.
read
(
ABC
.
class
);
assertEquals
(
"d"
,
abc
.
a
.
get
(
"b"
,
"c"
).
object
());
}
public
void
test_iterator_api_and_bind
()
throws
IOException
{
JsonIterator
iter
=
JsonIterator
.
parse
(
"[123, {'name': 'taowen', 'tags': ['crazy', 'hacker']}]"
.
replace
(
'\''
,
'"'
));
iter
.
readArray
();
int
userId
=
iter
.
readInt
();
iter
.
readArray
();
User
user
=
iter
.
read
(
User
.
class
);
user
.
userId
=
userId
;
iter
.
readArray
();
// end of array
System
.
out
.
println
(
user
);
}
public
void
test_empty_array_as_null
()
throws
IOException
{
JsoniterSpi
.
registerExtension
(
new
EmptyExtension
() {
@
Override
public
Decoder
createDecoder
(
final
String
cacheKey
,
final
Type
type
) {
if
(
cacheKey
.
endsWith
(
".original"
)) {
// avoid infinite loop
return
null
;
}
if
(
type
!=
Date
.
class
) {
return
null
;
}
return
new
Decoder
() {
@
Override
public
Object
decode
(
JsonIterator
iter1
)
throws
IOException
{
if
(
iter1
.
whatIsNext
() ==
ValueType
.
ARRAY
) {
if
(
iter1
.
readArray
()) {
// none empty array
throw
iter1
.
reportError
(
"decode [] as null"
,
"only empty array is expected"
);
}
else
{
return
null
;
}
}
else
{
// just use original decoder
TypeLiteral
typeLiteral
=
new
TypeLiteral
(
type
,
cacheKey
+
".original"
,
TypeLiteral
.
create
(
type
).
getDecoderCacheKey
());
return
iter1
.
read
(
typeLiteral
);
}
}
};
}
});
JsonIterator
iter
=
JsonIterator
.
parse
(
"[]"
);
assertNull
(
iter
.
read
(
Date
.
class
));
}
public
static
class
Order
{
@
JsonProperty
(
decoder
=
MaybeStringLongDecoder
.
class
)
public
long
order_id
;
@
JsonProperty
(
decoder
=
MaybeEmptyArrayDecoder
.
class
)
public
OrderDetails
order_details
;
}
public
static
class
OrderDetails
{
public
String
pay_type
;
}
public
void
test_iterator
()
throws
IOException
{
JsonIterator
iter
=
JsonIterator
.
parse
(
"{'numbers': ['1', '2', ['3', '4']]}"
.
replace
(
'\''
,
'"'
));
assertEquals
(
"numbers"
,
iter
.
readObject
());
assertTrue
(
iter
.
readArray
());
assertEquals
(
"1"
,
iter
.
readString
());
assertTrue
(
iter
.
readArray
());
assertEquals
(
"2"
,
iter
.
readString
());
assertTrue
(
iter
.
readArray
());
assertEquals
(
ValueType
.
ARRAY
,
iter
.
whatIsNext
());
assertTrue
(
iter
.
readArray
());
// start inner array
assertEquals
(
ValueType
.
STRING
,
iter
.
whatIsNext
());
assertEquals
(
"3"
,
iter
.
readString
());
assertTrue
(
iter
.
readArray
());
assertEquals
(
"4"
,
iter
.
readString
());
assertFalse
(
iter
.
readArray
());
// end inner array
assertFalse
(
iter
.
readArray
());
// end outer array
assertNull
(
iter
.
readObject
());
// end object
}
public
void
test_any_is_fun
()
throws
IOException
{
Any
any
=
JsonIterator
.
deserialize
(
"{'numbers': ['1', '2', ['3', '4']]}"
.
replace
(
'\''
,
'"'
));
any
.
get
(
"numbers"
).
asList
().
add
(
Any
.
wrap
(
"hello"
));
assertEquals
(
"{'numbers':['1', '2', ['3', '4'],'hello']}"
.
replace
(
'\''
,
'"'
),
JsonStream
.
serialize
(
any
));
any
=
JsonIterator
.
deserialize
(
"{'error': 'failed'}"
.
replace
(
'\''
,
'"'
));
assertFalse
(
any
.
toBoolean
(
"success"
));
any
=
JsonIterator
.
deserialize
(
"{'success': true}"
.
replace
(
'\''
,
'"'
));
assertTrue
(
any
.
toBoolean
(
"success"
));
any
=
JsonIterator
.
deserialize
(
"{'success': 'false'}"
.
replace
(
'\''
,
'"'
));
assertFalse
(
any
.
toBoolean
(
"success"
));
any
=
JsonIterator
.
deserialize
(
"[{'score':100}, {'score':102}]"
.
replace
(
'\''
,
'"'
));
assertEquals
(
"[100,102]"
,
JsonStream
.
serialize
(
any
.
get
(
'*'
,
"score"
)));
any
=
JsonIterator
.
deserialize
(
"[{'score':100}, {'score':[102]}]"
.
replace
(
'\''
,
'"'
));
assertEquals
(
"[{},{'score':102}]"
.
replace
(
'\''
,
'"'
),
JsonStream
.
serialize
(
any
.
get
(
'*'
,
'*'
,
0
)));
any
=
JsonIterator
.
deserialize
(
"[{'score':100}, {'score':102}]"
.
replace
(
'\''
,
'"'
));
assertEquals
(
Long
.
class
,
any
.
get
(
0
,
"score"
).
object
().
getClass
());
any
=
JsonIterator
.
deserialize
(
"[{'score':100}, {'score':102}]"
.
replace
(
'\''
,
'"'
));
assertEquals
(
ValueType
.
INVALID
,
any
.
get
(
0
,
"score"
,
"number"
).
valueType
());
any
=
JsonIterator
.
deserialize
(
"[{'score':100}, {'score':102}]"
.
replace
(
'\''
,
'"'
));
for
(
Any
record
:
any
) {
Any
.
EntryIterator
entries
=
record
.
entries
();
while
(
entries
.
next
()) {
System
.
out
.
println
(
entries
.
key
());
System
.
out
.
println
(
entries
.
value
());
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL