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
62a41aee
Commit
62a41aee
authored
Jun 20, 2016
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6188 from izeye:json
* pr/6188: Reuse objects in JsonParser implementations
parents
e3837521
25f37da4
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 @
62a41aee
...
@@ -33,6 +33,11 @@ import com.google.gson.reflect.TypeToken;
...
@@ -33,6 +33,11 @@ import com.google.gson.reflect.TypeToken;
*/
*/
public
class
GsonJsonParser
implements
JsonParser
{
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
();
private
Gson
gson
=
new
GsonBuilder
().
create
();
@Override
@Override
...
@@ -40,7 +45,7 @@ public class GsonJsonParser implements JsonParser {
...
@@ -40,7 +45,7 @@ public class GsonJsonParser implements JsonParser {
if
(
json
!=
null
)
{
if
(
json
!=
null
)
{
json
=
json
.
trim
();
json
=
json
.
trim
();
if
(
json
.
startsWith
(
"{"
))
{
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"
);
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
);
...
@@ -51,16 +56,10 @@ public class GsonJsonParser implements JsonParser {
...
@@ -51,16 +56,10 @@ public class GsonJsonParser implements JsonParser {
if
(
json
!=
null
)
{
if
(
json
!=
null
)
{
json
=
json
.
trim
();
json
=
json
.
trim
();
if
(
json
.
startsWith
(
"["
))
{
if
(
json
.
startsWith
(
"["
))
{
TypeToken
<
List
<
Object
>>
type
=
new
TypeToken
<
List
<
Object
>>()
{
return
this
.
gson
.
fromJson
(
json
,
LIST_TYPE
.
getType
());
};
return
this
.
gson
.
fromJson
(
json
,
type
.
getType
());
}
}
}
}
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
);
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 @
62a41aee
...
@@ -30,10 +30,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
...
@@ -30,10 +30,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*/
*/
public
class
JacksonJsonParser
implements
JsonParser
{
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
@Override
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
try
{
try
{
return
new
ObjectMapper
().
readValue
(
json
,
new
MapTypeReference
()
);
return
this
.
objectMapper
.
readValue
(
json
,
MAP_TYPE
);
}
}
catch
(
Exception
ex
)
{
catch
(
Exception
ex
)
{
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
,
ex
);
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
,
ex
);
...
@@ -43,17 +50,11 @@ public class JacksonJsonParser implements JsonParser {
...
@@ -43,17 +50,11 @@ public class JacksonJsonParser implements JsonParser {
@Override
@Override
public
List
<
Object
>
parseList
(
String
json
)
{
public
List
<
Object
>
parseList
(
String
json
)
{
try
{
try
{
TypeReference
<
List
<
Object
>>
type
=
new
TypeReference
<
List
<
Object
>>()
{
return
this
.
objectMapper
.
readValue
(
json
,
LIST_TYPE
);
};
return
new
ObjectMapper
().
readValue
(
json
,
type
);
}
}
catch
(
Exception
ex
)
{
catch
(
Exception
ex
)
{
throw
new
IllegalArgumentException
(
"Cannot parse JSON"
,
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