Skip to content

Commit ea306be

Browse files
authored
Merge pull request eugenp#3726 from brunotorrao/BAEL-1559
add: parsing json
2 parents ef1b033 + a25497f commit ea306be

5 files changed

Lines changed: 150 additions & 3 deletions

File tree

‎core-groovy/build.gradle‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
group 'com.baeldung'
2+
version '1.0-SNAPSHOT'
3+
4+
apply plugin: 'groovy'
5+
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
dependencies {
11+
compile 'org.codehaus.groovy:groovy-all:2.5.0-alpha-1'
12+
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
13+
}

‎core-groovy/pom.xml‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
<dependency>
2525
<groupId>org.codehaus.groovy</groupId>
2626
<artifactId>groovy</artifactId>
27-
<version>2.4.13</version>
27+
<version>2.5.0-alpha-1</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.codehaus.groovy</groupId>
31+
<artifactId>groovy-all</artifactId>
32+
<version>2.5.0-alpha-1</version>
2833
</dependency>
2934
<dependency>
3035
<groupId>org.codehaus.groovy</groupId>
3136
<artifactId>groovy-sql</artifactId>
32-
<version>2.4.13</version>
37+
<version>2.5.0-alpha-1</version>
3338
</dependency>
3439
<dependency>
3540
<groupId>org.junit.jupiter</groupId>
@@ -49,6 +54,12 @@
4954
<version>2.4.0</version>
5055
<scope>test</scope>
5156
</dependency>
57+
<dependency>
58+
<groupId>org.spockframework</groupId>
59+
<artifactId>spock-core</artifactId>
60+
<version>1.1-groovy-2.4</version>
61+
<scope>test</scope>
62+
</dependency>
5263
</dependencies>
5364

5465
<build>
@@ -114,5 +125,5 @@
114125
<junit.vintage.version>4.12.0</junit.vintage.version>
115126
<junit4.version>4.12</junit4.version>
116127
</properties>
117-
118128
</project>
129+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.json
2+
3+
class Account {
4+
String id
5+
BigDecimal value
6+
Date createdAt
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.json
2+
3+
import groovy.json.JsonGenerator
4+
import groovy.json.JsonOutput
5+
import groovy.json.JsonParserType
6+
import groovy.json.JsonSlurper
7+
8+
class JsonParser {
9+
10+
Account toObject(String json) {
11+
JsonSlurper jsonSlurper = new JsonSlurper()
12+
jsonSlurper.parseText(json) as Account
13+
}
14+
15+
Account toObjectWithIndexOverlay(String json) {
16+
JsonSlurper jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)
17+
jsonSlurper.parseText(json) as Account
18+
}
19+
20+
String toJson(Account account) {
21+
JsonOutput.toJson(account)
22+
}
23+
24+
String toJson(Account account, String dateFormat, String... fieldsToExclude) {
25+
JsonGenerator generator = new JsonGenerator.Options()
26+
.dateFormat(dateFormat)
27+
.excludeFieldsByName(fieldsToExclude)
28+
.build()
29+
generator.toJson(account)
30+
}
31+
32+
String prettyfy(String json) {
33+
JsonOutput.prettyPrint(json)
34+
}
35+
36+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.json
2+
3+
import spock.lang.Specification
4+
5+
import java.text.SimpleDateFormat
6+
7+
class JsonParserTest extends Specification {
8+
9+
JsonParser jsonParser
10+
11+
void setup () {
12+
jsonParser = new JsonParser()
13+
}
14+
15+
def 'Should parse to Account given Json String' () {
16+
given:
17+
def json = '{"id":"1234","value":15.6}'
18+
when:
19+
def account = jsonParser.toObject(json)
20+
then:
21+
account
22+
account instanceof Account
23+
account.id == '1234'
24+
account.value == 15.6
25+
}
26+
27+
def 'Should parse to Account given Json String with date property' () {
28+
given:
29+
def json = '{"id":"1234","value":15.6,"createdAt":"2018-01-01T00:00:00+0000"}'
30+
when:
31+
def account = jsonParser.toObjectWithIndexOverlay(json)
32+
then:
33+
account
34+
account instanceof Account
35+
account.id == '1234'
36+
account.value == 15.6
37+
println account.createdAt
38+
account.createdAt == Date.parse('yyyy-MM-dd', '2018-01-01')
39+
}
40+
41+
def 'Should parse to Json given an Account object' () {
42+
given:
43+
Account account = new Account(
44+
id: '123',
45+
value: 15.6,
46+
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
47+
)
48+
when:
49+
def json = jsonParser.toJson(account)
50+
then:
51+
json
52+
json == '{"value":15.6,"createdAt":"2018-01-01T00:00:00+0000","id":"123"}'
53+
}
54+
55+
def 'Should parse to Json given an Account object, a date format and fields to exclude' () {
56+
given:
57+
Account account = new Account(
58+
id: '123',
59+
value: 15.6,
60+
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
61+
)
62+
when:
63+
def json = jsonParser.toJson(account, 'MM/dd/yyyy', 'value')
64+
then:
65+
json
66+
json == '{"createdAt":"01/01/2018","id":"123"}'
67+
}
68+
69+
def 'Should prettify given a json string' () {
70+
given:
71+
String json = '{"value":15.6,"createdAt":"01/01/2018","id":"123456"}'
72+
when:
73+
def jsonPretty = jsonParser.prettyfy(json)
74+
then:
75+
jsonPretty
76+
jsonPretty == '{\n "value": 15.6,\n "createdAt": "01/01/2018",\n "id": "123456"\n}'
77+
}
78+
79+
80+
}

0 commit comments

Comments
 (0)