Page 1 of 1

JSON helpers

Posted: Sun May 10, 2015 12:33 am
by eddy

Code: Select all

; ********************
; JSON Shorthand
; ********************
  
Macro Json_Object
  !{
  :
EndMacro
Macro Json_EndObject
  !},  
EndMacro

Macro Json_Array
  ![
  :
EndMacro
Macro Json_EndArray
  !],  
EndMacro

Macro Json_Member(member)
  !member:
  : 
EndMacro
Macro Json_Value(value)
  !value,
  : 
EndMacro
Macro Json_Member_Value(member, value)
  Json_Member(member)
  Json_Value(value)
EndMacro
Macro Json_Member_Object(member)
  Json_Member(member)
  Json_Object
EndMacro
Macro Json_Member_Array(member)
  Json_Member(member)
  Json_Array
EndMacro

Macro Json_Variable(v)
  !v={
  :
EndMacro
Macro Json_EndVariable
  !};
  :
EndMacro

CompilerIf #PB_Compiler_IsMainFile
  ; ********************
  ; EXAMPLE
  ; ********************
  
  Define CustomButton
  Json_Variable(v_custombutton)
    Json_Member_Value("gadget.type","button")
    Json_Member_Object(text)
      Json_Member_Value('color','#FFCC00')
      Json_Member_Value(fontSize,24)
    Json_EndObject
    Json_Member_Array("todoList, coord")
      Json_Value(10)
      Json_Value(20)
      Json_Object Json_Member_Value("position","234px, 100px") Json_EndObject
      Json_Object Json_Member_Value("distance","1000km") Json_EndObject
    Json_EndArray
  Json_EndVariable
  Debug CustomButton
  !console.log(v_custombutton);
  !alert("v_custombutton['gadget.type']="+v_custombutton["gadget.type"]+"\nv_custombutton.text.color="+v_custombutton.text.color);
  
  !console.log({
    Json_Member("day")   Json_Value(20)
    Json_Member("month") Json_Value(10)
    Json_Member("year")  Json_Value(2015)
    Json_Member("holidays")
    Json_Object
      Json_Member("days off") Json_Value(20)
      Json_Member("special")
      Json_Array
        Json_Value("Xmas")
        Json_Value("New Year Eve")
      Json_EndArray
    Json_EndObject
  !});
  
CompilerEndIf

Re: JSON shorthands

Posted: Wed May 13, 2015 12:34 pm
by Fred
I guess the built-in JSON lib is too verbose ? Do you have some imporvements to suggest to improve it ?

Re: JSON shorthands

Posted: Thu May 14, 2015 6:50 pm
by eddy
Fred wrote:I guess the built-in JSON lib is too verbose ? Do you have some imporvements to suggest to improve it ?
Yeah, I wrote a lot of inlined javascript lines because of JSON.