File tree Expand file tree Collapse file tree
main/groovy/com/baeldung/json
test/groovy/com/baeldung/json Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,6 +8,6 @@ repositories {
88}
99
1010dependencies {
11- compile ' org.codehaus.groovy:groovy-all:2.5.0-alpha-1 '
11+ compile ' org.codehaus.groovy:groovy-all:2.4.13 '
1212 testCompile ' org.spockframework:spock-core:1.1-groovy-2.4'
1313}
Original file line number Diff line number Diff line change 2323 <dependency >
2424 <groupId >org.codehaus.groovy</groupId >
2525 <artifactId >groovy</artifactId >
26- <version >2.5.0-alpha-1 </version >
26+ <version >2.4.13 </version >
2727 </dependency >
2828 <dependency >
2929 <groupId >org.codehaus.groovy</groupId >
3030 <artifactId >groovy-all</artifactId >
31- <version >2.5.0-alpha-1 </version >
31+ <version >2.4.13 </version >
3232 </dependency >
3333 <dependency >
3434 <groupId >org.codehaus.groovy</groupId >
3535 <artifactId >groovy-sql</artifactId >
36- <version >2.5.0-alpha-1 </version >
36+ <version >2.4.13 </version >
3737 </dependency >
3838 <dependency >
3939 <groupId >org.junit.jupiter</groupId >
Original file line number Diff line number Diff line change 11package com.baeldung.json
22
3- import groovy.json.JsonGenerator
43import groovy.json.JsonOutput
54import groovy.json.JsonParserType
65import groovy.json.JsonSlurper
@@ -21,14 +20,6 @@ class JsonParser {
2120 JsonOutput . toJson(account)
2221 }
2322
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-
3223 String prettyfy (String json ) {
3324 JsonOutput . prettyPrint(json)
3425 }
Original file line number Diff line number Diff line change @@ -52,20 +52,6 @@ class JsonParserTest extends Specification {
5252 json == ' {"value":15.6,"createdAt":"2018-01-01T00:00:00+0000","id":"123"}'
5353 }
5454
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-
6955 def ' Should prettify given a json string' () {
7056 given :
7157 String json = ' {"value":15.6,"createdAt":"01/01/2018","id":"123456"}'
You can’t perform that action at this time.
0 commit comments