So in the absence of merge functionality i created a clone function.
This is obviously not performant, but ok for small stuff.
Code: Select all
Procedure jsonClone(jSrc, jDest)
Select JSONType(jSrc)
Case #PB_JSON_Null:
SetJSONNull(jDest)
Case #PB_JSON_String:
SetJSONString(jDest, GetJSONString(jSrc))
Case #PB_JSON_Number:
SetJSONDouble(jDest, GetJSONDouble(jSrc))
Case #PB_JSON_Boolean:
SetJSONBoolean(jDest, GetJSONBoolean(jSrc))
Case #PB_JSON_Array:
Protected i, len = JSONArraySize(jSrc)
SetJSONArray(jDest)
For i = 0 To (len - 1)
Protected ja = GetJSONElement(jSrc, i)
Protected ca = AddJSONElement(jDest)
jsonClone(ja, ca)
Next
Case #PB_JSON_Object:
SetJSONObject(jDest)
If ExamineJSONMembers(jSrc)
While NextJSONMember(jSrc)
Protected key.s = JSONMemberKey(jSrc)
Protected jo = JSONMemberValue(jSrc)
Protected co = AddJSONMember(jDest, key)
jsonClone(jo, co)
Wend
EndIf
Default:
EndSelect
EndProcedure
Usage would like this
Code: Select all
top = ParseJSON(#PB_Any, ~"{\"foo\":\"bar\",\"test\":1}")
sub = ParseJSON(#PB_Any, ~"{\"prop1\":\"val1\",\"test1\":1, \"more\":{ \"a\":1, \"b\": \"hi\"}}")
Debug ComposeJSON(top, #PB_JSON_PrettyPrint)
Debug ComposeJSON(sub, #PB_JSON_PrettyPrint)
no = AddJSONMember(JSONValue(top), "sub")
jsonClone(JSONValue(sub), no)
Debug ComposeJSON(top, #PB_JSON_PrettyPrint)
sub2 = ParseJSON(#PB_Any, ~"[\"#1\", 2, {\"three\": true}]")
Debug ComposeJSON(sub2, #PB_JSON_PrettyPrint)
no2 = AddJSONMember(JSONValue(top), "sub2")
jsonClone(JSONValue(sub2), no2)
Debug ComposeJSON(top, #PB_JSON_PrettyPrint)