JavaScript ObjectScript Notation (JSON) Builder

From CacheWiki

Jump to: navigation, search

Introduction

I have written a 'JavaScript ObjectScript Notation (JSON)' builder function to pass data from a WebLink Developer (WLD) server to a Web Browser client. It was written with Caché 5.0 and based on information from this wikipedia page.

Example

Here is an example of a JSON message. The 'indent' parameter is optional and if not defined all whitespace is removed. This is how I intent to use it when transferring information from the server to browser.

Set indent=0
Set jsonText=""
Set jsonText=$$StartObject^fJSON(jsonText,.indent)
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"firstName")
Set jsonText=$$String^fJSON(jsonText,"John")
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"lastName")
Set jsonText=$$String^fJSON(jsonText,"Smith")
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"address")
Set jsonText=$$StartObject^fJSON(jsonText,.indent)
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"streetAddress")
Set jsonText=$$String^fJSON(jsonText,"21 2nd Street")
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"city")
Set jsonText=$$String^fJSON(jsonText,"New York")
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"state")
Set jsonText=$$String^fJSON(jsonText,"NY")
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"postalCode")
Set jsonText=$$Number^fJSON(jsonText,10021)
Set jsonText=$$EndObject^fJSON(jsonText,.indent)
Set jsonText=$$NewMember^fJSON(jsonText,.indent,"phoneNumbers")
Set jsonText=$$StartArray^fJSON(jsonText,.indent)
Set jsonText=$$NewElement^fJSON(jsonText,.indent)
Set jsonText=$$String^fJSON(jsonText,"212 555-1234")
Set jsonText=$$NewElement^fJSON(jsonText,.indent)
Set jsonText=$$String^fJSON(jsonText,"646 555-4567")
Set jsonText=$$EndArray^fJSON(jsonText,.indent)
Set jsonText=$$EndObject^fJSON(jsonText,.indent)
indent=0
jsonText="
{
  "firstName": "John",
  "lastName": "Smith",
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  },
  "phoneNumbers": [
    "212 555-1234",
    "646 555-4567"
  ]
}"
jsonText="{ "firstName": "John", "lastName": "Smith", "address": { "streetAddres
s": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "
phoneNumbers": [ "212 555-1234", "646 555-4567" ] }"

Code

fJSON   ;;JAVASCRIPT OBJECT NOTATION BUILDER;;V1.00;;NOV2008;;TSG;;
        ; SET OF FUNCTIONS TO BUILD JSON STRINGS
        ;
        Quit
        ;
StartObject(json,&ind) ;
        Quit $$StartObjArr(json,.ind,"{")
        ;
NewMember(json,&ind,str) ;
        Quit $$NewMemEle(json,.ind,str)
        ;
EndObject(json,&ind) ;
        Quit $$EndObjArr(json,.ind,"}")
        ;
StartArray(json,&ind) ;
        Quit $$StartObjArr(json,.ind,"[")
        ;
NewElement(json,&ind) ;
        Quit $$NewMemEle(json,.ind)
        ;
EndArray(json,&ind) ;
        Quit $$EndObjArr(json,.ind,"]")
        ;
Boolean(json,bool) ;
        Quit json_$Select(bool:"true",1:"false")
        ;
Number(json,num) ;
        Quit json_+num
        ;
String(json,str) ;
        If str="" Quit json_"null"
        Quit json_$Char(34)_str_$Char(34)
        ;
        ; internal function - do not use
StartObjArr(json,&ind,str) ;
        New count
        If $Data(ind),$Extract($Reverse(json),2)'?1(1":",1" ") Do 
        . Set json=json_$Char(13,10)
        . For count=1:1:ind Set json=json_" "
        . Quit
        If $Data(ind) Set ind=ind+2
        Quit json_str_" "
        ;
        ; internal function - do not use
NewMemEle(json,&ind,str) ;
        New count
        If $Length(json),$Extract($Reverse(json),2)'?1(1"{",1"[") Set json=json_", "
        If $Data(ind) Set json=json_$Char(13,10)
        For count=1:1:$Get(ind,0) Set json=json_" "
        If $Data(str) Quit json_$Char(34)_str_$Char(34)_": "
        Quit json
        ;
        ; internal function - do not use
EndObjArr(json,&ind,str) ;
        New count
        If $Data(ind) Set json=json_$Char(13,10),ind=ind-2
        For count=1:1:$Get(ind,1) Set json=json_" "
        Quit json_str
        ;
Personal tools