Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
head_group
xyqb-user2
Commits
4007ccc8
Commit
4007ccc8
authored
Jul 12, 2018
by
技术部-任文超
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
试试新版镜像机器人
parent
362f95a7
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
79 additions
and
0 deletions
+79
-0
JsonTest.java
src/test/java/common/JsonTest.java
+79
-0
No files found.
src/test/java/common/JsonTest.java
0 → 100644
View file @
4007ccc8
package
common
;
import
java.sql.Timestamp
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.serializer.SerializerFeature
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.SerializationFeature
;
import
com.google.gson.Gson
;
import
lombok.extern.slf4j.Slf4j
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.JUnit4
;
import
cn.quantgroup.xyqb.entity.UserDetail
;
import
cn.quantgroup.xyqb.model.IdType
;
@Slf4j
@RunWith
(
JUnit4
.
class
)
public
class
JsonTest
{
private
static
final
Gson
GSON
=
new
Gson
();
private
static
final
ObjectMapper
MAPPER
=
new
ObjectMapper
()
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
)
.
configure
(
SerializationFeature
.
WRITE_ENUMS_USING_INDEX
,
true
)
.
configure
(
DeserializationFeature
.
FAIL_ON_NUMBERS_FOR_ENUMS
,
true
);
private
static
final
String
body
=
"{\"id\":795,\"userId\":878,\"phoneNo\":\"18810882232\",\"name\":\"马怔\",\"idNo\":\"534255196412036829\",\"idType\":\"ID_CARD\",\"isAuthenticated\":false,\"gender\":\"FEMALE\",\"email\":\"83929382938@qq.com\",\"qq\":\"\",\"createdAt\":1529463464000}"
;
@Test
public
void
intFloat
(){
try
{
UserDetail
userData
=
new
UserDetail
();
userData
.
setId
(
131L
);
userData
.
setUserId
(
119L
);
userData
.
setIdNo
(
"wanglaoji"
);
userData
.
setIdType
(
IdType
.
ID_CARD
);
userData
.
setCreatedAt
(
new
Timestamp
(
1
));
log
.
info
(
"序列化--GSON--成功!data:{}"
,
GSON
.
toJson
(
userData
));
log
.
info
(
"序列化--MAPPER--成功!data:{}"
,
MAPPER
.
writeValueAsString
(
userData
));
log
.
info
(
"序列化--JSON--成功!data:{}"
,
JSON
.
toJSONString
(
userData
,
SerializerFeature
.
config
(
JSON
.
DEFAULT_GENERATE_FEATURE
,
SerializerFeature
.
WriteEnumUsingName
,
false
)));
log
.
info
(
"序列化--JSONObject--成功!data:{}"
,
JSONObject
.
toJSONString
(
userData
));
}
catch
(
Exception
e
){
log
.
error
(
"序列化--GSON--失败!"
,
e
);
}
}
@Test
public
void
gsonGson
(){
try
{
UserDetail
userData
=
GSON
.
fromJson
(
body
,
UserDetail
.
class
);
log
.
info
(
"GSON--成功!data:{}"
,
userData
);
}
catch
(
Exception
e
){
log
.
error
(
"GSON--失败!"
,
e
);
}
}
@Test
public
void
jacksonObjectMapper
(){
try
{
UserDetail
userData
=
MAPPER
.
readValue
(
body
,
UserDetail
.
class
);
log
.
info
(
"MAPPER--成功!data:{}"
,
userData
);
}
catch
(
Exception
e
){
log
.
error
(
"MAPPER--失败!"
,
e
);
}
}
@Test
public
void
fastjsonJSONObject
(){
try
{
UserDetail
userData
=
JSONObject
.
parseObject
(
body
,
UserDetail
.
class
);
log
.
info
(
"JSONObject--成功!data:{}"
,
userData
);
userData
=
JSON
.
parseObject
(
body
,
UserDetail
.
class
);
log
.
info
(
"JSONObject--成功!data:{}"
,
userData
);
}
catch
(
Exception
e
){
log
.
error
(
"JSONObject--失败!"
,
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment