SetActiveJSONMember(),CombineJSONMember(),CopyJSON()
Posted: Sun Jul 30, 2017 9:03 pm
Sometimes it is necassary to combine to JSONs, search for a special member by key or copy a json in a new one.
Exampleimplantation for SetActiveJSONMember() and CombineJSONMember() (include value2 in value1)
For Copy create a empty JSON and use it as first Parameter in combineJSONMember()
Exampleimplantation for SetActiveJSONMember() and CombineJSONMember() (include value2 in value1)
For Copy create a empty JSON and use it as first Parameter in combineJSONMember()
Code: Select all
Declare CombineJSONMember(Value1,Value2)
Procedure SetActiveJSONMember(ObjectValue,name.s)
Protected ret
If ExamineJSONMembers(ObjectValue)
While NextJSONMember(ObjectValue)
If JSONMemberKey(ObjectValue)=name
ret=JSONMemberValue(ObjectValue)
Break
EndIf
Wend
EndIf
ProcedureReturn ret
EndProcedure
Procedure _InsertArray(newva,jmv2)
Protected count1,count,i,Element,NewElement
count1=JSONArraySize(newva)
count=JSONArraySize(jmv2)
For i=0 To count-1
Element=GetJSONElement(jmv2,i)
If i<count1
RemoveJSONElement(newva,i)
EndIf
NewElement=AddJSONElement(newva,i)
Select JSONType(element)
Case #PB_JSON_Null
SetJSONNull(NewElement)
Case #PB_JSON_String
SetJSONString(NewElement,GetJSONString(Element))
Case #PB_JSON_Number
SetJSONDouble(NewElement,GetJSONDouble(Element))
Case #PB_JSON_Boolean
SetJSONBoolean(NewElement,GetJSONBoolean(Element))
Case #PB_JSON_Array
SetJSONArray(NewElement)
_InsertArray(NewElement,Element)
Case #PB_JSON_Object
SetJSONObject(NewElement)
CombineJSONMember(NewElement,Element)
EndSelect
Next
EndProcedure
Procedure CombineJSONMember(Value1,Value2)
Protected newva
Protected jmv2
Protected jmk2.s
If ExamineJSONMembers(Value2)
While NextJSONMember(Value2)
jmv2=JSONMemberValue(Value2)
jmk2.s=JSONMemberKey(Value2)
Select JSONType(jmv2)
Case #PB_JSON_Null
newva=AddJSONMember(Value1,jmk2)
SetJSONNull(newva)
Case #PB_JSON_String
newva=AddJSONMember(Value1,jmk2)
SetJSONString(newva,GetJSONString(jmv2))
Case #PB_JSON_Number
newva=AddJSONMember(Value1,jmk2)
SetJSONDouble(newva,GetJSONDouble(jmv2))
Case #PB_JSON_Boolean
newva=AddJSONMember(Value1,jmk2)
SetJSONBoolean(newva,GetJSONBoolean(jmv2))
Case #PB_JSON_Array
newva=SetActiveJSONMember(Value1,jmk2)
If newva=0 Or JSONType(newva)<>#PB_JSON_Array
newva=AddJSONMember(Value1,jmk2)
SetJSONArray(newva)
EndIf
_InsertArray(newva,jmv2)
Case #PB_JSON_Object
newva=SetActiveJSONMember(Value1,jmk2)
If newva=0 Or JSONType(newva)<>#PB_JSON_Object
newva=AddJSONMember(Value1,jmk2)
SetJSONObject(newva)
EndIf
CombineJSONMember(newva,jmv2)
EndSelect
Wend
EndIf
EndProcedure
;-
;- Example
Structure s1
a.i
b.i
EndStructure
Structure t1
a.i
v.s1
EndStructure
Structure s2
a.i
d.s
EndStructure
Structure t2
a.i
c.i
d.i
v.s2
w.s2
List str.s()
Array List.s1(2)
EndStructure
j1=CreateJSON(#PB_Any)
j2=CreateJSON(#PB_Any)
v1.t1
v1\a=10
v1\v\a=20
v1\v\b=30
v2.t2
v2\a=40
v2\c=50
v2\d=60
v2\v\a=70
v2\v\d="String1"
v2\w\a=80
v2\w\d="String2"
AddElement(v2\Str()):v2\Str()="String3"
AddElement(v2\Str()):v2\Str()="String4"
AddElement(v2\Str()):v2\Str()="String5"
For i=0 To 2
v2\List(i)\a=1000*i+100
v2\List(i)\b=1000*i+110
Next
InsertJSONStructure(JSONValue(j1),@v1,t1)
InsertJSONStructure(JSONValue(j2),@v2,t2)
Debug "V1"
Debug ComposeJSON(j1)
Debug "v2"
Debug ComposeJSON(j2)
CombineJSONMember(JSONValue(j1),JSONValue(j2))
Debug "Combine in v1"
Debug ComposeJSON(j1)