From 46e446683a4fc56ac5e8fe5b832a848a8fe35005 Mon Sep 17 00:00:00 2001 From: Bruno Matos Torrao Date: Sun, 25 Feb 2018 14:58:33 -0300 Subject: [PATCH 1/4] add: parsing json --- core-groovy/build.gradle | 13 +++ .../groovy/com/baeldung/json/Account.groovy | 7 ++ .../com/baeldung/json/JsonParser.groovy | 36 +++++++++ .../com/baeldung/json/JsonParserTest.groovy | 80 +++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 core-groovy/build.gradle create mode 100644 core-groovy/src/main/groovy/com/baeldung/json/Account.groovy create mode 100644 core-groovy/src/main/groovy/com/baeldung/json/JsonParser.groovy create mode 100644 core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy 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}' + } + + +} From 5a0932dff54b4dab3fa77b6743eafebc0cab0683 Mon Sep 17 00:00:00 2001 From: Bruno Matos Torrao Date: Sun, 25 Feb 2018 15:47:51 -0300 Subject: [PATCH 2/4] fix: maven build and tests --- core-groovy/pom.xml | 130 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 core-groovy/pom.xml diff --git a/core-groovy/pom.xml b/core-groovy/pom.xml new file mode 100644 index 0000000000..7ab91c7af2 --- /dev/null +++ b/core-groovy/pom.xml @@ -0,0 +1,130 @@ + + + 4.0.0 + + core-groovy + 1.0-SNAPSHOT + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + central + http://jcenter.bintray.com + + + + + + org.codehaus.groovy + groovy + 2.5.0-alpha-1 + + + org.codehaus.groovy + groovy-all + 2.5.0-alpha-1 + + + org.codehaus.groovy + groovy-sql + 2.5.0-alpha-1 + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.hsqldb + hsqldb + 2.4.0 + test + + + + org.spockframework + spock-core + 1.1-groovy-2.4 + test + + + + + + + org.codehaus.gmavenplus + gmavenplus-plugin + 1.6 + + + + addSources + addTestSources + compile + compileTests + + + + + + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + maven-failsafe-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + + + + + + UTF-8 + 1.1.2 + 1.1.2 + 1.1.2 + 1.1.2 + 0.15 + 1.5.0 + + 5.0.0 + 1.0.0 + 4.12.0 + 4.12 + + + \ No newline at end of file From 821360efbadb3c751a918a0f6990cfcee17af2fb Mon Sep 17 00:00:00 2001 From: Bruno Matos Torrao Date: Sun, 25 Feb 2018 16:31:53 -0300 Subject: [PATCH 3/4] fix: assert date --- .../src/test/groovy/com/baeldung/json/JsonParserTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy index 2bf2b0be7c..c383a1b6da 100644 --- a/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy @@ -26,7 +26,7 @@ class JsonParserTest extends Specification { 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"}' + def json = '{"id":"1234","value":15.6,"createdAt":"2018-01-01T00:00:00+0000"}' when: def account = jsonParser.toObjectWithIndexOverlay(json) then: From a25497f258f65162f6efbdff8bae7e3f427a4bef Mon Sep 17 00:00:00 2001 From: Bruno Matos Torrao Date: Sun, 25 Feb 2018 16:34:58 -0300 Subject: [PATCH 4/4] fix: assert date --- .../src/test/groovy/com/baeldung/json/JsonParserTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy index c383a1b6da..fcd51d58bc 100644 --- a/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy +++ b/core-groovy/src/test/groovy/com/baeldung/json/JsonParserTest.groovy @@ -49,7 +49,7 @@ class JsonParserTest extends Specification { def json = jsonParser.toJson(account) then: json - json == '{"value":15.6,"createdAt":"2018-01-01T02:00:00+0000","id":"123"}' + 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' () {