You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Guillaume Laforge <gl...@gmail.com> on 2023/09/04 16:13:00 UTC

Records support in JSON

Hi,

I was a bit surprised by this behavior with serializing records in JSON.

I was expecting this assertion to pass:

import groovy.json.JsonOutput
record Person(String name) {}
assert JsonOutput.toJson([new Person('Guillaume')]) ==
'[{"name":"Guillaume"}]'

Instead, I got [{}]

Shouldn't records be treated like POGOs and thus serialize the same way
like in my example above? Or is there a good reason why this is not the
case?

Guillaume

-- 
*Guillaume Laforge*
Apache Groovy committer
Developer Advocate @ Google Cloud <https://cloud.google.com/>

   - Blog: glaforge.dev
   - Twitter: @glaforge <http://twitter.com/glaforge>
   - Mastodon: @glaforge@uwyn.net <http://%40glaforge@uwyn.net/>

Re: Records support in JSON

Posted by Guillaume Laforge <gl...@gmail.com>.
Neat! It looks great!
Thank you.

On Wed, Sep 6, 2023 at 4:04 PM Paul King <pa...@asert.com.au> wrote:

> I have a prototype fix. It alters RecordTypeASTTransformation to emit
> a BeanInfo class in the emulate case and MetaClassImpl to handle
> properties within native records. The following script then works:
>
> ============
> @RecordOptions(mode=EMULATE)
> record PersonA(String name) {}
>
> @RecordOptions(mode=NATIVE)
> record PersonB(String name) {}
>
> class PersonC {
>   String name
> }
>
> def a = new PersonA('Guillaume')
> println a.metaClass.properties*.name
> println a.metaClass.properties*.class.simpleName
>
> def b = new PersonB('Jochen')
> println b.metaClass.properties*.name
> println b.metaClass.properties*.class.simpleName
>
> def c = new PersonC(name: 'Eric')
> println c.metaClass.properties*.name
> println c.metaClass.properties*.class.simpleName
>
> import groovy.json.JsonOutput
>
> println JsonOutput.toJson([a,b,c])
> =============
> With output:
>
> [name, serialVersionUID, class]
> [MetaBeanProperty, CachedField, MetaBeanProperty]
> [name, serialVersionUID, class]
> [MetaBeanProperty, CachedField, MetaBeanProperty]
> [name, class]
> [MetaBeanProperty, MetaBeanProperty]
> [{"name":"Guillaume"},{"name":"Jochen"},{"name":"Eric"}]
>
> I'll add some more tests and create a PR tomorrow.
>
> Paul.
>
> <
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
> >
> Virus-free.www.avast.com
> <
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
> >
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> On Tue, Sep 5, 2023 at 6:48 PM Guillaume Laforge <gl...@gmail.com>
> wrote:
> >
> > I also forget the fact that Groovy records are not always Java records
> when running on older JDKs...
> > So it needs to make the difference with real and groovy-specific
> fallthrough records.
> >
> > On Tue, Sep 5, 2023 at 10:46 AM Guillaume Laforge <gl...@gmail.com>
> wrote:
> >>
> >> Sure, I just created:
> >> https://issues.apache.org/jira/browse/GROOVY-11167
> >>
> >> Also, thanks for the workaround.
> >> I ended up just using a POGO instead, but I hadn't thought of
> implementing a Map.
> >>
> >> I guess JsonOutput needs to be updated here.
> >> I suppose we would add a new if case that check if the class of the
> object is a record, and the retrieve the components with
> clazz.getRecordComponents()?
> >>
> >> Guillaume
> >>
> >> On Tue, Sep 5, 2023 at 7:44 AM Paul King <pa...@asert.com.au> wrote:
> >>>
> >>> We aren't picking up the metabean properties for the record correctly.
> >>> Would you like to file a bug report?
> >>>
> >>> Workarounds are to call "toMap()" (or *.toMap() on the list), or do
> >>> something like:
> >>> ```
> >>> @AutoImplement
> >>> record Person(String name) implements Map {
> >>>   Set entrySet() { toMap().entrySet() }
> >>> }
> >>> ```
> >>>
> >>>
> >>> <
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
> >
> >>> Virus-free.www.avast.com
> >>> <
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
> >
> >>> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
> >>>
> >>> On Tue, Sep 5, 2023 at 2:13 AM Guillaume Laforge <gl...@gmail.com>
> wrote:
> >>> >
> >>> > Hi,
> >>> >
> >>> > I was a bit surprised by this behavior with serializing records in
> JSON.
> >>> >
> >>> > I was expecting this assertion to pass:
> >>> >
> >>> > import groovy.json.JsonOutput
> >>> > record Person(String name) {}
> >>> > assert JsonOutput.toJson([new Person('Guillaume')]) ==
> '[{"name":"Guillaume"}]'
> >>> >
> >>> > Instead, I got [{}]
> >>> >
> >>> > Shouldn't records be treated like POGOs and thus serialize the same
> way like in my example above? Or is there a good reason why this is not the
> case?
> >>> >
> >>> > Guillaume
> >>> >
> >>> > --
> >>> > Guillaume Laforge
> >>> > Apache Groovy committer
> >>> > Developer Advocate @ Google Cloud
> >>> >
> >>> > Blog: glaforge.dev
> >>> > Twitter: @glaforge
> >>> > Mastodon: @glaforge@uwyn.net
> >>
> >>
> >>
> >> --
> >> Guillaume Laforge
> >> Apache Groovy committer
> >> Developer Advocate @ Google Cloud
> >>
> >> Blog: glaforge.dev
> >> Twitter: @glaforge
> >> Mastodon: @glaforge@uwyn.net
> >
> >
> >
> > --
> > Guillaume Laforge
> > Apache Groovy committer
> > Developer Advocate @ Google Cloud
> >
> > Blog: glaforge.dev
> > Twitter: @glaforge
> > Mastodon: @glaforge@uwyn.net
>


-- 
*Guillaume Laforge*
Apache Groovy committer
Developer Advocate @ Google Cloud <https://cloud.google.com/>

   - Blog: glaforge.dev
   - Twitter: @glaforge <http://twitter.com/glaforge>
   - Mastodon: @glaforge@uwyn.net <http://%40glaforge@uwyn.net/>

Re: Records support in JSON

Posted by Paul King <pa...@asert.com.au>.
I have a prototype fix. It alters RecordTypeASTTransformation to emit
a BeanInfo class in the emulate case and MetaClassImpl to handle
properties within native records. The following script then works:

============
@RecordOptions(mode=EMULATE)
record PersonA(String name) {}

@RecordOptions(mode=NATIVE)
record PersonB(String name) {}

class PersonC {
  String name
}

def a = new PersonA('Guillaume')
println a.metaClass.properties*.name
println a.metaClass.properties*.class.simpleName

def b = new PersonB('Jochen')
println b.metaClass.properties*.name
println b.metaClass.properties*.class.simpleName

def c = new PersonC(name: 'Eric')
println c.metaClass.properties*.name
println c.metaClass.properties*.class.simpleName

import groovy.json.JsonOutput

println JsonOutput.toJson([a,b,c])
=============
With output:

[name, serialVersionUID, class]
[MetaBeanProperty, CachedField, MetaBeanProperty]
[name, serialVersionUID, class]
[MetaBeanProperty, CachedField, MetaBeanProperty]
[name, class]
[MetaBeanProperty, MetaBeanProperty]
[{"name":"Guillaume"},{"name":"Jochen"},{"name":"Eric"}]

I'll add some more tests and create a PR tomorrow.

Paul.

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Tue, Sep 5, 2023 at 6:48 PM Guillaume Laforge <gl...@gmail.com> wrote:
>
> I also forget the fact that Groovy records are not always Java records when running on older JDKs...
> So it needs to make the difference with real and groovy-specific fallthrough records.
>
> On Tue, Sep 5, 2023 at 10:46 AM Guillaume Laforge <gl...@gmail.com> wrote:
>>
>> Sure, I just created:
>> https://issues.apache.org/jira/browse/GROOVY-11167
>>
>> Also, thanks for the workaround.
>> I ended up just using a POGO instead, but I hadn't thought of implementing a Map.
>>
>> I guess JsonOutput needs to be updated here.
>> I suppose we would add a new if case that check if the class of the object is a record, and the retrieve the components with clazz.getRecordComponents()?
>>
>> Guillaume
>>
>> On Tue, Sep 5, 2023 at 7:44 AM Paul King <pa...@asert.com.au> wrote:
>>>
>>> We aren't picking up the metabean properties for the record correctly.
>>> Would you like to file a bug report?
>>>
>>> Workarounds are to call "toMap()" (or *.toMap() on the list), or do
>>> something like:
>>> ```
>>> @AutoImplement
>>> record Person(String name) implements Map {
>>>   Set entrySet() { toMap().entrySet() }
>>> }
>>> ```
>>>
>>>
>>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>>> Virus-free.www.avast.com
>>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>>> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>>
>>> On Tue, Sep 5, 2023 at 2:13 AM Guillaume Laforge <gl...@gmail.com> wrote:
>>> >
>>> > Hi,
>>> >
>>> > I was a bit surprised by this behavior with serializing records in JSON.
>>> >
>>> > I was expecting this assertion to pass:
>>> >
>>> > import groovy.json.JsonOutput
>>> > record Person(String name) {}
>>> > assert JsonOutput.toJson([new Person('Guillaume')]) == '[{"name":"Guillaume"}]'
>>> >
>>> > Instead, I got [{}]
>>> >
>>> > Shouldn't records be treated like POGOs and thus serialize the same way like in my example above? Or is there a good reason why this is not the case?
>>> >
>>> > Guillaume
>>> >
>>> > --
>>> > Guillaume Laforge
>>> > Apache Groovy committer
>>> > Developer Advocate @ Google Cloud
>>> >
>>> > Blog: glaforge.dev
>>> > Twitter: @glaforge
>>> > Mastodon: @glaforge@uwyn.net
>>
>>
>>
>> --
>> Guillaume Laforge
>> Apache Groovy committer
>> Developer Advocate @ Google Cloud
>>
>> Blog: glaforge.dev
>> Twitter: @glaforge
>> Mastodon: @glaforge@uwyn.net
>
>
>
> --
> Guillaume Laforge
> Apache Groovy committer
> Developer Advocate @ Google Cloud
>
> Blog: glaforge.dev
> Twitter: @glaforge
> Mastodon: @glaforge@uwyn.net

Re: Records support in JSON

Posted by Guillaume Laforge <gl...@gmail.com>.
I also forget the fact that Groovy records are not always Java records when
running on older JDKs...
So it needs to make the difference with real and groovy-specific
fallthrough records.

On Tue, Sep 5, 2023 at 10:46 AM Guillaume Laforge <gl...@gmail.com>
wrote:

> Sure, I just created:
> https://issues.apache.org/jira/browse/GROOVY-11167
>
> Also, thanks for the workaround.
> I ended up just using a POGO instead, but I hadn't thought of implementing
> a Map.
>
> I guess JsonOutput needs to be updated here
> <https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java#L174>
> .
> I suppose we would add a new if case that check if the class of the object
> is a record, and the retrieve the components with
> clazz.getRecordComponents()?
>
> Guillaume
>
> On Tue, Sep 5, 2023 at 7:44 AM Paul King <pa...@asert.com.au> wrote:
>
>> We aren't picking up the metabean properties for the record correctly.
>> Would you like to file a bug report?
>>
>> Workarounds are to call "toMap()" (or *.toMap() on the list), or do
>> something like:
>> ```
>> @AutoImplement
>> record Person(String name) implements Map {
>>   Set entrySet() { toMap().entrySet() }
>> }
>> ```
>>
>>
>> <
>> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
>> >
>> Virus-free.www.avast.com
>> <
>> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
>> >
>> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>
>> On Tue, Sep 5, 2023 at 2:13 AM Guillaume Laforge <gl...@gmail.com>
>> wrote:
>> >
>> > Hi,
>> >
>> > I was a bit surprised by this behavior with serializing records in JSON.
>> >
>> > I was expecting this assertion to pass:
>> >
>> > import groovy.json.JsonOutput
>> > record Person(String name) {}
>> > assert JsonOutput.toJson([new Person('Guillaume')]) ==
>> '[{"name":"Guillaume"}]'
>> >
>> > Instead, I got [{}]
>> >
>> > Shouldn't records be treated like POGOs and thus serialize the same way
>> like in my example above? Or is there a good reason why this is not the
>> case?
>> >
>> > Guillaume
>> >
>> > --
>> > Guillaume Laforge
>> > Apache Groovy committer
>> > Developer Advocate @ Google Cloud
>> >
>> > Blog: glaforge.dev
>> > Twitter: @glaforge
>> > Mastodon: @glaforge@uwyn.net
>>
>
>
> --
> *Guillaume Laforge*
> Apache Groovy committer
> Developer Advocate @ Google Cloud <https://cloud.google.com/>
>
>    - Blog: glaforge.dev
>    - Twitter: @glaforge <http://twitter.com/glaforge>
>    - Mastodon: @glaforge@uwyn.net <http://%40glaforge@uwyn.net/>
>
>

-- 
*Guillaume Laforge*
Apache Groovy committer
Developer Advocate @ Google Cloud <https://cloud.google.com/>

   - Blog: glaforge.dev
   - Twitter: @glaforge <http://twitter.com/glaforge>
   - Mastodon: @glaforge@uwyn.net <http://%40glaforge@uwyn.net/>

Re: Records support in JSON

Posted by Guillaume Laforge <gl...@gmail.com>.
Sure, I just created:
https://issues.apache.org/jira/browse/GROOVY-11167

Also, thanks for the workaround.
I ended up just using a POGO instead, but I hadn't thought of implementing
a Map.

I guess JsonOutput needs to be updated here
<https://github.com/apache/groovy/blob/master/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java#L174>
.
I suppose we would add a new if case that check if the class of the object
is a record, and the retrieve the components with
clazz.getRecordComponents()?

Guillaume

On Tue, Sep 5, 2023 at 7:44 AM Paul King <pa...@asert.com.au> wrote:

> We aren't picking up the metabean properties for the record correctly.
> Would you like to file a bug report?
>
> Workarounds are to call "toMap()" (or *.toMap() on the list), or do
> something like:
> ```
> @AutoImplement
> record Person(String name) implements Map {
>   Set entrySet() { toMap().entrySet() }
> }
> ```
>
>
> <
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
> >
> Virus-free.www.avast.com
> <
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail
> >
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> On Tue, Sep 5, 2023 at 2:13 AM Guillaume Laforge <gl...@gmail.com>
> wrote:
> >
> > Hi,
> >
> > I was a bit surprised by this behavior with serializing records in JSON.
> >
> > I was expecting this assertion to pass:
> >
> > import groovy.json.JsonOutput
> > record Person(String name) {}
> > assert JsonOutput.toJson([new Person('Guillaume')]) ==
> '[{"name":"Guillaume"}]'
> >
> > Instead, I got [{}]
> >
> > Shouldn't records be treated like POGOs and thus serialize the same way
> like in my example above? Or is there a good reason why this is not the
> case?
> >
> > Guillaume
> >
> > --
> > Guillaume Laforge
> > Apache Groovy committer
> > Developer Advocate @ Google Cloud
> >
> > Blog: glaforge.dev
> > Twitter: @glaforge
> > Mastodon: @glaforge@uwyn.net
>


-- 
*Guillaume Laforge*
Apache Groovy committer
Developer Advocate @ Google Cloud <https://cloud.google.com/>

   - Blog: glaforge.dev
   - Twitter: @glaforge <http://twitter.com/glaforge>
   - Mastodon: @glaforge@uwyn.net <http://%40glaforge@uwyn.net/>

Re: Records support in JSON

Posted by Paul King <pa...@asert.com.au>.
We aren't picking up the metabean properties for the record correctly.
Would you like to file a bug report?

Workarounds are to call "toMap()" (or *.toMap() on the list), or do
something like:
```
@AutoImplement
record Person(String name) implements Map {
  Set entrySet() { toMap().entrySet() }
}
```


<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Tue, Sep 5, 2023 at 2:13 AM Guillaume Laforge <gl...@gmail.com> wrote:
>
> Hi,
>
> I was a bit surprised by this behavior with serializing records in JSON.
>
> I was expecting this assertion to pass:
>
> import groovy.json.JsonOutput
> record Person(String name) {}
> assert JsonOutput.toJson([new Person('Guillaume')]) == '[{"name":"Guillaume"}]'
>
> Instead, I got [{}]
>
> Shouldn't records be treated like POGOs and thus serialize the same way like in my example above? Or is there a good reason why this is not the case?
>
> Guillaume
>
> --
> Guillaume Laforge
> Apache Groovy committer
> Developer Advocate @ Google Cloud
>
> Blog: glaforge.dev
> Twitter: @glaforge
> Mastodon: @glaforge@uwyn.net