commit
ea306bec5f
|
@ -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'
|
||||
}
|
|
@ -24,12 +24,17 @@
|
|||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy</artifactId>
|
||||
<version>2.4.13</version>
|
||||
<version>2.5.0-alpha-1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>2.5.0-alpha-1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-sql</artifactId>
|
||||
<version>2.4.13</version>
|
||||
<version>2.5.0-alpha-1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
@ -49,6 +54,12 @@
|
|||
<version>2.4.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spockframework</groupId>
|
||||
<artifactId>spock-core</artifactId>
|
||||
<version>1.1-groovy-2.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -114,5 +125,5 @@
|
|||
<junit.vintage.version>4.12.0</junit.vintage.version>
|
||||
<junit4.version>4.12</junit4.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.json
|
||||
|
||||
class Account {
|
||||
String id
|
||||
BigDecimal value
|
||||
Date createdAt
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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-01T00: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-01T00: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}'
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue