groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Wagenleitner <john.wagenleit...@gmail.com>
Subject Re: JsonOutput serialization options feature
Date Sun, 11 Sep 2016 22:02:48 GMT
On Sun, Sep 11, 2016 at 11:51 AM, Kostas Saidis <saikos@gmail.com> wrote:

> +1, definitely!
>
> If it is possible, the dateFormat method should also accept an optional
> Locale parameter (as mentioned in TODO comment in the source), with a
> default value of Locale.US.
>
> Cheers,
> Kostas
>
>

Thanks for pointing that out, I had forgot about that.  I added a new
method that accepts both a date format string and Locale.



>
> On 11/9/2016 6:59 πμ, John Wagenleitner wrote:
>
> Groovy Json users,
>
> I recently worked on a project where it would have been helpful if
> JsonOutput/JsonBuilder/StreamingJsonBuilder had options for controlling
> the output.  I noticed there were some open tickets [1] for feature
> requests around this so decided to try to add some options to control the
> serialization and have proposed a pull request (https://github.com/apache/
> groovy/pull/371).
>
> Just wanted to poll those that use the Json generating features in Groovy
> to see if you think these options would be useful and would welcome any
> code review for those that would be interested in reviewing the PR.  For a
> quick overview I've included a sample below [2].
>
> [1]
> GROOVY-6699: JSON slurper ability to ignore propertie/field names (at
> least the serializing part)
> GROOVY-6854: JsonOutput should be configurable with ISO 8601 UTC time zone
> instead of GMT
> GROOVY-7682: JsonBuilder or JsonOutput incompatible with JodaTime (for now
> via a closure converter)
> GROOVY-7780: JSONBuilder/JSONSlurper: Option to Only Include NonNull values
> GROOVY-7858: Make JsonBuilder configurable to not write entries with null
> values
>
> [2]
>
> import groovy.json.*
> def options = JsonOutput.options()
>         .excludeNulls()
>         .excludeFieldsByName('make', 'country', 'record')
>         .excludeFieldsByType(Number)
>         .addConverter(URL) { url -> '"http://groovy-lang.org"' }
>
> StringWriter writer = new StringWriter()
> StreamingJsonBuilder builder = new StreamingJsonBuilder(writer, options)
>
> builder.records {
>     car {
>         name 'HSV Maloo'        make 'Holden'        year 2006        country 'Australia'
       homepage new URL('http://example.org')
>         record {
>             type 'speed'            description 'production pickup truck with speed of
271kph'        }
>     }
> }
> assert writer.toString() == '{"records":{"car":{"name":"HSV Maloo","homepage":"http://groovy-lang.org"}}}'
>
>
>

Mime
View raw message