You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by James Kleeh <ja...@gmail.com> on 2016/10/25 19:07:38 UTC

2.5 JsonBuilder Converters thoughts

Currently if one were to register a converter with options like so:

JsonGenerator.Options options = new JsonGenerator.Options()
options.addConverter(MyCustomType) { MyCustomType mct ->
    mct.name
}
    

Assuming “mct.name” returns a string, it will be output without quotes because of:

Converter converter = findConverter(objectClass);
if (converter != null) {
    writeRaw(converter.convert(object, key), buffer);
    return;
}

I’d be curious to hear what the use case is for outputting the data as raw. I think the percentage of users that would prefer to have their data further processed by that method would vastly outnumber the ones that do not.

I think something like this would be a better solution:

Converter converter = findConverter(objectClass);
if (converter != null) {
    object = converter.convert(object, key);
}

Thoughts?

Thanks,
James Kleeh

Re: 2.5 JsonBuilder Converters thoughts

Posted by James Kleeh <ja...@gmail.com>.
John,

PR submitted: https://github.com/apache/groovy/pull/457 <https://github.com/apache/groovy/pull/457>

James

> On Nov 4, 2016, at 12:30 PM, John Wagenleitner <jo...@gmail.com> wrote:
> 
> I think there is consensus and a PR would be great.
> 
> On Fri, Nov 4, 2016 at 9:23 AM, James Kleeh <james.kleeh@gmail.com <ma...@gmail.com>> wrote:
> Do we have any consensus on this? Should I submit a PR with the change?
> 
> > On Oct 28, 2016, at 6:51 AM, Graeme Rocher <graeme.rocher@gmail.com <ma...@gmail.com>> wrote:
> >
> > one thing to note. I would assume if we allow JsonOutput.unescaped it
> > would the convert method would have to return Object? Currently the
> > JsonUnescaped type doesn't implement CharSequence
> >
> > If that is the case it may also be interesting to support return types
> > of Writable since.
> >
> > Cheers
> >
> > On Fri, Oct 28, 2016 at 12:46 PM, Graeme Rocher <graeme.rocher@gmail.com <ma...@gmail.com>> wrote:
> >> Yeah I agree JsonOutput.unescaped allows the same flexibility without
> >> compromising the most common use case.
> >>
> >> Cheers
> >>
> >> On Tue, Oct 25, 2016 at 10:32 PM, John Wagenleitner
> >> <john.wagenleitner@gmail.com <ma...@gmail.com>> wrote:
> >>> On Tue, Oct 25, 2016 at 12:07 PM, James Kleeh <james.kleeh@gmail.com <ma...@gmail.com>> wrote:
> >>>>
> >>>> Currently if one were to register a converter with options like so:
> >>>>
> >>>> JsonGenerator.Options options = new JsonGenerator.Options()
> >>>> options.addConverter(MyCustomType) { MyCustomType mct ->
> >>>>    mct.name <http://mct.name/>
> >>>> }
> >>>>
> >>>>
> >>>> Assuming “mct.name <http://mct.name/>” returns a string, it will be output without quotes
> >>>> because of:
> >>>>
> >>>> Converter converter = findConverter(objectClass);
> >>>> if (converter != null) {
> >>>>    writeRaw(converter.convert(object, key), buffer);
> >>>>    return;
> >>>> }
> >>>>
> >>>> I’d be curious to hear what the use case is for outputting the data as
> >>>> raw. I think the percentage of users that would prefer to have their data
> >>>> further processed by that method would vastly outnumber the ones that do
> >>>> not.
> >>>>
> >>>> I think something like this would be a better solution:
> >>>>
> >>>> Converter converter = findConverter(objectClass);
> >>>> if (converter != null) {
> >>>>    object = converter.convert(object, key);
> >>>> }
> >>>>
> >>>> Thoughts?
> >>>
> >>>
> >>>
> >>> I had aimed for making it as flexible as possible and purposefully had it
> >>> not participate in further processing to avoid surprises and be able to
> >>> output JSON that otherwise wouldn't be possible due to the way the generator
> >>> was already configured (nulls or being able to write out a number such as
> >>> 9.3e7).
> >>>
> >>> I like your suggestion and think the flexibility could still be accomplished
> >>> using JsonOutput.unescaped; and since 2.5 isn't out yet there's still time
> >>> to refine how it works.
> >>>
> >>> I also would like to hear what others think.
> >>>
> >>> John
> >>
> >>
> >>
> >> --
> >> Graeme Rocher
> >
> >
> >
> > --
> > Graeme Rocher
> 
> 


Re: 2.5 JsonBuilder Converters thoughts

Posted by John Wagenleitner <jo...@gmail.com>.
I think there is consensus and a PR would be great.

On Fri, Nov 4, 2016 at 9:23 AM, James Kleeh <ja...@gmail.com> wrote:

> Do we have any consensus on this? Should I submit a PR with the change?
>
> > On Oct 28, 2016, at 6:51 AM, Graeme Rocher <gr...@gmail.com>
> wrote:
> >
> > one thing to note. I would assume if we allow JsonOutput.unescaped it
> > would the convert method would have to return Object? Currently the
> > JsonUnescaped type doesn't implement CharSequence
> >
> > If that is the case it may also be interesting to support return types
> > of Writable since.
> >
> > Cheers
> >
> > On Fri, Oct 28, 2016 at 12:46 PM, Graeme Rocher <gr...@gmail.com>
> wrote:
> >> Yeah I agree JsonOutput.unescaped allows the same flexibility without
> >> compromising the most common use case.
> >>
> >> Cheers
> >>
> >> On Tue, Oct 25, 2016 at 10:32 PM, John Wagenleitner
> >> <jo...@gmail.com> wrote:
> >>> On Tue, Oct 25, 2016 at 12:07 PM, James Kleeh <ja...@gmail.com>
> wrote:
> >>>>
> >>>> Currently if one were to register a converter with options like so:
> >>>>
> >>>> JsonGenerator.Options options = new JsonGenerator.Options()
> >>>> options.addConverter(MyCustomType) { MyCustomType mct ->
> >>>>    mct.name
> >>>> }
> >>>>
> >>>>
> >>>> Assuming “mct.name” returns a string, it will be output without
> quotes
> >>>> because of:
> >>>>
> >>>> Converter converter = findConverter(objectClass);
> >>>> if (converter != null) {
> >>>>    writeRaw(converter.convert(object, key), buffer);
> >>>>    return;
> >>>> }
> >>>>
> >>>> I’d be curious to hear what the use case is for outputting the data as
> >>>> raw. I think the percentage of users that would prefer to have their
> data
> >>>> further processed by that method would vastly outnumber the ones that
> do
> >>>> not.
> >>>>
> >>>> I think something like this would be a better solution:
> >>>>
> >>>> Converter converter = findConverter(objectClass);
> >>>> if (converter != null) {
> >>>>    object = converter.convert(object, key);
> >>>> }
> >>>>
> >>>> Thoughts?
> >>>
> >>>
> >>>
> >>> I had aimed for making it as flexible as possible and purposefully had
> it
> >>> not participate in further processing to avoid surprises and be able to
> >>> output JSON that otherwise wouldn't be possible due to the way the
> generator
> >>> was already configured (nulls or being able to write out a number such
> as
> >>> 9.3e7).
> >>>
> >>> I like your suggestion and think the flexibility could still be
> accomplished
> >>> using JsonOutput.unescaped; and since 2.5 isn't out yet there's still
> time
> >>> to refine how it works.
> >>>
> >>> I also would like to hear what others think.
> >>>
> >>> John
> >>
> >>
> >>
> >> --
> >> Graeme Rocher
> >
> >
> >
> > --
> > Graeme Rocher
>
>

Re: 2.5 JsonBuilder Converters thoughts

Posted by James Kleeh <ja...@gmail.com>.
Do we have any consensus on this? Should I submit a PR with the change?

> On Oct 28, 2016, at 6:51 AM, Graeme Rocher <gr...@gmail.com> wrote:
> 
> one thing to note. I would assume if we allow JsonOutput.unescaped it
> would the convert method would have to return Object? Currently the
> JsonUnescaped type doesn't implement CharSequence
> 
> If that is the case it may also be interesting to support return types
> of Writable since.
> 
> Cheers
> 
> On Fri, Oct 28, 2016 at 12:46 PM, Graeme Rocher <gr...@gmail.com> wrote:
>> Yeah I agree JsonOutput.unescaped allows the same flexibility without
>> compromising the most common use case.
>> 
>> Cheers
>> 
>> On Tue, Oct 25, 2016 at 10:32 PM, John Wagenleitner
>> <jo...@gmail.com> wrote:
>>> On Tue, Oct 25, 2016 at 12:07 PM, James Kleeh <ja...@gmail.com> wrote:
>>>> 
>>>> Currently if one were to register a converter with options like so:
>>>> 
>>>> JsonGenerator.Options options = new JsonGenerator.Options()
>>>> options.addConverter(MyCustomType) { MyCustomType mct ->
>>>>    mct.name
>>>> }
>>>> 
>>>> 
>>>> Assuming “mct.name” returns a string, it will be output without quotes
>>>> because of:
>>>> 
>>>> Converter converter = findConverter(objectClass);
>>>> if (converter != null) {
>>>>    writeRaw(converter.convert(object, key), buffer);
>>>>    return;
>>>> }
>>>> 
>>>> I’d be curious to hear what the use case is for outputting the data as
>>>> raw. I think the percentage of users that would prefer to have their data
>>>> further processed by that method would vastly outnumber the ones that do
>>>> not.
>>>> 
>>>> I think something like this would be a better solution:
>>>> 
>>>> Converter converter = findConverter(objectClass);
>>>> if (converter != null) {
>>>>    object = converter.convert(object, key);
>>>> }
>>>> 
>>>> Thoughts?
>>> 
>>> 
>>> 
>>> I had aimed for making it as flexible as possible and purposefully had it
>>> not participate in further processing to avoid surprises and be able to
>>> output JSON that otherwise wouldn't be possible due to the way the generator
>>> was already configured (nulls or being able to write out a number such as
>>> 9.3e7).
>>> 
>>> I like your suggestion and think the flexibility could still be accomplished
>>> using JsonOutput.unescaped; and since 2.5 isn't out yet there's still time
>>> to refine how it works.
>>> 
>>> I also would like to hear what others think.
>>> 
>>> John
>> 
>> 
>> 
>> --
>> Graeme Rocher
> 
> 
> 
> -- 
> Graeme Rocher


Re: 2.5 JsonBuilder Converters thoughts

Posted by Graeme Rocher <gr...@gmail.com>.
one thing to note. I would assume if we allow JsonOutput.unescaped it
would the convert method would have to return Object? Currently the
JsonUnescaped type doesn't implement CharSequence

If that is the case it may also be interesting to support return types
of Writable since.

Cheers

On Fri, Oct 28, 2016 at 12:46 PM, Graeme Rocher <gr...@gmail.com> wrote:
> Yeah I agree JsonOutput.unescaped allows the same flexibility without
> compromising the most common use case.
>
> Cheers
>
> On Tue, Oct 25, 2016 at 10:32 PM, John Wagenleitner
> <jo...@gmail.com> wrote:
>> On Tue, Oct 25, 2016 at 12:07 PM, James Kleeh <ja...@gmail.com> wrote:
>>>
>>> Currently if one were to register a converter with options like so:
>>>
>>> JsonGenerator.Options options = new JsonGenerator.Options()
>>> options.addConverter(MyCustomType) { MyCustomType mct ->
>>>     mct.name
>>> }
>>>
>>>
>>> Assuming “mct.name” returns a string, it will be output without quotes
>>> because of:
>>>
>>> Converter converter = findConverter(objectClass);
>>> if (converter != null) {
>>>     writeRaw(converter.convert(object, key), buffer);
>>>     return;
>>> }
>>>
>>> I’d be curious to hear what the use case is for outputting the data as
>>> raw. I think the percentage of users that would prefer to have their data
>>> further processed by that method would vastly outnumber the ones that do
>>> not.
>>>
>>> I think something like this would be a better solution:
>>>
>>> Converter converter = findConverter(objectClass);
>>> if (converter != null) {
>>>     object = converter.convert(object, key);
>>> }
>>>
>>> Thoughts?
>>
>>
>>
>> I had aimed for making it as flexible as possible and purposefully had it
>> not participate in further processing to avoid surprises and be able to
>> output JSON that otherwise wouldn't be possible due to the way the generator
>> was already configured (nulls or being able to write out a number such as
>> 9.3e7).
>>
>> I like your suggestion and think the flexibility could still be accomplished
>> using JsonOutput.unescaped; and since 2.5 isn't out yet there's still time
>> to refine how it works.
>>
>> I also would like to hear what others think.
>>
>> John
>
>
>
> --
> Graeme Rocher



-- 
Graeme Rocher

Re: 2.5 JsonBuilder Converters thoughts

Posted by Graeme Rocher <gr...@gmail.com>.
Yeah I agree JsonOutput.unescaped allows the same flexibility without
compromising the most common use case.

Cheers

On Tue, Oct 25, 2016 at 10:32 PM, John Wagenleitner
<jo...@gmail.com> wrote:
> On Tue, Oct 25, 2016 at 12:07 PM, James Kleeh <ja...@gmail.com> wrote:
>>
>> Currently if one were to register a converter with options like so:
>>
>> JsonGenerator.Options options = new JsonGenerator.Options()
>> options.addConverter(MyCustomType) { MyCustomType mct ->
>>     mct.name
>> }
>>
>>
>> Assuming “mct.name” returns a string, it will be output without quotes
>> because of:
>>
>> Converter converter = findConverter(objectClass);
>> if (converter != null) {
>>     writeRaw(converter.convert(object, key), buffer);
>>     return;
>> }
>>
>> I’d be curious to hear what the use case is for outputting the data as
>> raw. I think the percentage of users that would prefer to have their data
>> further processed by that method would vastly outnumber the ones that do
>> not.
>>
>> I think something like this would be a better solution:
>>
>> Converter converter = findConverter(objectClass);
>> if (converter != null) {
>>     object = converter.convert(object, key);
>> }
>>
>> Thoughts?
>
>
>
> I had aimed for making it as flexible as possible and purposefully had it
> not participate in further processing to avoid surprises and be able to
> output JSON that otherwise wouldn't be possible due to the way the generator
> was already configured (nulls or being able to write out a number such as
> 9.3e7).
>
> I like your suggestion and think the flexibility could still be accomplished
> using JsonOutput.unescaped; and since 2.5 isn't out yet there's still time
> to refine how it works.
>
> I also would like to hear what others think.
>
> John



-- 
Graeme Rocher

Re: 2.5 JsonBuilder Converters thoughts

Posted by John Wagenleitner <jo...@gmail.com>.
On Tue, Oct 25, 2016 at 12:07 PM, James Kleeh <ja...@gmail.com> wrote:

> Currently if one were to register a converter with options like so:
>
> JsonGenerator.Options options = new JsonGenerator.Options()
> options.addConverter(MyCustomType) { MyCustomType mct ->
>     mct.name
> }
>
>
> Assuming “mct.name” returns a string, it will be output without quotes
> because of:
>
> Converter converter = findConverter(objectClass);
> if (converter != null) {
>     writeRaw(converter.convert(object, key), buffer);
>     return;
> }
>
> I’d be curious to hear what the use case is for outputting the data as
> raw. I think the percentage of users that would prefer to have their data
> further processed by that method would vastly outnumber the ones that do
> not.
>
> I think something like this would be a better solution:
>
> Converter converter = findConverter(objectClass);
> if (converter != null) {
>     object = converter.convert(object, key);
> }
>
> Thoughts?
>


I had aimed for making it as flexible as possible and purposefully had it
not participate in further processing to avoid surprises and be able to
output JSON that otherwise wouldn't be possible due to the way the
generator was already configured (nulls or being able to write out a number
such as 9.3e7).

I like your suggestion and think the flexibility could still be
accomplished using JsonOutput.unescaped; and since 2.5 isn't out yet
there's still time to refine how it works.

I also would like to hear what others think.

John