Map => Json

Started by stanl, May 30, 2023, 08:39:35 AM

Previous topic - Next topic

stanl

Should this be looked at as a user-defined UDF, or could it be part of the mapping functions, i.e. like MapPutCSV(), but as MapPutJson()? Useful for REST which may need params, headers, body data.

td

Just a starting point and one of many ways to perform the task.

Code (winbatch) Select
AddExtender("ilcjs44i.dll", 0, "ilcjs64i.dll")

gosub UDPGLOB ;; Define UDPs

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Test map with various datatypes.
mStuff["pi"] = 3.121
mStuff["name"] = "WinBatch"
mStuff["answer"] = Int64(42)
mStuff["happy"] = ObjectType("BOOL", @true)
mStuff["nothing"] = ObjectType("NULL", 0)

; For this test start with an empty JSON object.
Obj = jsConNew(@JsonObj )
Cnt = MapToJson(Obj, mStuff)
Text = jsSerialize(Obj, 1, @JsonUTF16)

; In case more is added below.
jsConClose(Obj)

; Did it work?
Message("Map To JSON", Text:@lf:"Entries added: ":Cnt)
exit

:UDPGLOB ; Define procedures here.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MapToJson - adds a WIL map a JSON
;;             object as a key/value
;;             pairs.
;;  _jsobj - JSON extender JSON object
;;  _map   - WIL map to add to _jsobj
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;           
#defineFunction MapToJson(_jsobj,_map)
             
   foreach key in _map
      value =  _map[key]
      type  = GetJsType(&value)
      jsValueAdd(_jsobj, key, value, type)
   next

   return ArrInfo(_map, 1)
#endfunction

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GetJsType - returns JSON extender
;;             JSON type number that
;;             is the closed match to
;;             a WIL variables type.
;;
;;   _var - Pointer to WIL varaible.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;           
#defineFunction GetJsType( _pvar )
   ret = @JsonNull ; Unmappable type result is default.

   type = VarType(*_pvar)
   if type == 0        then ret = @JsonNull    ; undefined
   else if type & 1    then ret = @JsonNum     ; 32-bit integer
   else if type & 2    then ret = @JsonStr     ; string
   else if type & 4    then ret = @JsonNum     ; file handle
   else if type & 32   then ret = @JsonNum     ; floating point value
   else if type & 64   then ret = @JsonNum     ; binary buffer
   else if type & 128  then ret = @JsonStr     ; Unicode
   else if type & 256  then ret = @JsonNull    ; array (can`t nest so should never happen)
   else if type & 512  then ret = GetVtJsType(_pvar)  ; variant
   else if type & 1024 then ret = @JsonNull    ; COM Object (will cause an error)
   else if type & 8192 then ret = @JsonNum     ; 64-bit integer

   return ret
#endFunction

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GetVtJsType - returns JSON extender
;;               JSON type number that
;;               is the closed match to
;;               a variant variable type.
;;
;;   _var - Pointer to WIL varaible.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;           
#defineFunction GetVtJsType( _pvar )
   ret = @JsonNull ; Unmappable type result is default.

   type = ObjectTypeGet(*_pvar)
   if type == "EMPTY"        then ret = @JsonNull
   else if type == "I1"      then ret = @JsonNum     
   else if type == "UI1"     then ret = @JsonStr     
   else if type == "I2"      then ret = @JsonNum     
   else if type == "UI2"     then ret = @JsonNum     
   else if type == "I4"      then ret = @JsonNum   
   else if type == "UI4"     then ret = @JsonNum
   else if type == "I8"      then ret = @JsonNum
   else if type == "R4"      then ret = @JsonNum
   else if type == "R8"      then ret = @JsonNum
   else if type == "DECIMAL" then ret = @JsonStr 
   else if type == "CY"      then ret = @JsonStr 
   else if type == "DATE"    then ret = @JsonStr 
   else if type == "BSTR"    then ret = @JsonStr 
   else if type == "ERROR"   then ret = @JsonNull   
   else if type == "BOOL"    then ret = @JsonBool

   return ret

#endFunction

return

There are multiple ways the above script can be optimized.  Using a UDF has the advantage of allowing you to perform the conversion to your needs and preferences.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Nice. And useful. Thanks.