diff --git a/core-groovy/build.gradle b/core-groovy/build.gradle new file mode 100644 index 0000000000..b3f33836da --- /dev/null +++ b/core-groovy/build.gradle @@ -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' +} diff --git a/core-groovy/src/main/groovy/com/baeldung/json/Account.groovy b/core-groovy/src/main/groovy/com/baeldung/json/Account.groovy new file mode 100644 index 0000000000..84b294f0bd --- /dev/null +++ b/core-groovy/src/main/groovy/com/baeldung/json/Account.groovy @@ -0,0 +1,7 @@ +package com.baeldung.json + +class Account { + String id + BigDecimal value + Date createdAt +} \ No newline at end of file diff --git a/core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy b/core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy new file mode 100644 index 0000000000..0d7c451972 --- /dev/null +++ b/core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy @@ -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) + } + +} diff --git a/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy new file mode 100644 index 0000000000..2bf2b0be7c --- /dev/null +++ b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy @@ -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}' + } + + +}