Skip to content

Commit 46e4466

Browse files
committed
add: parsing json
1 parent bdae4fe commit 46e4466

4 files changed

Lines changed: 136 additions & 0 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+
}
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-01T02: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-01T02: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)