PureBasic
PureBasic
Firebase JSON Put and Patch
See more JSON Examples
Demonstrates how to apply Firebase put and patch events to a JSON database.Chilkat PureBasic Downloads
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
json1.s = "{" + Chr(34) + "a" + Chr(34) + ": 1, " + Chr(34) + "b" + Chr(34) + ": 2}"
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Use Firebase delimiters for JSON paths.
CkJsonObject::setCkDelimiterChar(json, "/")
CkJsonObject::ckLoad(json,json1)
CkJsonObject::ckFirebasePut(json,"/c","{" + Chr(34) + "foo" + Chr(34) + ": true, " + Chr(34) + "bar" + Chr(34) + ": false}")
; Output should be: {"a":1,"b":2,"c":{"foo":true,"bar":false}}
Debug "1) " + CkJsonObject::ckEmit(json)
CkJsonObject::ckFirebasePut(json,"/c",Chr(34) + "hello world" + Chr(34))
; Output should be: {"a":1,"b":2,"c":"hello world"}
Debug "2) " + CkJsonObject::ckEmit(json)
CkJsonObject::ckFirebasePut(json,"/c","{" + Chr(34) + "foo" + Chr(34) + ": " + Chr(34) + "abc" + Chr(34) + ", " + Chr(34) + "bar" + Chr(34) + ": 123}")
; Output should be: {"a":1,"b":2,"c":{"foo":"abc","bar":123}}
Debug "3) " + CkJsonObject::ckEmit(json)
; Back to the original..
CkJsonObject::ckFirebasePut(json,"/","{" + Chr(34) + "a" + Chr(34) + ": 1, " + Chr(34) + "b" + Chr(34) + ": 2}")
Debug "4) " + CkJsonObject::ckEmit(json)
CkJsonObject::ckFirebasePut(json,"/c","{" + Chr(34) + "foo" + Chr(34) + ": true, " + Chr(34) + "bar" + Chr(34) + ": false}")
CkJsonObject::ckFirebasePatch(json,"/c","{" + Chr(34) + "foo" + Chr(34) + ": 3, " + Chr(34) + "baz" + Chr(34) + ": 4}")
; Output should be: {"a":1,"b":2,"c":{"foo":3,"bar":false,"baz":4}}
Debug "5) " + CkJsonObject::ckEmit(json)
CkJsonObject::ckFirebasePatch(json,"/c","{" + Chr(34) + "foo" + Chr(34) + ": " + Chr(34) + "abc123" + Chr(34) + ", " + Chr(34) + "baz" + Chr(34) + ": {" + Chr(34) + "foo" + Chr(34) + ": true, " + Chr(34) + "bar" + Chr(34) + ": false}, " + Chr(34) + "bax" + Chr(34) + ": {" + Chr(34) + "foo" + Chr(34) + ": 200, " + Chr(34) + "bar" + Chr(34) + ": 400} }")
; Output should be: {"a":1,"b":2,"c":{"foo":"abc123","bar":false,"baz":{"foo":true,"bar":false},"bax":{"foo":200,"bar":400}}}
Debug "6) " + CkJsonObject::ckEmit(json)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndProcedure