Forums / Developer / JSON-view of any content object

JSON-view of any content object

Author Message

Felix Laate

Saturday 04 August 2007 1:35:41 pm

Hi all!

I'm looking into the possibility to get any content object as an JSON-object. The reason for this is that I'm currently planning several AJAX-based stuff for a certain website.

Has anyone done this already? As an extension to the content-module? Or as a brand new one?

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

kracker (the)

Monday 06 August 2007 5:03:16 am

Hello Felix,

Not certain if this meets your needs but I saw this the other day and thought I'd share this reference in passing.

<i>http://pubsvn.ez.no/community/trunk/extension/xeditor/modules/xeditor/JSON.php</i>

<i>cheers,
//kracker

Green Day - American Idiot - Boulevard of Broken Dreams; `I walk alone, I walk alone .... ..`</i>

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Paul Borgermans

Monday 06 August 2007 5:56:18 am

Hi Felix,

We are looking into this not only for any content object, but on a wider scale. For example in the Solr based extension to navigate through pages of search results and some fetch functions.

So this is planned, but an open funding project on this could speed up the development.

Can you elaborate a bit more on your needs (if it's more than just get an object by id), maybe write a small proposal for open funding?

@Kracker: the JSON implementation you point to is pure PHP, better is to use the native implementation (included with PHP5.2 and up by default, but can be installed with previous PHP versions as well). This is really fast!

Best regards
Paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Xavier Dutoit

Monday 06 August 2007 9:11:30 am

If speed isn't an issue (and it probably isn't, otherwise you wouldn't be using ez ;) you can create a json layout that generates the right content.

you can have a general json_full template that generates all the attributes, or customize some or...

Otherwise, you can have a look at the ajax extension created by andre for the left hand tree (ezjaxx ), I think the latest version generates directly the content, without using the template languages and said it was much quicker.

X+

http://www.sydesy.com

André R.

Monday 06 August 2007 3:04:53 pm

The one in svn is even faster, I just haven't released it yet since a new feature called cache manager needs to be changed ( it currently deletes outdated compiled templates, but should rather recompile them ).

But, I do not use json in ezjaxx since I didn't want to do the formatting in javascript.
Json output will be a lot easier with the upcoming php 5 version anyway ( ps: not 3.10 Xav :p )

And as for speed and scalability, I think you will have to change your opinion on that in the course of the next 6 months ;)

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Felix Laate

Tuesday 07 August 2007 3:18:25 am

Hi all!

Thank you all for answering!

My needs are somewhat unclear, but let me use a concrete example of a service in which I think JSON would be an efficient option:

<i>There exists a class named reciept and a datatype named ingredient. A reciept consists of many ingredients.

The example-service lets lets you specify what ingredients you might have at home and suggests reciepts based on that, and even gives you a shopping list consisting of the missing ingredients. Lets imagine there are some 1200 ingredients and 6000 reciepts.

You should be able to click on e.g. meat, and a list of types of meat should appear. The you choose e.g. lamb, and a list of types of lambmeat appear. The you drag and drop the meat you have into e.g. a pot. And each time you add something to the pot, the list of suggested reciepts is updated, listed objects appearing weighted (after how well they fit the stuff in the pot).
</i>

As I see things, a AJAX-based solution would fit these needs. Because of usability and because speed is an issue.

It's not a secret that eZ Publish is not the quickest solution out there, but it's not a big problem when it comes to listing and full viewing. But for services as in the example above, in my opinion, speed is absolutly an issue. If there was aquick way to return a JSON-object containing any content object, it could be combined with e.g. the C-based php-json (http://aurore.net/projects/php-json/) and e.g. mootools to make a great solution.

I'm all for Open Funding. All for One and One for All.

Cheers
Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Xavier Dutoit

Wednesday 15 August 2007 3:11:30 pm

Hi,

I'm working on a json extension... stay tuned...

X+

http://www.sydesy.com

Felix Laate

Thursday 16 August 2007 12:05:11 am

Hi again,

great! Can't wait.

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Xavier Dutoit

Thursday 16 August 2007 1:20:49 am

By the way that's json only, combined with jquery.

Pretty fast, but need help to convert each attribute content to something useful.

Any volunteer ?

X+

http://www.sydesy.com

Felix Laate

Thursday 16 August 2007 3:05:07 am

Do you need som javascript-code that takes a JSON-object and turns it into template-attributes?

Do I understand you correctly?

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Xavier Dutoit

Thursday 16 August 2007 4:28:58 am

Well, the problem is that the content you get is the row data from the db (you have a few standard fields, that each different type of attribute use differently).

What we need is to transate that if the attribute is a ezinteger, the field data_int_1 contains the value, and data_int_3 the max allowed value...

and do that for each type of attribute as there isn't anything standard to my knowledge (you have a method to convert to string if I'm right).

This conversion can be either on the server or the client.

X+

http://www.sydesy.com

André R.

Thursday 16 August 2007 5:11:28 am

As part of the eztinymce development, I have created a general json_content class to export contentObject / node data to json.

Tak a look here:
http://ez.no/community/contribs/hacks/tinymce_xhtml_editor

It's in /classes/json_content.php
Examples can be found in /modules/eztinymce/*

it only supports ezstring and ezimage as a start, but support for the simple datatypes should be easy.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Xavier Dutoit

Thursday 16 August 2007 8:00:53 am

Some of your code is quite.... ;)

            $node = $obj;

That's what I generate for one single node

{"ID":"1054","Name":"a second point we offer is to \r\ngo to the line again","CurrentLanguage":"eng-GB","ClassName":false,"ClassIdentifier":false,"DataMap":{"1":{"eng-GB":{"text":{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"235","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"text","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5684","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"eztext","DataText":"a second point we offer is to \r\ngo to the line again","DataInt":null,"DataFloat":"0"},"alert":{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"236","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"alert","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5685","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"ezboolean","DataText":"","DataInt":"0","DataFloat":"0"},"ref":{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"237","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"ref","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5687","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"ezstring","DataText":"","DataInt":null,"DataFloat":"0"}}}},"ContentActionList":false,"ContentObjectAttributes":{"1":{"eng-GB":[{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"235","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"text","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5684","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"eztext","DataText":"a second point we offer is to \r\ngo to the line again","DataInt":null,"DataFloat":"0"},{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"236","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"alert","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5685","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"ezboolean","DataText":"","DataInt":"0","DataFloat":"0"},{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"237","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"ref","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5687","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"ezstring","DataText":"","DataInt":null,"DataFloat":"0"}]}},"MainNodeID":false,"PersistentDataDirty":false,"SectionID":"1","OwnerID":"14","ClassID":"26","IsPublished":"0","Published":"1186757618","Modified":"1186757618","CurrentVersion":"1","Status":"1","RemoteID":"e6c62a157cd6c52e37916147b5fa647e","ContentObjectAttributeArray":{"1":{"eng-GB":[{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"235","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"text","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5684","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"eztext","DataText":"a second point we offer is to \r\ngo to the line again","DataInt":null,"DataFloat":"0"},{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"236","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"alert","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5685","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"ezboolean","DataText":"","DataInt":"0","DataFloat":"0"},{"HTTPValue":null,"Content":null,"DisplayInfo":null,"IsValid":null,"ContentClassAttributeID":"237","ValidationError":null,"ValidationLog":null,"ContentClassAttributeIdentifier":"ref","ContentClassAttributeCanTranslate":null,"ContentClassAttributeName":null,"ContentClassAttributeIsInformationCollector":null,"ContentClassAttributeIsRequired":null,"PersistentDataDirty":false,"InputParameters":false,"HasValidationError":false,"DataTypeCustom":null,"ID":"5687","ContentObjectID":"1054","Version":"1","LanguageCode":"eng-GB","AttributeOriginalID":"0","SortKeyInt":"0","SortKeyString":"","DataTypeString":"ezstring","DataText":"","DataInt":null,"DataFloat":"0"}]}}}

(basically, hardcode mapping between the ez object and json.

Are you aware of an easy php trick to clean a wee bit ? (eg. get rid of all the HTTPValue,DisplayInfo... ?)

Last, but not the least, an easy method to populate the Content ?

Any suggestion more than welcome.
X+

http://www.sydesy.com

André R.

Thursday 16 August 2007 8:56:52 am

I'm not following you here, the node = obj thing is because its php5 code, and its just a quick and dirt way of handling both objects and nodes.

here is my output:

{
"name":"Home",
"contentobject_id":"66",
"main_node_id":"2",
"node_id":"2",
"parent_node_id":"1",
"url_alias":"",
"depth":"1",
"modified":"1187265487",
"published":"1178269888",
"section_id":"1",
"class_identifier":"frontpage",
"current_language":"eng-GB",
"owner_id":"14",
"data_map":[]
}

with code like this (simplified):

$jsonObj = new JsonContent();
echo $jsonObj->encode( eZContentObjectTreeNode::fetch( 2 ) );

you can aslo specify parameters, for getting datatype:

$jsonObj = new JsonContent();
echo $jsonObj->encode( $myObj, array('dataMap' => array('image') ) );

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Xavier Dutoit

Thursday 16 August 2007 12:40:53 pm

Created a project, i'll upload the code there, so we can have real examples.

X+

http://www.sydesy.com

Heath

Sunday 02 September 2007 10:14:46 pm

I found Xavier's json project the other day, @ <i>http://projects.ez.no/json</i>

Looking forward to the publishing of the first revision :)

Cheers,
Heath

Brookins Consulting | http://brookinsconsulting.com/
Certified | http://auth.ez.no/certification/verify/380350
Solutions | http://projects.ez.no/users/community/brookins_consulting
eZpedia community documentation project | http://ezpedia.org

Felix Laate

Monday 03 September 2007 12:52:10 am

Me too. Can't wait to try it out!

Do you still require a helping hand Xavier?

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Xavier Dutoit

Monday 03 September 2007 8:55:04 am

Very much so !

Any help wanted.

First, I need to retrieve my login/pwd on projects (got a new computer, lost all my accounts ;(

http://www.sydesy.com

Xavier Dutoit

Monday 10 September 2007 1:42:58 am

Hi,

I've uploaded everything on the svn.

Please feel free to add yourself to the project, that's a good start, but still quite crude.

X+

http://www.sydesy.com

Felix Laate

Saturday 22 September 2007 7:48:34 am

Hi Xavier,

thanks for this great work. I've begun to design a few scenarios where I would use json. And hopefully I will be able to make a js class for the client side.

Keep on rolling!

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com