• Login
  • login to access premium content (it's free).
 # 
 # File Formats
 # JSON
 # XML
 #
 # We rely on a global tag_filetype to determine output text format
 # if tag_filetype is ambiguous then default to json
 #  - if tag_filetype='XML' then filetype='XML' else filetype='JSON' /if
/#

The api library is used for generating responses to api requests.

It focuses on building a json or an xml string to be delivered via webpush()

Build nodes by adding name/value pairs with outvar=appendNode(outvar,name,value) then render final out using push_nodes(outvar)

For an example suppose we wanted to create a xml or json object to describe the properties of a file

a=fileinfo('/logo.jpg')

       name       size   last modify         kind   area
a ==> /logo.jpg   1950   09/10/05 12:43:00   FILE   PUBLIC

This can be accomplished by creating 5 nodes using appendnode()

info='ERROR'
info=appendNode(info,'name',a[1,1])
info=appendNode(info,'size',a[2,1])
info=appendNode(info,'modify',a[3,1])
info=appendNode(info,'kind',a[4,1])
info=appendNode(info,'area',a[5,1])

out=push_nodes(info)

Notice push_nodes() actually delivers the rendered json or XML pen here

The above code will render the variable out as either JSON or XML, uses global tag_filetype to determine output format.

JSON
{
  name :  "/logo.png",
  size :  1048,
  modify :  "2017-10-05T12:43",
  kind :  "FILE",
  area :  "PUBLIC"
}

XML
<document>
 <name>
  /logo.png
 </name>
 <size>
  1048
 </size>
 <modify>
  10/05/17 12:43:00
 </modify>
 <kind>
  FILE
 </kind>
 <area>
  PUBLIC
 </area>
</document>

You can also send out a table like this

table='ERROR'
table=appendTableNode(table,'fileinfo',a)

out=push_nodes(table)

JSON
[
 "/logo.png",
 1048,
 "10/05/17 12:43:00",
 "FILE",
 "PUBLIC"
]

XML
<fileinfo cols="5" rows="1">
 <row>
  <col>
   /logo.png
  </col>
  <col>
   1048
  </col>
  <col>
   10/05/17 12:43:00
  </col>
  <col>
   FILE
  </col>
  <col>
   PUBLIC
  </col>
 </row>
</fileinfo>

and finally you can nest and create hierarchical trees.

Suppose you want to deliver the json as name values and also as a table you can do it this way...

info='ERROR'
info=appendNode(info,'name',a[1,1])
info=appendNode(info,'size',a[2,1])
info=appendNode(info,'modify',a[3,1])
info=appendNode(info,'kind',a[4,1])
info=appendNode(info,'area',a[5,1])

out='ERROR'
out=appendNode(out,'properties',info)
out=appendTableNode(out,'fileinfo',a)

out=push_nodes(out)


JSON
{
  properties : {
    name :  "/logo.png",
    size :  1048,
    modify :  "2017-10-05T12:43",
    kind :  "FILE",
    area :  "PUBLIC"
  },
  fileinfo : [
    "/logo.png",
    1048,
    "10/05/17 12:43:00",
    "FILE",
    "PUBLIC"
  ]
}

XML
<document>
 <properties>
  <name>
   /logo.png
  </name>
  <size>
   1048
  </size>
  <modify>
   10/05/17 12:43:00
  </modify>
  <kind>
   FILE
  </kind>
  <area>
   PUBLIC
  </area>
 </properties>
 <fileinfo cols="5" rows="1">
  <row>
   <col>
    /logo.png
   </col>
   <col>
    1048
   </col>
   <col>
    10/05/17 12:43:00
   </col>
   <col>
    FILE
   </col>
   <col>
    PUBLIC
   </col>
  </row>
 </fileinfo>
</document>

And one final note, you could use XML attributes to represent the data as well

attribs='ERROR'
attribs=setvalue(attribs,'size',a[2,1])
attribs=setvalue(attribs,'modify',a[3,1])
attribs=setvalue(attribs,'kind',a[4,1])
attribs=setvalue(attribs,'area',a[5,1])

out='ERROR'
out=appendNodeAttribs(out,'file',a[1,1],attribs)

out=push_nodes(out)

XML
<file size="1048" modify="10/05/17 12:43:00" kind="FILE" area="PUBLIC">
 /logo.png
</file>

JSON
{
 @size :  1048,
 @modify :  2017-10-05T12:43,
 @kind :  FILE,
 @area :  PUBLIC,
 file :  /logo.png
}

end.