Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
DEMO
spring-boot
Commits
25f37da4
Commit
25f37da4
authored
Jun 20, 2016
by
Johnny Lim
Committed by
Stephane Nicoll
Jun 20, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reuse objects in JsonParser implementations
Closes gh-6188
parent
e3837521
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
16 deletions
+16
-16
GsonJsonParser.java
...in/java/org/springframework/boot/json/GsonJsonParser.java
+7
-8
JacksonJsonParser.java
...java/org/springframework/boot/json/JacksonJsonParser.java
+9
-8
No files found.
spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java
View file @
25f37da4
...
...
@@ -33,6 +33,11 @@ import com.google.gson.reflect.TypeToken;
*/
public
class
GsonJsonParser
implements
JsonParser
{
private
static
final
TypeToken
<
List
<
Object
>>
LIST_TYPE
=
new
TypeToken
<
List
<
Object
>>()
{
};
private
static
final
TypeToken
<
Map
<
String
,
Object
>>
MAP_TYPE
=
new
TypeToken
<
Map
<
String
,
Object
>>()
{
};
private
Gson
gson
=
new
GsonBuilder
().
create
();
@Override
...
...
@@ -40,7 +45,7 @@ public class GsonJsonParser implements JsonParser {
if
(
json
!=
null
)
{
json
=
json
.
trim
();
if
(
json
.
startsWith
(
"{"
))
{
return
this
.
gson
.
fromJson
(
json
,
new
MapTypeToken
()
.
getType
());
return
this
.
gson
.
fromJson
(
json
,
MAP_TYPE
.
getType
());
}
}
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
);
...
...
@@ -51,16 +56,10 @@ public class GsonJsonParser implements JsonParser {
if
(
json
!=
null
)
{
json
=
json
.
trim
();
if
(
json
.
startsWith
(
"["
))
{
TypeToken
<
List
<
Object
>>
type
=
new
TypeToken
<
List
<
Object
>>()
{
};
return
this
.
gson
.
fromJson
(
json
,
type
.
getType
());
return
this
.
gson
.
fromJson
(
json
,
LIST_TYPE
.
getType
());
}
}
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
);
}
private
static
final
class
MapTypeToken
extends
TypeToken
<
Map
<
String
,
Object
>>
{
}
}
spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java
View file @
25f37da4
...
...
@@ -30,10 +30,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*/
public
class
JacksonJsonParser
implements
JsonParser
{
private
static
final
TypeReference
<
List
<
Object
>>
LIST_TYPE
=
new
TypeReference
<
List
<
Object
>>()
{
};
private
static
final
TypeReference
<
Map
<
String
,
Object
>>
MAP_TYPE
=
new
TypeReference
<
Map
<
String
,
Object
>>()
{
};
private
final
ObjectMapper
objectMapper
=
new
ObjectMapper
();
@Override
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
try
{
return
new
ObjectMapper
().
readValue
(
json
,
new
MapTypeReference
()
);
return
this
.
objectMapper
.
readValue
(
json
,
MAP_TYPE
);
}
catch
(
Exception
ex
)
{
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
,
ex
);
...
...
@@ -43,17 +50,11 @@ public class JacksonJsonParser implements JsonParser {
@Override
public
List
<
Object
>
parseList
(
String
json
)
{
try
{
TypeReference
<
List
<
Object
>>
type
=
new
TypeReference
<
List
<
Object
>>()
{
};
return
new
ObjectMapper
().
readValue
(
json
,
type
);
return
this
.
objectMapper
.
readValue
(
json
,
LIST_TYPE
);
}
catch
(
Exception
ex
)
{
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
,
ex
);
}
}
private
static
class
MapTypeReference
extends
TypeReference
<
Map
<
String
,
Object
>>
{
};
}
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