You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by John Wagenleitner <jo...@gmail.com> on 2016/09/11 03:59:12 UTC

JsonOutput serialization options feature

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"}}}'

Re: JsonOutput serialization options feature

Posted by Robert Stagner <re...@gmail.com>.
+1 from me.  I like the ability to pick and choose what I can exclude

On Sun, Sep 11, 2016 at 8:47 AM Guillaume Laforge <gl...@gmail.com>
wrote:

> More options sounds like a good idea.
>
> On Sun, Sep 11, 2016 at 5:43 PM, Suderman Keith <su...@anc.org> wrote:
>
>> +1
>>
>> I haven't had a chance to look at your pull request, but the lack of
>> configurability is the main reason I use Jackson instead of the Groovy
>> JsonBuilder/JsonOutput classes.
>>
>> Keith
>>
>> On Sep 10, 2016, at 8:59 PM, John Wagenleitner <
>> john.wagenleitner@gmail.com> 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"}}}'
>>
>>
>> ------------------------------
>> Research Associate
>> Department of Computer Science
>> Vassar College
>> Poughkeepsie, NY
>>
>>
>>
>> ------------------------------
>> Research Associate
>> Department of Computer Science
>> Vassar College
>> Poughkeepsie, NY
>>
>>
>
>
> --
> Guillaume Laforge
> Apache Groovy committer & PMC Vice-President
> Developer Advocate @ Google Cloud Platform
>
> Blog: http://glaforge.appspot.com/
> Social: @glaforge <http://twitter.com/glaforge> / Google+
> <https://plus.google.com/u/0/114130972232398734985/posts>
>

Re: JsonOutput serialization options feature

Posted by Guillaume Laforge <gl...@gmail.com>.
More options sounds like a good idea.

On Sun, Sep 11, 2016 at 5:43 PM, Suderman Keith <su...@anc.org> wrote:

> +1
>
> I haven't had a chance to look at your pull request, but the lack of
> configurability is the main reason I use Jackson instead of the Groovy
> JsonBuilder/JsonOutput classes.
>
> Keith
>
> On Sep 10, 2016, at 8:59 PM, John Wagenleitner <
> john.wagenleitner@gmail.com> 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"}}}'
>
>
> ------------------------------
> Research Associate
> Department of Computer Science
> Vassar College
> Poughkeepsie, NY
>
>
>
> ------------------------------
> Research Associate
> Department of Computer Science
> Vassar College
> Poughkeepsie, NY
>
>


-- 
Guillaume Laforge
Apache Groovy committer & PMC Vice-President
Developer Advocate @ Google Cloud Platform

Blog: http://glaforge.appspot.com/
Social: @glaforge <http://twitter.com/glaforge> / Google+
<https://plus.google.com/u/0/114130972232398734985/posts>

Re: JsonOutput serialization options feature

Posted by Suderman Keith <su...@anc.org>.
+1

I haven't had a chance to look at your pull request, but the lack of configurability is the main reason I use Jackson instead of the Groovy JsonBuilder/JsonOutput classes.

Keith

> On Sep 10, 2016, at 8:59 PM, John Wagenleitner <john.wagenleitner@gmail.com <ma...@gmail.com>> 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 <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 <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 <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 <http://groovy-lang.org/>"}}}'

------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY



------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY


Re: JsonOutput serialization options feature

Posted by Graeme Rocher <gr...@gmail.com>.
Great, yeah that would be a big help, since efficiency over the wire
for JSON services is a big deal and the unicode escaping doesn't help
there.

Also, can we consider https://github.com/apache/groovy/pull/429 as an
improvement? Again it makes using StreamingJsonBuilder more efficient
and maintainable.

Thanks

On Fri, Sep 23, 2016 at 1:11 AM, John Wagenleitner
<jo...@gmail.com> wrote:
> On Wed, Sep 21, 2016 at 3:22 AM, Graeme Rocher <gr...@gmail.com>
> wrote:
>>
>> Hi John,
>>
>> Looks very useful, so +1
>>
>> One thing that would also be useful is to be able to configure the
>> ability to disable unicode encoding. It doesn't seem to be recommended
>> for all circumstances. See:
>>
>>
>> http://stackoverflow.com/questions/12271547/shouldnt-json-stringify-escape-unicode-characters?noredirect=1&lq=1
>>
>> And it is blocking resolution of this issue:
>>
>> https://github.com/grails/grails-views/issues/71
>>
>> Thoughts?
>
>
>
> Hi Graeme,
>
> Good suggestion, and also seems to be a requested feature in GROOVY-6975
> [1].  So seems worthwhile to add a disableUnicodeEscaping or
> disableNonAsciiEscaping option.  I'll see if I can add that to the PR soon.
>
>
> [1] https://issues.apache.org/jira/browse/GROOVY-6975
>
>
>
>
>>
>>
>> On Mon, Sep 12, 2016 at 12:02 AM, John Wagenleitner
>> <jo...@gmail.com> wrote:
>> > On Sun, Sep 11, 2016 at 11:51 AM, Kostas Saidis <sa...@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"}}}'
>> >>
>> >>
>> >
>>
>>
>>
>> --
>> Graeme Rocher
>
>



-- 
Graeme Rocher

Re: JsonOutput serialization options feature

Posted by John Wagenleitner <jo...@gmail.com>.
On Wed, Sep 21, 2016 at 3:22 AM, Graeme Rocher <gr...@gmail.com>
wrote:

> Hi John,
>
> Looks very useful, so +1
>
> One thing that would also be useful is to be able to configure the
> ability to disable unicode encoding. It doesn't seem to be recommended
> for all circumstances. See:
>
> http://stackoverflow.com/questions/12271547/shouldnt-json-
> stringify-escape-unicode-characters?noredirect=1&lq=1
>
> And it is blocking resolution of this issue:
>
> https://github.com/grails/grails-views/issues/71
>
> Thoughts?
>


Hi Graeme,

Good suggestion, and also seems to be a requested feature in GROOVY-6975
[1].  So seems worthwhile to add a disableUnicodeEscaping or
disableNonAsciiEscaping option.  I'll see if I can add that to the PR soon.


[1] https://issues.apache.org/jira/browse/GROOVY-6975





>
> On Mon, Sep 12, 2016 at 12:02 AM, John Wagenleitner
> <jo...@gmail.com> wrote:
> > On Sun, Sep 11, 2016 at 11:51 AM, Kostas Saidis <sa...@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"}}}'
> >>
> >>
> >
>
>
>
> --
> Graeme Rocher
>

Re: JsonOutput serialization options feature

Posted by Graeme Rocher <gr...@gmail.com>.
Hi John,

Looks very useful, so +1

One thing that would also be useful is to be able to configure the
ability to disable unicode encoding. It doesn't seem to be recommended
for all circumstances. See:

http://stackoverflow.com/questions/12271547/shouldnt-json-stringify-escape-unicode-characters?noredirect=1&lq=1

And it is blocking resolution of this issue:

https://github.com/grails/grails-views/issues/71

Thoughts?


On Mon, Sep 12, 2016 at 12:02 AM, John Wagenleitner
<jo...@gmail.com> wrote:
> On Sun, Sep 11, 2016 at 11:51 AM, Kostas Saidis <sa...@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"}}}'
>>
>>
>



-- 
Graeme Rocher

Re: JsonOutput serialization options feature

Posted by John Wagenleitner <jo...@gmail.com>.
On Sun, Sep 11, 2016 at 11:51 AM, Kostas Saidis <sa...@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"}}}'
>
>
>

Re: JsonOutput serialization options feature

Posted by Kostas Saidis <sa...@gmail.com>.
+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

On 11/9/2016 6:59 \u03c0\u03bc, 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' year2006 country'Australia' homepagenew 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"}}}'