Saturday, March 15, 2014 1:30:28 PM

So, we're having our developer's teleconference, working out the specs for an 
app that will be pulling data from 4D. I have done similar projects, using SOAP 
in 4D's web services (which is a fantastic implementation--works beautifully 
and is really simple to set up and use). Then, they casually toss out, "oh, 
we're going to be doing this project using JSON; let us know the details on 
your end by our next meeting on Monday. Have a nice weekend."*

Gulp!

But hey, this is 4D, and with Active4D, I know this will be a piece of cake! (I 
hope... :-)

So, just would like to go over a general overview on this, since I haven't done 
anything using JSON, and just want to make sure I'm on the right track. Again, 
I have extensive knowledge of 4D's SOAP implementation, so if I can more or 
less figure out a way to get JSON working in a similar fashion, I should be 
good to go.

With 4D's web services, it works as follows:

1) You create a method and in the method properties, designate it as available 
as a web service (click a checkbox).

2) In the method you put in SOAP DECLARATION calls to designate your input and 
output parameters (basically binding the variable names from SOAP to 4D 
variables)

3) Write your code.

That's it; 4D handles everything else, with I/O handled by the web server, 
everything translated automatically to and from XML, etc. The caller requests 
the WSDL (basically the directory of your SOAP methods and parameter lists), 
then just calls the methods with input parameters, and gets back the output 
parameters. 

So, to do the same sort of thing in JSON, I'm thinking Active4D will be ideal, 
since there's a wealth of JSON commands already built, and apparently these are 
all native plugin commands now, so should be pretty fast. Plus I know 
Aparajita's stuff is always industrials strength and super powerful, so all I 
need to do is figure out the right way to approach it.

What I'm thinking is to build separate A4D pages, each corresponding to a 
function (corresponding to a SOAP method in 4D). This essentially will be the 
API for the project. So it might be set up like:

https://www.site.com/project/getattendees.a4d

https://www.site.com/project/getmeetings.a4d

and each A4D page would return the corresponding data from 4D (ie, list of 
attendees, list of meetings, etc.). They'll POST to these pages with perhaps 
some parameters (password to access data, search vars, data to input, etc.).

This should make code management fairly straightforward; each page is a 
function, and updating a function means just popping in a new page (no need to 
take down 4D and recompile).

Now for the details. As mentioned, with 4D's implementation of SOAP, 
everything's done for you; you don't have to muck around with XML or anything 
with the protocol. You just declare your variables, and 4D handles the rest. 
For JSON, I'm assuming I can do something similar in Active4D, where I can get 
the incoming data somehow from the native JSON format into 4D vars.

I'm assuming I would use the command:

parse json(inJSON) -> <any>

somewhere at the top of the method, right?

So, where does "inJSON" come from? 

In 4D version 14, there's a new command that probably would do the trick:

WEB GET HTTP BODY ( inJSON )

However, looking through the docs for Active4D, I found under the section of 
"Form Variables":

> Posting Raw Data
> Normally, item values in the form variables collection are either text or 
> text arrays. However, if a form is posted which contains raw data (such as 
> XML) and no form variables, and the “parameter mode” configuration option is 
> not set to “query params”, Active4D creates a single BLOB item in the form 
> variables collection with the key “_data_”.

Although it mentions "XML", I think this would also apply to JSON, no? So, I'm 
guessing you access it in Active4D as _form{"_data_"}. Is that right?

The ideal would be if I can simply convert JSON data into a collection; then 
I'm home free with accessing the data. So would something like this work?

$a4d_collection := new collection
$a4d_collection ::= parse json (BLOB to text(_form{"_data_"})) 

Then I can simply reference an item in the collection, such as:

$vValue := $a4d_collection{"key"}

Am I on the right track? Will this even work? Or is there a better way to do 
this?

OK, once I have the data in 4D variables, then I do my code. When finished, to 
output the data back, I assume I build a JSON object and then output it using 
the "write json" command, correct?

So would something like this work?

$JSON_Object := new json 

add to json($JSON_Object; "Item1"; $vItem1)
add to json($JSON_Object; "Array2"; $aArray2)
etc.

write json ($JSON_Object) 


How am I doing? Any of this make sense? (And yes, I know there are other even 
more powerful A4D commands to add data to JSON; will be looking at these when 
building the code.)

OK, many thanks for any help, comments, etc. As mentioned, I'm new to JSON, so 
please forgive my ignorance on how this should work...

Cheers!

Michael Larue

*To a programmer, Friday just means there are only two working days until 
Monday. :-)




_______________________________________________
Active4D-dev mailing list
[email protected]
http://list.aparajitaworld.com/listinfo/active4d-dev
Archives: http://active4d-nabble.aparajitaworld.com/

Reply via email to