You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by JCR <jc...@proxymit.net> on 2020/05/11 08:30:56 UTC

Resource to json serializing

Before I write anything on my own...

Hello

There is this convenient .json extension which produces a JSON 
serialization of any given resource in Sling.

What would be a "best practice" if I only wanted a subset of all 
properties to be serialized on the json stream? That is, I would like to 
provide a "positive" list (lists all props to be exported) or a 
"negative" one (props to be excluded).

Example: I have props a, b and c on a given node/resource and only a and 
c should appear on the output.

Thanks for any pattern hint
Juerg


Re: Resource to json serializing

Posted by Steven Walters <ke...@gmail.com>.
>
> >... First question being, which JSON library do we support? As we settled on
> > johnzon, I assume thats the one...
>
> It looks like the Johnzon Mapper module [2] should support what I need
> but I haven't found real documentation about it so far. I'll have a
> look a might switch the graphql-core to it if I like what I see.

though Johnzon Mapper can handle generic data types like deeply nested maps,
such as what is returned by graphql-java, but the controls here are
rather limited.
Most of the functionality is tuned to well-defined POJOs and not something
generic such as a sling Resource or JCR Node.
I've been using Mapper by itself and combined with graphql-java for 3+ years,
filing bugs and PRs on Mapper along the way.
For graphql-java + any JSON serializer, it simplifies down
to null, Map, and GraphQLScalarType serializations.

If one would want to use Mapper within Sling generically, this would
likely entail
having a org.apache.johnzon.mapper.ObjectConverter.Writer implementation to
handle serializing Resources as JSON properly.
This Writer implementation would then handle the control as to *what
to write* on a
Resource, such as which properties and which resource/node children, leaving the
*how to write* to the Mapper configuration.

From a rather quick attempt at seeing if such a Writer exists, I did
not find anything
but instead found many direct uses of JSON-P instead.
Whether this is a benefit or nuisance lies in how much of the JSON-P logic is
shared or duplicated. I've not gone deep enough to have any foundation for
evaluating sling here.

Utilizing Mapper would allow simplify configuring how to serialize
property values
types (e.g. java.time types as ISO 8601) by registering Adapters or
Writers to serialize
those types.
The configuration could then be shared by all components within the
system needing
to serialize (deserialize) and then possibly allow for one-time
overrides by accepting an optional
Mapper instance when serializing.

Re: Resource to json serializing

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Thu, May 14, 2020 at 9:25 AM Carsten Ziegeler <cz...@apache.org> wrote:
>
> I think it makes sense to discuss API options first, to avoid going back
> and forth. ..

Agreed.

FWIW for the graphql-core module that I'm working on I need to convert
arbitrary objects to and from JSON [1], I suppose that's part of what
we need here.

So far I haven't designed an API for that, just mimicked
https://github.com/jdereg/json-io which I'm using for now, as it has
minimal requirements and is simple to use.

>... First question being, which JSON library do we support? As we settled on
> johnzon, I assume thats the one...

It looks like the Johnzon Mapper module [2] should support what I need
but I haven't found real documentation about it so far. I'll have a
look a might switch the graphql-core to it if I like what I see.

-Bertrand

[1] https://github.com/apache/sling-org-apache-sling-graphql-core/blob/master/src/main/java/org/apache/sling/graphql/core/json/JsonSerializer.java
[2] http://johnzon.apache.org/apidocs/org/apache/johnzon/mapper/package-summary.html

Re: Resource to json serializing

Posted by jcr <jc...@proxymit.net>.
On 14.05.20 09:25, Carsten Ziegeler wrote:.

>
> What's the input of the api? A resource? A value map? Something else?
> What's the output? A string? A Json Object ?
>
> What manipulation operations are supported? Filtering of properties?
>
> Depending on what exactly the api does, we can then argue of where to 
> put it.

Carsten, thanks for picking this up.

 From the Sling HP: "Apache Sling™ is a framework for RESTful 
web-applications..."

The key secret sauce of Sling has always been its approach to REST. Now, 
I'd like to stress the 'RE' in the acronym.
To make Sling really the framework of choice for any type of web app, I 
expected it to offer real simple generation of representations of all 
sorts, for Web (html), SPAs & Co (Json), semantic web, CMIS (XML/RDF), 
etc. This ideally directly based on the Resource as state.

While Sling is very far progressed in this for HTML (e.g. sighlty/HTL), 
the other representations somehow lack behind though they are equally 
important, especially for machine-to-machine comms.

So, my idea for the general purpose: have a flexible, extensible 
rendition engine which could potentially be used for any format. But 
that's probably something for Sling 16...
For the JSON part and this moment, I'd be absolutely happy with the 
JsonItemWriter API as found in Sling 8 and already discussed in this 
mail thread 
(https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html).

JsonItemWriter includes:

- direct conversion of Nodes/Resources to a JSON string (REST!)
- allows the control of recursion levels
- allows to set a negative list of properties (those not to render)
- let's one choose the date format
- (hopefully) renders multi-value props as a Json array

I think I will raise the issue in Jira going in that direction.

-Juerg

>
>
> On 13.05.2020 14:05, Robert Munteanu wrote:
>> On Wed, 2020-05-13 at 11:45 +0200, JCR wrote:
>>> And the invoice goes... to the ASF?
>>
>> If you're looking for someone to invoice I don't think anyone here can
>> help :-)
>>
>>>
>>> Where can I raise the issue? I don't think I have write access to the
>>> Sling Jira.
>>
>> https://issues.apache.org/jira/browse/SLING
>>
>> Once registered you can definitely create issues.
>>
>>> And what would be the best approach? Re-activate the
>>> org.apache.sling.commons.json package?
>>
>> That package was retired for legal reasons, so it can't be reactivated.
>> More details are at
>>
>>    https://issues.apache.org/jira/browse/SLING-6536
>>
>> However, it's on Maven Central, and you can deploy it to your instance
>> if you think it's useful and you judget that the licensing terms are
>> acceptable to you. However, it is not supported by us.
>>
>> https://search.maven.org/search?q=a:org.apache.sling.commons.json
>>
>> The approach would be to expose a new API to allows consumers to render
>> objects as JSON (and probably other formats) in the format used by the
>> DefaultGetServlet and also filter the properties that are rendered.
>>
>> Thanks,
>> Robert
>>
>>>
>>> Thanks, Juerg
>>>
>>> On 12.05.2020 17:47, Robert Munteanu wrote:
>>>> On Tue, 2020-05-12 at 16:09 +0200, JCR wrote:
>>>>> Thanks Daniel, John,
>>>>>
>>>>> As the app makes high usage of dynamic JCR Node features (based
>>>>> on
>>>>> nt:unstructured, mixins, etc), a relatively static sling model
>>>>> class
>>>>> seems a bit unpractical, though a typesafe contract would be
>>>>> desirable,
>>>>> of course.
>>>>>
>>>>> On the other hand, the JsonItemWriter would almost be ideal but
>>>>> is
>>>>> not
>>>>> present in Sling 11 anymore. Actually, the entire commons.json
>>>>> package
>>>>> disappeared. Seemingly, this package has been replaced by Apache
>>>>> Johnzon
>>>>> but I cannot find anything equivalent to JsonItemWriter.
>>>>>
>>>>> Anybody aware of its substitute?
>>>> FWIW, the rendering in the DefaultGetServlet is not accessible to
>>>> the
>>>> outside world [1]. I could imagine that we could open on the
>>>> default
>>>> renderers in ways that do not break encapsulation.
>>>>
>>>> Feel free to raise an issue if that's interesting to you.
>>>> Obviously, a
>>>> PR would be even better :-)
>>>>
>>>> Thanks,
>>>> Robert
>>>>
>>>>> Thanks,
>>>>> Juerg
>>>>>
>>>>>
>>>>> On 11.05.20 16:39, John Kramer wrote:
>>>>>> You could also use a JsonItemWriter and write your own servlet
>>>>>> if
>>>>>> you want to do it that way:
>>>>>>
>>>>>> https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html 
>>>>>>
>>>>>>
>>>>>>
>>>>>> John Kramer
>>>>>> Manager, AEM Engineer
>>>>>> E-Commerce Web Team
>>>>>> e: john.kramer@panerabread.com <ma...@panera.com>
>>>>>> m: 314-435-2370
>>>>>>
>>>>>> On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
>>>>>>
>>>>>>        Warning:  This email originated from outside of Panera.
>>>>>> Beware
>>>>>> of clicking links and attachments.
>>>>>>                       Sounds like a use case for Sling Models 
>>>>>> Exporter:
>>>>>> https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
>>>>>> https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
>>>>>>               The only caveat is that it's designed for named
>>>>>> parameters, if
>>>>>> you really
>>>>>>        need it to be completely configurable at runtime, I'd
>>>>>> suggest
>>>>>> creating a
>>>>>>        servlet attached to a resource type that filters the
>>>>>> current
>>>>>> resources
>>>>>>        value map and uses Jackson to convert it to a JSON.
>>>>>>               On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net>
>>>>>> wrote:
>>>>>>               > Before I write anything on my own...
>>>>>>        >
>>>>>>        > Hello
>>>>>>        >
>>>>>>        > There is this convenient .json extension which produces
>>>>>> a
>>>>>> JSON
>>>>>>        > serialization of any given resource in Sling.
>>>>>>        >
>>>>>>        > What would be a "best practice" if I only wanted a
>>>>>> subset of
>>>>>> all
>>>>>>        > properties to be serialized on the json stream? That
>>>>>> is, I
>>>>>> would like to
>>>>>>        > provide a "positive" list (lists all props to be
>>>>>> exported)
>>>>>> or a
>>>>>>        > "negative" one (props to be excluded).
>>>>>>        >
>>>>>>        > Example: I have props a, b and c on a given
>>>>>> node/resource
>>>>>> and only a and
>>>>>>        > c should appear on the output.
>>>>>>        >
>>>>>>        > Thanks for any pattern hint
>>>>>>        > Juerg
>>>>>>        >
>>>>>>        >
>>>>>>
>>>> [1]:
>>>> https://github.com/apache/sling-org-apache-sling-servlets-get/blob/master/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRenderer.java 
>>>>
>>>>
>>
>

Re: Resource to json serializing

Posted by Robert Munteanu <ro...@apache.org>.
On Thu, 2020-05-14 at 09:25 +0200, Carsten Ziegeler wrote:
> I think it makes sense to discuss API options first, to avoid going
> back 
> 
> and forth.

+1. We can do this as a Jira, or via email, but we definitely need to
properly define this, as we're talking about exposing API.

Thanks,
Robert


Re: Resource to json serializing

Posted by Carsten Ziegeler <cz...@apache.org>.
I think it makes sense to discuss API options first, to avoid going back 
and forth.

First question being, which JSON library do we support? As we settled on 
johnzon, I assume thats the one.

What's the input of the api? A resource? A value map? Something else?
What's the output? A string? A Json Object ?

What manipulation operations are supported? Filtering of properties?

Depending on what exactly the api does, we can then argue of where to 
put it.

Just some thoughts.

Regards
Carsten

On 13.05.2020 14:05, Robert Munteanu wrote:
> On Wed, 2020-05-13 at 11:45 +0200, JCR wrote:
>> And the invoice goes... to the ASF?
> 
> If you're looking for someone to invoice I don't think anyone here can
> help :-)
> 
>>
>> Where can I raise the issue? I don't think I have write access to the
>> Sling Jira.
> 
> https://issues.apache.org/jira/browse/SLING
> 
> Once registered you can definitely create issues.
> 
>> And what would be the best approach? Re-activate the
>> org.apache.sling.commons.json package?
> 
> That package was retired for legal reasons, so it can't be reactivated.
> More details are at
> 
>    https://issues.apache.org/jira/browse/SLING-6536
> 
> However, it's on Maven Central, and you can deploy it to your instance
> if you think it's useful and you judget that the licensing terms are
> acceptable to you. However, it is not supported by us.
> 
>    https://search.maven.org/search?q=a:org.apache.sling.commons.json
> 
> The approach would be to expose a new API to allows consumers to render
> objects as JSON (and probably other formats) in the format used by the
> DefaultGetServlet and also filter the properties that are rendered.
> 
> Thanks,
> Robert
> 
>>
>> Thanks, Juerg
>>
>> On 12.05.2020 17:47, Robert Munteanu wrote:
>>> On Tue, 2020-05-12 at 16:09 +0200, JCR wrote:
>>>> Thanks Daniel, John,
>>>>
>>>> As the app makes high usage of dynamic JCR Node features (based
>>>> on
>>>> nt:unstructured, mixins, etc), a relatively static sling model
>>>> class
>>>> seems a bit unpractical, though a typesafe contract would be
>>>> desirable,
>>>> of course.
>>>>
>>>> On the other hand, the JsonItemWriter would almost be ideal but
>>>> is
>>>> not
>>>> present in Sling 11 anymore. Actually, the entire commons.json
>>>> package
>>>> disappeared. Seemingly, this package has been replaced by Apache
>>>> Johnzon
>>>> but I cannot find anything equivalent to JsonItemWriter.
>>>>
>>>> Anybody aware of its substitute?
>>> FWIW, the rendering in the DefaultGetServlet is not accessible to
>>> the
>>> outside world [1]. I could imagine that we could open on the
>>> default
>>> renderers in ways that do not break encapsulation.
>>>
>>> Feel free to raise an issue if that's interesting to you.
>>> Obviously, a
>>> PR would be even better :-)
>>>
>>> Thanks,
>>> Robert
>>>
>>>> Thanks,
>>>> Juerg
>>>>
>>>>
>>>> On 11.05.20 16:39, John Kramer wrote:
>>>>> You could also use a JsonItemWriter and write your own servlet
>>>>> if
>>>>> you want to do it that way:
>>>>>
>>>>> https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
>>>>>
>>>>>
>>>>> John Kramer
>>>>> Manager, AEM Engineer
>>>>> E-Commerce Web Team
>>>>> e: john.kramer@panerabread.com <ma...@panera.com>
>>>>> m: 314-435-2370
>>>>>
>>>>> On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
>>>>>
>>>>>        Warning:  This email originated from outside of Panera.
>>>>> Beware
>>>>> of clicking links and attachments.
>>>>>         
>>>>>        
>>>>>        Sounds like a use case for Sling Models Exporter:
>>>>>        
>>>>>        
>>>>> https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
>>>>>        
>>>>>        
>>>>> https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
>>>>>        
>>>>>        The only caveat is that it's designed for named
>>>>> parameters, if
>>>>> you really
>>>>>        need it to be completely configurable at runtime, I'd
>>>>> suggest
>>>>> creating a
>>>>>        servlet attached to a resource type that filters the
>>>>> current
>>>>> resources
>>>>>        value map and uses Jackson to convert it to a JSON.
>>>>>        
>>>>>        On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net>
>>>>> wrote:
>>>>>        
>>>>>        > Before I write anything on my own...
>>>>>        >
>>>>>        > Hello
>>>>>        >
>>>>>        > There is this convenient .json extension which produces
>>>>> a
>>>>> JSON
>>>>>        > serialization of any given resource in Sling.
>>>>>        >
>>>>>        > What would be a "best practice" if I only wanted a
>>>>> subset of
>>>>> all
>>>>>        > properties to be serialized on the json stream? That
>>>>> is, I
>>>>> would like to
>>>>>        > provide a "positive" list (lists all props to be
>>>>> exported)
>>>>> or a
>>>>>        > "negative" one (props to be excluded).
>>>>>        >
>>>>>        > Example: I have props a, b and c on a given
>>>>> node/resource
>>>>> and only a and
>>>>>        > c should appear on the output.
>>>>>        >
>>>>>        > Thanks for any pattern hint
>>>>>        > Juerg
>>>>>        >
>>>>>        >
>>>>>        
>>>>>
>>> [1]:
>>> https://github.com/apache/sling-org-apache-sling-servlets-get/blob/master/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRenderer.java
>>>
> 

-- 
--
Carsten Ziegeler
Adobe Research Switzerland
cziegeler@apache.org

Re: Resource to json serializing

Posted by Robert Munteanu <ro...@apache.org>.
On Wed, 2020-05-13 at 11:45 +0200, JCR wrote:
> And the invoice goes... to the ASF?

If you're looking for someone to invoice I don't think anyone here can
help :-)

> 
> Where can I raise the issue? I don't think I have write access to the
> Sling Jira.

https://issues.apache.org/jira/browse/SLING

Once registered you can definitely create issues.

> And what would be the best approach? Re-activate the
> org.apache.sling.commons.json package?

That package was retired for legal reasons, so it can't be reactivated.
More details are at 

  https://issues.apache.org/jira/browse/SLING-6536

However, it's on Maven Central, and you can deploy it to your instance
if you think it's useful and you judget that the licensing terms are
acceptable to you. However, it is not supported by us.

  https://search.maven.org/search?q=a:org.apache.sling.commons.json

The approach would be to expose a new API to allows consumers to render
objects as JSON (and probably other formats) in the format used by the
DefaultGetServlet and also filter the properties that are rendered.

Thanks,
Robert

> 
> Thanks, Juerg
> 
> On 12.05.2020 17:47, Robert Munteanu wrote:
> > On Tue, 2020-05-12 at 16:09 +0200, JCR wrote:
> > > Thanks Daniel, John,
> > > 
> > > As the app makes high usage of dynamic JCR Node features (based
> > > on
> > > nt:unstructured, mixins, etc), a relatively static sling model
> > > class
> > > seems a bit unpractical, though a typesafe contract would be
> > > desirable,
> > > of course.
> > > 
> > > On the other hand, the JsonItemWriter would almost be ideal but
> > > is
> > > not
> > > present in Sling 11 anymore. Actually, the entire commons.json
> > > package
> > > disappeared. Seemingly, this package has been replaced by Apache
> > > Johnzon
> > > but I cannot find anything equivalent to JsonItemWriter.
> > > 
> > > Anybody aware of its substitute?
> > FWIW, the rendering in the DefaultGetServlet is not accessible to
> > the
> > outside world [1]. I could imagine that we could open on the
> > default
> > renderers in ways that do not break encapsulation.
> > 
> > Feel free to raise an issue if that's interesting to you.
> > Obviously, a
> > PR would be even better :-)
> > 
> > Thanks,
> > Robert
> > 
> > > Thanks,
> > > Juerg
> > > 
> > > 
> > > On 11.05.20 16:39, John Kramer wrote:
> > > > You could also use a JsonItemWriter and write your own servlet
> > > > if
> > > > you want to do it that way:
> > > > 
> > > > https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
> > > > 
> > > > 
> > > > John Kramer
> > > > Manager, AEM Engineer
> > > > E-Commerce Web Team
> > > > e: john.kramer@panerabread.com <ma...@panera.com>
> > > > m: 314-435-2370
> > > > 
> > > > On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
> > > > 
> > > >       Warning:  This email originated from outside of Panera.
> > > > Beware
> > > > of clicking links and attachments.
> > > >        
> > > >       
> > > >       Sounds like a use case for Sling Models Exporter:
> > > >       
> > > >       
> > > > https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
> > > >       
> > > >       
> > > > https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
> > > >       
> > > >       The only caveat is that it's designed for named
> > > > parameters, if
> > > > you really
> > > >       need it to be completely configurable at runtime, I'd
> > > > suggest
> > > > creating a
> > > >       servlet attached to a resource type that filters the
> > > > current
> > > > resources
> > > >       value map and uses Jackson to convert it to a JSON.
> > > >       
> > > >       On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net>
> > > > wrote:
> > > >       
> > > >       > Before I write anything on my own...
> > > >       >
> > > >       > Hello
> > > >       >
> > > >       > There is this convenient .json extension which produces
> > > > a
> > > > JSON
> > > >       > serialization of any given resource in Sling.
> > > >       >
> > > >       > What would be a "best practice" if I only wanted a
> > > > subset of
> > > > all
> > > >       > properties to be serialized on the json stream? That
> > > > is, I
> > > > would like to
> > > >       > provide a "positive" list (lists all props to be
> > > > exported)
> > > > or a
> > > >       > "negative" one (props to be excluded).
> > > >       >
> > > >       > Example: I have props a, b and c on a given
> > > > node/resource
> > > > and only a and
> > > >       > c should appear on the output.
> > > >       >
> > > >       > Thanks for any pattern hint
> > > >       > Juerg
> > > >       >
> > > >       >
> > > >       
> > > > 
> > [1]: 
> > https://github.com/apache/sling-org-apache-sling-servlets-get/blob/master/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRenderer.java
> > 


Re: Resource to json serializing

Posted by JCR <jc...@proxymit.net>.
And the invoice goes... to the ASF?

Where can I raise the issue? I don't think I have write access to the Sling Jira.
And what would be the best approach? Re-activate the org.apache.sling.commons.json package?

Thanks, Juerg

On 12.05.2020 17:47, Robert Munteanu wrote:
> On Tue, 2020-05-12 at 16:09 +0200, JCR wrote:
>> Thanks Daniel, John,
>>
>> As the app makes high usage of dynamic JCR Node features (based on
>> nt:unstructured, mixins, etc), a relatively static sling model class
>> seems a bit unpractical, though a typesafe contract would be
>> desirable,
>> of course.
>>
>> On the other hand, the JsonItemWriter would almost be ideal but is
>> not
>> present in Sling 11 anymore. Actually, the entire commons.json
>> package
>> disappeared. Seemingly, this package has been replaced by Apache
>> Johnzon
>> but I cannot find anything equivalent to JsonItemWriter.
>>
>> Anybody aware of its substitute?
> FWIW, the rendering in the DefaultGetServlet is not accessible to the
> outside world [1]. I could imagine that we could open on the default
> renderers in ways that do not break encapsulation.
>
> Feel free to raise an issue if that's interesting to you. Obviously, a
> PR would be even better :-)
>
> Thanks,
> Robert
>
>> Thanks,
>> Juerg
>>
>>
>> On 11.05.20 16:39, John Kramer wrote:
>>> You could also use a JsonItemWriter and write your own servlet if
>>> you want to do it that way:
>>>
>>> https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
>>>
>>>
>>> John Kramer
>>> Manager, AEM Engineer
>>> E-Commerce Web Team
>>> e: john.kramer@panerabread.com <ma...@panera.com>
>>> m: 314-435-2370
>>>
>>> On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
>>>
>>>       Warning:  This email originated from outside of Panera. Beware
>>> of clicking links and attachments.
>>>        
>>>       
>>>       Sounds like a use case for Sling Models Exporter:
>>>       
>>>       
>>> https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
>>>       
>>>       
>>> https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
>>>       
>>>       The only caveat is that it's designed for named parameters, if
>>> you really
>>>       need it to be completely configurable at runtime, I'd suggest
>>> creating a
>>>       servlet attached to a resource type that filters the current
>>> resources
>>>       value map and uses Jackson to convert it to a JSON.
>>>       
>>>       On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
>>>       
>>>       > Before I write anything on my own...
>>>       >
>>>       > Hello
>>>       >
>>>       > There is this convenient .json extension which produces a
>>> JSON
>>>       > serialization of any given resource in Sling.
>>>       >
>>>       > What would be a "best practice" if I only wanted a subset of
>>> all
>>>       > properties to be serialized on the json stream? That is, I
>>> would like to
>>>       > provide a "positive" list (lists all props to be exported)
>>> or a
>>>       > "negative" one (props to be excluded).
>>>       >
>>>       > Example: I have props a, b and c on a given node/resource
>>> and only a and
>>>       > c should appear on the output.
>>>       >
>>>       > Thanks for any pattern hint
>>>       > Juerg
>>>       >
>>>       >
>>>       
>>>
> [1]: https://github.com/apache/sling-org-apache-sling-servlets-get/blob/master/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRenderer.java
>


Re: Resource to json serializing

Posted by Robert Munteanu <ro...@apache.org>.
On Tue, 2020-05-12 at 16:09 +0200, JCR wrote:
> Thanks Daniel, John,
> 
> As the app makes high usage of dynamic JCR Node features (based on 
> nt:unstructured, mixins, etc), a relatively static sling model class 
> seems a bit unpractical, though a typesafe contract would be
> desirable, 
> of course.
> 
> On the other hand, the JsonItemWriter would almost be ideal but is
> not 
> present in Sling 11 anymore. Actually, the entire commons.json
> package 
> disappeared. Seemingly, this package has been replaced by Apache
> Johnzon 
> but I cannot find anything equivalent to JsonItemWriter.
> 
> Anybody aware of its substitute?

FWIW, the rendering in the DefaultGetServlet is not accessible to the
outside world [1]. I could imagine that we could open on the default
renderers in ways that do not break encapsulation.

Feel free to raise an issue if that's interesting to you. Obviously, a
PR would be even better :-)

Thanks,
Robert

> 
> Thanks,
> Juerg
> 
> 
> On 11.05.20 16:39, John Kramer wrote:
> > You could also use a JsonItemWriter and write your own servlet if
> > you want to do it that way:
> > 
> > https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
> > 
> > 
> > John Kramer
> > Manager, AEM Engineer
> > E-Commerce Web Team
> > e: john.kramer@panerabread.com <ma...@panera.com>
> > m: 314-435-2370
> > 
> > On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
> > 
> >      Warning:  This email originated from outside of Panera. Beware
> > of clicking links and attachments.
> >       
> >      
> >      Sounds like a use case for Sling Models Exporter:
> >      
> >      
> > https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
> >      
> >      
> > https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
> >      
> >      The only caveat is that it's designed for named parameters, if
> > you really
> >      need it to be completely configurable at runtime, I'd suggest
> > creating a
> >      servlet attached to a resource type that filters the current
> > resources
> >      value map and uses Jackson to convert it to a JSON.
> >      
> >      On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
> >      
> >      > Before I write anything on my own...
> >      >
> >      > Hello
> >      >
> >      > There is this convenient .json extension which produces a
> > JSON
> >      > serialization of any given resource in Sling.
> >      >
> >      > What would be a "best practice" if I only wanted a subset of
> > all
> >      > properties to be serialized on the json stream? That is, I
> > would like to
> >      > provide a "positive" list (lists all props to be exported)
> > or a
> >      > "negative" one (props to be excluded).
> >      >
> >      > Example: I have props a, b and c on a given node/resource
> > and only a and
> >      > c should appear on the output.
> >      >
> >      > Thanks for any pattern hint
> >      > Juerg
> >      >
> >      >
> >      
> > 

[1]: https://github.com/apache/sling-org-apache-sling-servlets-get/blob/master/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRenderer.java


Re: Resource to json serializing

Posted by JCR <jc...@proxymit.net>.
Hi John

Yes, that's a problem.
Even in the Sling "open source space", many examples/code excerpts include references to proprietary Adobe code, not 
available to "pure" Sling projects...

-Juerg

On 12.05.2020 17:50, John Kramer wrote:
> Hi Juerg,
>
> I am using Apache sling as part of AEM. Adobe packages its own very similar class com.day.cq.commons.TidyJsonItemWriter. Of course that doesn't help if you aren't using AEM.
>
> John Kramer
> Manager, AEM Engineer
> E-Commerce Web Team
> e: john.kramer@panerabread.com <ma...@panera.com>
> m: 314-435-2370
>
> On 5/12/20, 09:10, "JCR" <jc...@proxymit.net> wrote:
>
>      Thanks Daniel, John,
>      
>      As the app makes high usage of dynamic JCR Node features (based on
>      nt:unstructured, mixins, etc), a relatively static sling model class
>      seems a bit unpractical, though a typesafe contract would be desirable,
>      of course.
>      
>      On the other hand, the JsonItemWriter would almost be ideal but is not
>      present in Sling 11 anymore. Actually, the entire commons.json package
>      disappeared. Seemingly, this package has been replaced by Apache Johnzon
>      but I cannot find anything equivalent to JsonItemWriter.
>      
>      Anybody aware of its substitute?
>      
>      Thanks,
>      Juerg
>      
>      
>      On 11.05.20 16:39, John Kramer wrote:
>      > You could also use a JsonItemWriter and write your own servlet if you want to do it that way:
>      >
>      > https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
>      >
>      >
>      > John Kramer
>      > Manager, AEM Engineer
>      > E-Commerce Web Team
>      > e: john.kramer@panerabread.com <ma...@panera.com>
>      > m: 314-435-2370
>      >
>      > On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
>      >
>      >      Warning:  This email originated from outside of Panera. Beware of clicking links and attachments.
>      >
>      >
>      >      Sounds like a use case for Sling Models Exporter:
>      >
>      >      https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
>      >
>      >      https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
>      >
>      >      The only caveat is that it's designed for named parameters, if you really
>      >      need it to be completely configurable at runtime, I'd suggest creating a
>      >      servlet attached to a resource type that filters the current resources
>      >      value map and uses Jackson to convert it to a JSON.
>      >
>      >      On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
>      >
>      >      > Before I write anything on my own...
>      >      >
>      >      > Hello
>      >      >
>      >      > There is this convenient .json extension which produces a JSON
>      >      > serialization of any given resource in Sling.
>      >      >
>      >      > What would be a "best practice" if I only wanted a subset of all
>      >      > properties to be serialized on the json stream? That is, I would like to
>      >      > provide a "positive" list (lists all props to be exported) or a
>      >      > "negative" one (props to be excluded).
>      >      >
>      >      > Example: I have props a, b and c on a given node/resource and only a and
>      >      > c should appear on the output.
>      >      >
>      >      > Thanks for any pattern hint
>      >      > Juerg
>      >      >
>      >      >
>      >
>      >
>      
>      
>


Re: Resource to json serializing

Posted by John Kramer <Jo...@panerabread.com>.
Hi Juerg,

I am using Apache sling as part of AEM. Adobe packages its own very similar class com.day.cq.commons.TidyJsonItemWriter. Of course that doesn't help if you aren't using AEM.

John Kramer 
Manager, AEM Engineer
E-Commerce Web Team
e: john.kramer@panerabread.com <ma...@panera.com>
m: 314-435-2370

On 5/12/20, 09:10, "JCR" <jc...@proxymit.net> wrote:

    Thanks Daniel, John,
    
    As the app makes high usage of dynamic JCR Node features (based on 
    nt:unstructured, mixins, etc), a relatively static sling model class 
    seems a bit unpractical, though a typesafe contract would be desirable, 
    of course.
    
    On the other hand, the JsonItemWriter would almost be ideal but is not 
    present in Sling 11 anymore. Actually, the entire commons.json package 
    disappeared. Seemingly, this package has been replaced by Apache Johnzon 
    but I cannot find anything equivalent to JsonItemWriter.
    
    Anybody aware of its substitute?
    
    Thanks,
    Juerg
    
    
    On 11.05.20 16:39, John Kramer wrote:
    > You could also use a JsonItemWriter and write your own servlet if you want to do it that way:
    >
    > https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
    >
    >
    > John Kramer
    > Manager, AEM Engineer
    > E-Commerce Web Team
    > e: john.kramer@panerabread.com <ma...@panera.com>
    > m: 314-435-2370
    >
    > On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
    >
    >      Warning:  This email originated from outside of Panera. Beware of clicking links and attachments.
    >       
    >      
    >      Sounds like a use case for Sling Models Exporter:
    >      
    >      https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
    >      
    >      https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
    >      
    >      The only caveat is that it's designed for named parameters, if you really
    >      need it to be completely configurable at runtime, I'd suggest creating a
    >      servlet attached to a resource type that filters the current resources
    >      value map and uses Jackson to convert it to a JSON.
    >      
    >      On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
    >      
    >      > Before I write anything on my own...
    >      >
    >      > Hello
    >      >
    >      > There is this convenient .json extension which produces a JSON
    >      > serialization of any given resource in Sling.
    >      >
    >      > What would be a "best practice" if I only wanted a subset of all
    >      > properties to be serialized on the json stream? That is, I would like to
    >      > provide a "positive" list (lists all props to be exported) or a
    >      > "negative" one (props to be excluded).
    >      >
    >      > Example: I have props a, b and c on a given node/resource and only a and
    >      > c should appear on the output.
    >      >
    >      > Thanks for any pattern hint
    >      > Juerg
    >      >
    >      >
    >      
    >
    
    


Re: Resource to json serializing

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Tue, May 12, 2020 at 4:10 PM JCR <jc...@proxymit.net> wrote:
> ...the entire commons.json package
> disappeared. Seemingly, this package has been replaced by Apache Johnzon
> but I cannot find anything equivalent to JsonItemWriter.
>
> Anybody aware of its substitute?...

I'm not sure if we have one, and happy to be proven wrong if that's the case.

For the new GraphQL module [1] I've been using json-io [2] which looks
good so far but I haven't done a lot with it at this point.

-Bertrand

[1] https://github.com/apache/sling-org-apache-sling-graphql-core
[2] https://github.com/jdereg/json-io

Re: Resource to json serializing

Posted by JCR <jc...@proxymit.net>.
Thanks Daniel, John,

As the app makes high usage of dynamic JCR Node features (based on 
nt:unstructured, mixins, etc), a relatively static sling model class 
seems a bit unpractical, though a typesafe contract would be desirable, 
of course.

On the other hand, the JsonItemWriter would almost be ideal but is not 
present in Sling 11 anymore. Actually, the entire commons.json package 
disappeared. Seemingly, this package has been replaced by Apache Johnzon 
but I cannot find anything equivalent to JsonItemWriter.

Anybody aware of its substitute?

Thanks,
Juerg


On 11.05.20 16:39, John Kramer wrote:
> You could also use a JsonItemWriter and write your own servlet if you want to do it that way:
>
> https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html
>
>
> John Kramer
> Manager, AEM Engineer
> E-Commerce Web Team
> e: john.kramer@panerabread.com <ma...@panera.com>
> m: 314-435-2370
>
> On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:
>
>      Warning:  This email originated from outside of Panera. Beware of clicking links and attachments.
>       
>      
>      Sounds like a use case for Sling Models Exporter:
>      
>      https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
>      
>      https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
>      
>      The only caveat is that it's designed for named parameters, if you really
>      need it to be completely configurable at runtime, I'd suggest creating a
>      servlet attached to a resource type that filters the current resources
>      value map and uses Jackson to convert it to a JSON.
>      
>      On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
>      
>      > Before I write anything on my own...
>      >
>      > Hello
>      >
>      > There is this convenient .json extension which produces a JSON
>      > serialization of any given resource in Sling.
>      >
>      > What would be a "best practice" if I only wanted a subset of all
>      > properties to be serialized on the json stream? That is, I would like to
>      > provide a "positive" list (lists all props to be exported) or a
>      > "negative" one (props to be excluded).
>      >
>      > Example: I have props a, b and c on a given node/resource and only a and
>      > c should appear on the output.
>      >
>      > Thanks for any pattern hint
>      > Juerg
>      >
>      >
>      
>


Re: Resource to json serializing

Posted by John Kramer <Jo...@panerabread.com>.
You could also use a JsonItemWriter and write your own servlet if you want to do it that way:

https://sling.apache.org/apidocs/sling8/org/apache/sling/commons/json/jcr/JsonItemWriter.html


John Kramer 
Manager, AEM Engineer
E-Commerce Web Team
e: john.kramer@panerabread.com <ma...@panera.com>
m: 314-435-2370

On 5/11/20, 08:54, "Daniel Klco" <dk...@apache.org> wrote:

    Warning:  This email originated from outside of Panera. Beware of clicking links and attachments.
     
    
    Sounds like a use case for Sling Models Exporter:
    
    https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
    
    https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
    
    The only caveat is that it's designed for named parameters, if you really
    need it to be completely configurable at runtime, I'd suggest creating a
    servlet attached to a resource type that filters the current resources
    value map and uses Jackson to convert it to a JSON.
    
    On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
    
    > Before I write anything on my own...
    >
    > Hello
    >
    > There is this convenient .json extension which produces a JSON
    > serialization of any given resource in Sling.
    >
    > What would be a "best practice" if I only wanted a subset of all
    > properties to be serialized on the json stream? That is, I would like to
    > provide a "positive" list (lists all props to be exported) or a
    > "negative" one (props to be excluded).
    >
    > Example: I have props a, b and c on a given node/resource and only a and
    > c should appear on the output.
    >
    > Thanks for any pattern hint
    > Juerg
    >
    >
    


Re: Resource to json serializing

Posted by Ruben Reusser <rr...@headwire.com>.
hey, sorry, super late to the party here but this can be solved with 
sling models:

sling models have a @JsonAnyGetter method - for a reference, see the 
implementation of this in the peregrine-cms land:

https://github.com/headwirecom/peregrine-cms/blob/develop/platform/base/core/src/main/java/com/peregrine/nodetypes/models/NtUnstructuredModel.java
https://github.com/headwirecom/peregrine-cms/blob/develop/platform/base/core/src/main/java/com/peregrine/nodetypes/models/Container.java

Ruben

On 5/11/2020 6:54 AM, Daniel Klco wrote:
> Sounds like a use case for Sling Models Exporter:
>
> https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1
>
> https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
>
> The only caveat is that it's designed for named parameters, if you really
> need it to be completely configurable at runtime, I'd suggest creating a
> servlet attached to a resource type that filters the current resources
> value map and uses Jackson to convert it to a JSON.
>
> On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:
>
>> Before I write anything on my own...
>>
>> Hello
>>
>> There is this convenient .json extension which produces a JSON
>> serialization of any given resource in Sling.
>>
>> What would be a "best practice" if I only wanted a subset of all
>> properties to be serialized on the json stream? That is, I would like to
>> provide a "positive" list (lists all props to be exported) or a
>> "negative" one (props to be excluded).
>>
>> Example: I have props a, b and c on a given node/resource and only a and
>> c should appear on the output.
>>
>> Thanks for any pattern hint
>> Juerg
>>
>>

Re: Resource to json serializing

Posted by Daniel Klco <dk...@apache.org>.
Sounds like a use case for Sling Models Exporter:

https://sling.apache.org/documentation/bundles/models.html#exporter-framework-since-130-1

https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/

The only caveat is that it's designed for named parameters, if you really
need it to be completely configurable at runtime, I'd suggest creating a
servlet attached to a resource type that filters the current resources
value map and uses Jackson to convert it to a JSON.

On Mon, May 11, 2020 at 4:31 AM JCR <jc...@proxymit.net> wrote:

> Before I write anything on my own...
>
> Hello
>
> There is this convenient .json extension which produces a JSON
> serialization of any given resource in Sling.
>
> What would be a "best practice" if I only wanted a subset of all
> properties to be serialized on the json stream? That is, I would like to
> provide a "positive" list (lists all props to be exported) or a
> "negative" one (props to be excluded).
>
> Example: I have props a, b and c on a given node/resource and only a and
> c should appear on the output.
>
> Thanks for any pattern hint
> Juerg
>
>