[magnolia-dev] [JIRA] (JSMODELS-5) Way to get the data structure of an object - JS and NON-JS objects

"JIRA (on behalf of Christopher Zimmermann)" <jira-O8DnXSz/[email protected]> Thu, 15 Jun 2017 15:55:02 +0200 (CEST)
Newsgroups gmane.comp.cms.magnolia.devel
Message-ID <[email protected]>
Christopher Zimmermann updated an issue

Magnolia Javascript Models / https://jira.magnolia-cms.com/browse/JSMODELS-5 JSMODELS-5

Way to get the data structure of an object - JS and NON-JS objects

Change By:
Christopher Zimmermann

Some way for a templater to output the structure and content of an object within a template for development purposes.

Why:
It can be hard to work with content objects in a template because you do not know the structure of the content.
It can be especially hard to understand the actual structure of the objects returned from Javascript models. Ideally a simple method in cmsfn would give a templater a string representation of the object.

It 's difficult to work with the objects that are passed into the methods of a JS model from the templating script, because its hard to know the structure of the object.

For example the object returned by cmsfn.children is an object of objects, so it cannot be iterated as a javascript array.

If you return an array from a model, then pass that array Its not possible to another model function use something like JSON .
* It cannot be iterated with for(var item stringify in input) {
* It cannot be iterated with or each (item in input){
* it does not have a length JS model .

It would be useful if the developer could determine its structure.
-
Maybe there should be method which outputs a string representation of the data structure. I tried using JSON.stringify, but it always returns undefined.
http://codereview.stackexchange.com/questions/91184/java-json-stringifier-with-the-nashorn-api
http://mail.openjdk.java.net/pipermail/nashorn-dev/2013-September/002012.html
-
If there is an existing method in Java or JS that solves this problem, then we should simply document that.

---
Implementation:
Dumper can be used to describe objects (for example in FTLs). User can choose how deep it should go in the object hierarchy when describing objects (using depth parameter) and if the output should in plain text or HTML formatted.

Example usage:
{code}
String result = Dumper.newBuilder().withDepth(3).withUseHtml(true).dump(myObject);
{code}

Users can also provide custom describers for certain object types by implementing {{AbstractDescriber<T>}} class:
{code}
String result = Dumper.newBuilder().withAdditionalDescriber(myCustomDescriber).dump(object);
{code}

Describers for following types are provided out of the box:
* Null
* Scalars
* Dates
* Calendar
* Arrays
* Collections
* Maps
* ContentMaps
* Nodes
* Properties
* Objects (this one describes only the object class to avoid generating huge hierarchies)
* JavaScript Objects (objects implementing JSObject interface)

Node:
{noformat}
Node (/magnolia-travels/Vietnam--Tradition-and-Today)
author = "Magnolia Travels" (String)
body = "<p>Vietnam is one of the world&rsquo;s most exotic and culturally rich destinations. A gem..." (String)
body_de = "<p>Vietnam ist eine der exotischsten und kulturreichsten Destinationen weltweit. Als Juwel unt..." (String)
description = "Discover the culture and everyday treasures of a rising phoenix" (String)
description_de = "Entdecken Sie die Kultur und die alltäglichen Schätze eines Phoenix aus der Asche" (String)
destination = Sequence (1)
0 = "7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2" (String)
duration = "14" (String)
image = "jcr:1044b3b8-30b1-48fe-8078-832b6cef8fb5" (String)
isFeatured = true (Boolean)
location = "Ho Chi Minh City, Vietnam" (String)
name = "Vietnam: Tradition and Today" (String)
name_de = "Vielfältiges Vietnam" (String)
tourTypes = Sequence (2)
0 = "415025c6-e4b5-4506-9384-34f428a52104" (String)
1 = "e007e401-1bf8-4658-8293-b9c743784264" (String)
mgnl:activationStatus = true (Boolean)
mgnl:comment = "" (String)
mgnl:created = Jan 29, 2015 07:57:18 AM (Date)
mgnl:createdBy = "superuser" (String)
mgnl:lastActivated = Jul 1, 2015 08:48:49 AM (Date)
mgnl:lastActivatedBy = "superuser" (String)
mgnl:lastModified = Jun 18, 2015 12:20:06 PM (Date)
mgnl:lastModifiedBy = "superuser" (String)
jcr:created = Jun 12, 2017 01:41:27 PM (Date)
jcr:createdBy = "admin" (String)
jcr:primaryType = "mgnl:content" (String)
jcr:uuid = "3d676477-eabc-4cbe-88b6-b77aa85a358a" (String)
{noformat}

Property:
{noformat}
Magnolia Travels (Property)
{noformat}

Multivalue Property:
{noformat}
Property (2)
0 = "415025c6-e4b5-4506-9384-34f428a52104" (String)
1 = "e007e401-1bf8-4658-8293-b9c743784264" (String)
{noformat}

ContentMap:
{noformat}
ContentMap (/magnolia-travels/Vietnam--Tradition-and-Today)
author = "Magnolia Travels" (String)
body = "<p>Vietnam is one of the world&rsquo;s most exotic and culturally rich destinations. A gem..." (String)
body_de = "<p>Vietnam ist eine der exotischsten und kulturreichsten Destinationen weltweit. Als Juwel unt..." (String)
depth = null (Null)
description = "Discover the culture and everyday treasures of a rising phoenix" (String)
description_de = "Entdecken Sie die Kultur und die alltäglichen Schätze eines Phoenix aus der Asche" (String)
destination = Sequence (1)
0 = "7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2" (String)
duration = "14" (String)
id = null (Null)
image = "jcr:1044b3b8-30b1-48fe-8078-832b6cef8fb5" (String)
isFeatured = true (Boolean)
location = "Ho Chi Minh City, Vietnam" (String)
name = "Vietnam: Tradition and Today" (String)
name_de = "Vielfältiges Vietnam" (String)
nodeType = null (Null)
path = null (Null)
tourTypes = Sequence (2)
0 = "415025c6-e4b5-4506-9384-34f428a52104" (String)
1 = "e007e401-1bf8-4658-8293-b9c743784264" (String)
mgnl:activationStatus = true (Boolean)
mgnl:comment = "" (String)
mgnl:created = Jan 29, 2015 07:57:18 AM (GregorianCalendar)
mgnl:createdBy = "superuser" (String)
mgnl:lastActivated = Jul 1, 2015 08:48:49 AM (GregorianCalendar)
mgnl:lastActivatedBy = "superuser" (String)
mgnl:lastModified = Jun 18, 2015 12:20:06 PM (GregorianCalendar)
mgnl:lastModifiedBy = "superuser" (String)
jcr:created = Jun 12, 2017 01:41:27 PM (GregorianCalendar)
jcr:createdBy = "admin" (String)
jcr:primaryType = "mgnl:content" (String)
jcr:uuid = "3d676477-eabc-4cbe-88b6-b77aa85a358a" (String)
{noformat}

Sequence of ContentMaps:
{noformat}
Sequence (36)
0 = ContentMap (/magnolia-travels/Vietnam--Tradition-and-Today)
author = "Magnolia Travels" (String)
body = "<p>Vietnam is one of the world&rsquo;s most exotic and culturally rich destinations. A gem..." (String)
body_de = "<p>Vietnam ist eine der exotischsten und kulturreichsten Destinationen weltweit. Als Juwel unt..." (String)
depth = null (Null)
description = "Discover the culture and everyday treasures of a rising phoenix" (String)
description_de = "Entdecken Sie die Kultur und die alltäglichen Schätze eines Phoenix aus der Asche" (String)
destination = Sequence (1)
0 = "7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2" (String)
duration = "14" (String)
id = null (Null)
image = "jcr:1044b3b8-30b1-48fe-8078-832b6cef8fb5" (String)
isFeatured = true (Boolean)
location = "Ho Chi Minh City, Vietnam" (String)
name = "Vietnam: Tradition and Today" (String)
name_de = "Vielfältiges Vietnam" (String)
nodeType = null (Null)
path = null (Null)
tourTypes = Sequence (2)
0 = "415025c6-e4b5-4506-9384-34f428a52104" (String)
1 = "e007e401-1bf8-4658-8293-b9c743784264" (String)
mgnl:activationStatus = true (Boolean)
mgnl:comment = "" (String)
mgnl:created = Jan 29, 2015 07:57:18 AM (GregorianCalendar)
mgnl:createdBy = "superuser" (String)
mgnl:lastActivated = Jul 1, 2015 08:48:49 AM (GregorianCalendar)
mgnl:lastActivatedBy = "superuser" (String)
mgnl:lastModified = Jun 18, 2015 12:20:06 PM (GregorianCalendar)
mgnl:lastModifiedBy = "superuser" (String)
jcr:created = Jun 12, 2017 01:41:27 PM (GregorianCalendar)
jcr:createdBy = "admin" (String)
jcr:primaryType = "mgnl:content" (String)
jcr:uuid = "3d676477-eabc-4cbe-88b6-b77aa85a358a" (String)
1 = ContentMap (/magnolia-travels/Hut-to-Hut-in-the-Swiss-Alps)
author = "Magnolia Travels" (String)
body = "<p>Some Swiss people are going to be pretty annoyed with us for revealing their secrets. Hush...." (String)
body_de = "<p>Einige Schweizer werden sich &auml;rgern, dass wir ihre Geheimnisse aufdecken. Psst... ..." (String)
depth = null (Null)
description = "There's more to Switzerland than skiing" (String)
description_de = "Die Schweiz ist mehr als nur Skifahren" (String)
destination = Sequence (1)
0 = "6cc50e28-fb0e-4e49-b3b6-728690a2e861" (String)
duration = "7" (String)
id = null (Null)
image = "jcr:80d091ae-22ce-4cfc-b302-cb39f25c3d30" (String)
location = "Zurich, Switzerland" (String)
name = "Hut to Hut in the Swiss Alps" (String)
name_de = "Von Hütte zu Hütte in den Schweizer Alpen" (String)
nodeType = null (Null)
path = null (Null)
tourTypes = Sequence (1)
0 = "eaf9a648-fae1-48ae-a293-69bed874f159" (String)
mgnl:activationStatus = true (Boolean)
mgnl:comment = "" (String)
mgnl:created = Jan 29, 2015 07:55:07 AM (GregorianCalendar)
mgnl:createdBy = "superuser" (String)
mgnl:lastActivated = Jul 1, 2015 08:48:52 AM (GregorianCalendar)
mgnl:lastActivatedBy = "superuser" (String)
mgnl:lastModified = Jun 18, 2015 11:36:14 AM (GregorianCalendar)
mgnl:lastModifiedBy = "superuser" (String)
jcr:created = Jun 12, 2017 01:41:27 PM (GregorianCalendar)
jcr:createdBy = "admin" (String)
jcr:primaryType = "mgnl:content" (String)
jcr:uuid = "d30b9ca6-e22d-45c5-9456-7538ab7bccf8" (String)
2 = ContentMap (/magnolia-travels/West-Coast---Highway-101)
author = "Magnolia Travels" (String)
body = "<p>Imagine walking in the footsteps of the explorers, missionaries, and soldiers who made Amer..." (String)
body_de = "<p>Stellen Sie sich vor, dass Sie auf den Spuren der Forscher, Missionare und Soldaten unterwe..." (String)
depth = null (Null)
description = "Family travels in USA" (String)
description_de = "Familienreisen in den USA" (String)
destination = Sequence (1)
0 = "ed3dcd18-78af-46df-a9e1-bc732479f2e7" (String)
duration = "7" (String)
id = null (Null)
image = "jcr:d44f783c-91ca-4b48-9533-ea4e147e13e9" (String)
location = "Seattle, USA" (String)
name = "West Coast - Highway 101" (String)
name_de = "Westküste - Highway 101" (String)
nodeType = null (Null)
path = null (Null)
tourTypes = Sequence (1)
0 = "17da160b-da66-4e09-9e31-5fd040880f4a" (String)
mgnl:activationStatus = true (Boolean)
mgnl:comment = "" (String)
mgnl:created = Jan 29, 2015 07:51:38 AM (GregorianCalendar)
mgnl:createdBy = "superuser" (String)
mgnl:lastActivated = Jul 1, 2015 08:48:56 AM (GregorianCalendar)
mgnl:lastActivatedBy = "superuser" (String)
mgnl:lastModified = Jun 18, 2015 11:40:45 AM (GregorianCalendar)
mgnl:lastModifiedBy = "superuser" (String)
jcr:created = Jun 12, 2017 01:41:27 PM (GregorianCalendar)
jcr:createdBy = "admin" (String)
jcr:primaryType = "mgnl:content" (String)
jcr:uuid = "730c8850-d638-4e91-b3fb-4041a0c59ffe" (String)
3 = ContentMap (/magnolia-travels/Inside-New-Delhi)
author = "Magnolia Travels" (String)
body = "<p>Delhi has many interesting monuments and museums, but <em>we won&#39;t be visitin..." (String)
body_de = "<p>Delhi hat viele interessante Baudenkm&auml;ler und Museen, aber wir besuchen auf dieser..." (String)
depth = null (Null)
description = "Get to know Delhi up close and personal" (String)
description_de = "Lernen Sie Delhi ganz aus der Nähe und intim kennen" (String)
destination = Sequence (1)
0 = "7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2" (String)
duration = "2" (String)
id = null (Null)
image = "jcr:ad33e9b0-855e-4bac-b35e-3c7e7588465c" (String)
location = "New Delhi, India" (String)
name = "Inside New Delhi" (String)
name_de = "In Neu Delhi eintauchen" (String)
nodeType = null (Null)
path = null (Null)
tourTypes = Sequence (2)
0 = "415025c6-e4b5-4506-9384-34f428a52104" (String)
1 = "e007e401-1bf8-4658-8293-b9c743784264" (String)
mgnl:activationStatus = true (Boolean)
mgnl:comment = "" (String)
mgnl:created = Jan 29, 2015 07:50:20 AM (GregorianCalendar)
mgnl:createdBy = "superuser" (String)
mgnl:lastActivated = Jul 1, 2015 08:49:03 AM (GregorianCalendar)
mgnl:lastActivatedBy = "superuser" (String)
mgnl:lastModified = Jun 18, 2015 11:41:28 AM (GregorianCalendar)
mgnl:lastModifiedBy = "superuser" (String)
jcr:created = Jun 12, 2017 01:41:27 PM (GregorianCalendar)
jcr:createdBy = "admin" (String)
jcr:primaryType = "mgnl:content" (String)
jcr:uuid = "3ba5ba55-ac02-4b37-a429-5fc0bb4b383b" (String)
...
{noformat}

Hash:
{noformat}
Hash (2)
foo = "bar" (String)
baz = Hash (3)
i = "am" (String)
nested = "hash" (String)
yay! = Hash (1)
hi = "bye" (String)
{noformat}

JavaScript Function:
{noformat}
[object Object] (JavaScript Function)
{noformat}

JavaScript Array:
{noformat}
JavaScript Array (3)
0 = "a" (String)
1 = "b" (String)
2 = "c" (String)
{noformat}

JavaScript Object:
{noformat}
JavaScript Object (4)
a = "foo" (String)
b = 11 (Integer)
c = JavaScript Array (3)
0 = 1 (Integer)
1 = 2 (Integer)
2 = 3 (Integer)
d = [object Object] (JavaScript Function)
{noformat}

https://jira.magnolia-cms.com/browse/JSMODELS-5#add-comment
Add Comment

This message was sent by Atlassian JIRA (v7.2.6#72008-sha1:26175bf)

----------

----------------------------------------------------------------

For list details, see: http://www.magnolia-cms.com/community/mailing-lists.html

Alternatively, use our forums: http://forum.magnolia-cms.com/

To unsubscribe, E-mail to: <[email protected]>

----------------------------------------------------------------
(unnamed) (image/png, 1.1 KB) - not displayed
(unnamed) (image/png, 468 B) - not displayed
(unnamed) (image/png, 2.9 KB) - not displayed