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
843e6d7c
Commit
843e6d7c
authored
Dec 01, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add 2 more JsonParser implementations
parent
998c29c4
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
235 additions
and
3 deletions
+235
-3
pom.xml
spring-boot-dependencies/pom.xml
+11
-2
pom.xml
spring-boot/pom.xml
+5
-0
JsonJsonParser.java
...in/java/org/springframework/boot/json/JsonJsonParser.java
+65
-0
JsonParserFactory.java
...java/org/springframework/boot/json/JsonParserFactory.java
+9
-1
SimpleJsonJsonParser.java
...a/org/springframework/boot/json/SimpleJsonJsonParser.java
+85
-0
JsonJsonParserTests.java
...va/org/springframework/boot/json/JsonJsonParserTests.java
+30
-0
SimpleJsonJsonParserTests.java
.../springframework/boot/json/SimpleJsonJsonParserTests.java
+30
-0
No files found.
spring-boot-dependencies/pom.xml
View file @
843e6d7c
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-dependencies
</artifactId>
...
...
@@ -102,6 +104,7 @@
<reactor.version>
1.1.5.RELEASE
</reactor.version>
<reactor-spring.version>
1.1.3.RELEASE
</reactor-spring.version>
<servlet-api.version>
3.1.0
</servlet-api.version>
<simple-json.version>
1.1.1
</simple-json.version>
<slf4j.version>
1.7.7
</slf4j.version>
<snakeyaml.version>
1.14
</snakeyaml.version>
<solr.version>
4.7.2
</solr.version>
...
...
@@ -464,6 +467,12 @@
<artifactId>
gson
</artifactId>
<version>
${gson.version}
</version>
</dependency>
<dependency>
<groupId>
com.googlecode.json-simple
</groupId>
<artifactId>
json-simple
</artifactId>
<version>
${simple-json.version}
</version>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
...
...
@@ -1528,7 +1537,7 @@
<activeByDefault>
true
</activeByDefault>
</activation>
<repositories>
<!-- Repositories to allow snapshot and milestone BOM imports during
<!-- Repositories to allow snapshot and milestone BOM imports during
development. This section is stripped out when a full release is prepared. -->
<repository>
<id>
spring-milestones
</id>
...
...
spring-boot/pom.xml
View file @
843e6d7c
...
...
@@ -59,6 +59,11 @@
<artifactId>
gson
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
com.googlecode.json-simple
</groupId>
<artifactId>
json-simple
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
javax.jms
</groupId>
<artifactId>
jms-api
</artifactId>
...
...
spring-boot/src/main/java/org/springframework/boot/json/JsonJsonParser.java
0 → 100644
View file @
843e6d7c
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
json
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.json.simple.JSONObject
;
import
org.json.simple.parser.JSONParser
;
import
org.json.simple.parser.ParseException
;
/**
* Thin wrapper to adapt {@link JSONObject} to a {@link JsonParser}.
*
* @author Dave Syer
* @since 1.2.0
* @see JsonParserFactory
*/
public
class
JsonJsonParser
implements
JsonParser
{
@Override
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
Map
<
String
,
Object
>
map
=
new
LinkedHashMap
<
String
,
Object
>();
try
{
@SuppressWarnings
(
"unchecked"
)
Map
<
String
,
Object
>
value
=
(
Map
<
String
,
Object
>)
new
JSONParser
()
.
parse
(
json
);
map
.
putAll
(
value
);
}
catch
(
ParseException
e
)
{
throw
new
IllegalArgumentException
(
"Cannot parse Json"
,
e
);
}
return
map
;
}
@Override
public
List
<
Object
>
parseList
(
String
json
)
{
List
<
Object
>
nested
=
new
ArrayList
<
Object
>();
try
{
@SuppressWarnings
(
"unchecked"
)
List
<
Object
>
value
=
(
List
<
Object
>)
new
JSONParser
().
parse
(
json
);
nested
.
addAll
(
value
);
}
catch
(
ParseException
e
)
{
throw
new
IllegalArgumentException
(
"Cannot parse Json"
,
e
);
}
return
nested
;
}
}
spring-boot/src/main/java/org/springframework/boot/json/JsonParserFactory.java
View file @
843e6d7c
...
...
@@ -30,13 +30,21 @@ public abstract class JsonParserFactory {
/**
* Static factory for the "best" JSON parser available on the classpath. Tries Jackson
* 2, then Snake YAML, and then falls back to the {@link SimpleJsonParser}.
* 2, then JSON (from eclipse), Simple JSON, Gson, Snake YAML, and then falls back to
* the {@link SimpleJsonParser}.
*
* @return a {@link JsonParser}
*/
public
static
JsonParser
getJsonParser
()
{
if
(
ClassUtils
.
isPresent
(
"com.fasterxml.jackson.databind.ObjectMapper"
,
null
))
{
return
new
JacksonJsonParser
();
}
if
(
ClassUtils
.
isPresent
(
"org.json.JSONObject"
,
null
))
{
return
new
JsonJsonParser
();
}
if
(
ClassUtils
.
isPresent
(
"org.json.simple.JSONObject"
,
null
))
{
return
new
SimpleJsonJsonParser
();
}
if
(
ClassUtils
.
isPresent
(
"com.google.gson.Gson"
,
null
))
{
return
new
GsonJsonParser
();
}
...
...
spring-boot/src/main/java/org/springframework/boot/json/SimpleJsonJsonParser.java
0 → 100644
View file @
843e6d7c
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
json
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
/**
* Thin wrapper to adapt {@link JSONObject} to a {@link JsonParser}.
*
* @author Dave Syer
* @since 1.2.0
* @see JsonParserFactory
*/
public
class
SimpleJsonJsonParser
implements
JsonParser
{
@Override
public
Map
<
String
,
Object
>
parseMap
(
String
json
)
{
Map
<
String
,
Object
>
map
=
new
LinkedHashMap
<
String
,
Object
>();
putAll
(
map
,
new
JSONObject
(
json
));
return
map
;
}
private
void
putAll
(
Map
<
String
,
Object
>
map
,
JSONObject
object
)
{
for
(
Object
key
:
object
.
keySet
())
{
String
name
=
key
.
toString
();
Object
value
=
object
.
get
(
name
);
if
(
value
instanceof
JSONObject
)
{
Map
<
String
,
Object
>
nested
=
new
LinkedHashMap
<
String
,
Object
>();
putAll
(
nested
,
(
JSONObject
)
value
);
value
=
nested
;
}
if
(
value
instanceof
JSONArray
)
{
List
<
Object
>
nested
=
new
ArrayList
<
Object
>();
addAll
(
nested
,
(
JSONArray
)
value
);
value
=
nested
;
}
map
.
put
(
name
,
value
);
}
}
private
void
addAll
(
List
<
Object
>
list
,
JSONArray
array
)
{
for
(
int
i
=
0
;
i
<
array
.
length
();
i
++)
{
Object
value
=
array
.
get
(
i
);
if
(
value
instanceof
JSONObject
)
{
Map
<
String
,
Object
>
nested
=
new
LinkedHashMap
<
String
,
Object
>();
putAll
(
nested
,
(
JSONObject
)
value
);
value
=
nested
;
}
if
(
value
instanceof
JSONArray
)
{
List
<
Object
>
nested
=
new
ArrayList
<
Object
>();
addAll
(
nested
,
(
JSONArray
)
value
);
value
=
nested
;
}
list
.
add
(
value
);
}
}
@Override
public
List
<
Object
>
parseList
(
String
json
)
{
List
<
Object
>
nested
=
new
ArrayList
<
Object
>();
addAll
(
nested
,
new
JSONArray
(
json
));
return
nested
;
}
}
spring-boot/src/test/java/org/springframework/boot/json/JsonJsonParserTests.java
0 → 100644
View file @
843e6d7c
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
json
;
/**
* Tests for {@link JsonJsonParser}.
*
* @author Dave Syer
*/
public
class
JsonJsonParserTests
extends
SimpleJsonParserTests
{
@Override
protected
JsonParser
getParser
()
{
return
new
JsonJsonParser
();
}
}
spring-boot/src/test/java/org/springframework/boot/json/SimpleJsonJsonParserTests.java
0 → 100644
View file @
843e6d7c
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
json
;
/**
* Tests for {@link SimpleJsonJsonParser}.
*
* @author Dave Syer
*/
public
class
SimpleJsonJsonParserTests
extends
SimpleJsonParserTests
{
@Override
protected
JsonParser
getParser
()
{
return
new
SimpleJsonJsonParser
();
}
}
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