JSON format of a RESTful application
By A.Bouchez on 2010, Friday July 2, 10:11 - Pascal Programing - Permalink
RESTful JSON is still a buzzing process... there is no standard yet, and they should not be, since JSON itself has its own RFC, and REST is a powerful but vague statement.
Note that most common RESTful JSON uses a quite verbose format for the JSON content: see for example http://bitworking.org/news/restful_json which proposed to put whole URL in the JSON content
[ "http://example.org/coll/1", "http://example.org/coll/2", "http://example.org/coll/3", ... "http://example.org/coll/N", ]
I really prefer our implementation
[{"ID":1},{"ID":2},{"ID":3},{"ID":4}]
which preserves bandwidth and human readibility: if you were able to send a GET
request to the URL http://example.org/collyou will be able to
append this URL at the beginning of every future request, doesn't it make
sense?
In all cases, our framework always returns the JSON content just as a pure response of a SQL query, with an array and field names.
Note that our JSON content has two layouts, which can be produced according
to the TSQLRestServer.NoAJAXJSON property:
1. the "expanded" or standard/AJAX layout, which allows you to create pure JavaScript objects from the JSON content, because the field name / JavaScript object property name is supplied for every value:
[{"ID":0,"Int":0,"Test":"abcde+¬ef+á+¬","Unicode":"abcde+¬ef+á+¬","Ansi":"abcde+¬ef+á+¬","ValFloat":3.14159265300000E+0000,"ValWord":1203,"ValDate":"2009-03-10T21:19:36","Next":0},{..}]
2. the "not expanded" layout, which reflects exactly the layout of the SQL request: first line are the field names, then all next lines are the field content:
{"FieldCount":9,"Values":["ID","Int","Test","Unicode","Ansi","ValFloat","ValWord","ValDate","Next",0,0,"abcde+¬ef+á+¬","abcde+¬ef+á+¬","abcde+¬ef+á+¬",3.14159265300000E+0000,1203,"2009-03-10T21:19:36",0,..]}
By default, the NoAJAXJSON property is set to TRUE in
TSQLRestServer.ExportServerNamedPipe: if you use named pipes for
communication, you probably won't use javascript because browser communicates
via HTTP!
But otherwise, NoAJAXJSON property is set to FALSE. You could
force its value to TRUE if you'd save some bandwidth and don't use javascript:
even the parsing of the JSON Content will be faster with Delphi if JSON content
is not expanded.
In this "not expanded" layout, the first JSON content of this post will be transfered as:
{"FieldCount":1,"Values":["ID",1,2,3,4,5]}
More details, and discussion to continue on our forum.