add: parsing json

This commit is contained in:
Bruno Matos Torrao 2018-02-25 14:58:33 -03:00
parent bdae4fe99b
commit 46e446683a
4 changed files with 136 additions and 0 deletions

13
core-groovy/build.gradle Normal file
View File

@ -0,0 +1,13 @@
group 'com.baeldung'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.5.0-alpha-1'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
}

View File

@ -0,0 +1,7 @@
package com.baeldung.json
class Account {
String id
BigDecimal value
Date createdAt
}

View File

@ -0,0 +1,36 @@
package com.baeldung.json
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
class JsonParser {
Account toObject(String json) {
JsonSlurper jsonSlurper = new JsonSlurper()
jsonSlurper.parseText(json) as Account
}
Account toObjectWithIndexOverlay(String json) {
JsonSlurper jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)
jsonSlurper.parseText(json) as Account
}
String toJson(Account account) {
JsonOutput.toJson(account)
}
String toJson(Account account, String dateFormat, String... fieldsToExclude) {
JsonGenerator generator = new JsonGenerator.Options()
.dateFormat(dateFormat)
.excludeFieldsByName(fieldsToExclude)
.build()
generator.toJson(account)
}
String prettyfy(String json) {
JsonOutput.prettyPrint(json)
}
}

View File

@ -0,0 +1,80 @@
package com.baeldung.json
import spock.lang.Specification
import java.text.SimpleDateFormat
class JsonParserTest extends Specification {
JsonParser jsonParser
void setup () {
jsonParser = new JsonParser()
}
def 'Should parse to Account given Json String' () {
given:
def json = '{"id":"1234","value":15.6}'
when:
def account = jsonParser.toObject(json)
then:
account
account instanceof Account
account.id == '1234'
account.value == 15.6
}
def 'Should parse to Account given Json String with date property' () {
given:
def json = '{"id":"1234","value":15.6,"createdAt":"2018-01-01T02:00:00+0000"}'
when:
def account = jsonParser.toObjectWithIndexOverlay(json)
then:
account
account instanceof Account
account.id == '1234'
account.value == 15.6
println account.createdAt
account.createdAt == Date.parse('yyyy-MM-dd', '2018-01-01')
}
def 'Should parse to Json given an Account object' () {
given:
Account account = new Account(
id: '123',
value: 15.6,
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
)
when:
def json = jsonParser.toJson(account)
then:
json
json == '{"value":15.6,"createdAt":"2018-01-01T02:00:00+0000","id":"123"}'
}
def 'Should parse to Json given an Account object, a date format and fields to exclude' () {
given:
Account account = new Account(
id: '123',
value: 15.6,
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
)
when:
def json = jsonParser.toJson(account, 'MM/dd/yyyy', 'value')
then:
json
json == '{"createdAt":"01/01/2018","id":"123"}'
}
def 'Should prettify given a json string' () {
given:
String json = '{"value":15.6,"createdAt":"01/01/2018","id":"123456"}'
when:
def jsonPretty = jsonParser.prettyfy(json)
then:
jsonPretty
jsonPretty == '{\n "value": 15.6,\n "createdAt": "01/01/2018",\n "id": "123456"\n}'
}
}