boten Active Member Licensed User Longtime User Dec 5, 2015 #1 this is the code, a very simple map, each value is a type Code: Copy Sub Process_Globals Dim mp As Map Type tp(a As String,z As String) End Sub Sub AppStart (Form1 As Form, Args() As String) mp.Initialize Dim t As tp t.Initialize t.a="aaa" t.z="AAA" mp.Put(1,t) Dim t As tp t.Initialize t.a="bbb" t.z="BBB" mp.Put(2,t) Dim t As tp t.Initialize t.a="ccc" t.z="CCC" mp.Put(3,t) Dim js As JSONGenerator Dim s As String js.Initialize(mp) s=js.ToPrettyString(3) Log(s) Dim msg As Msgboxes msg.Show("done","") End Sub the log output is: Code: Copy Program started. { "1": "[IsInitialized=true, a=aaa, z=AAA\n]", "2": "[IsInitialized=true, a=bbb, z=BBB\n]", "3": "[IsInitialized=true, a=ccc, z=CCC\n]" } Why the Isinitialize appears, and how to get rid of it? Why the "\n" and how to get rid of it?
this is the code, a very simple map, each value is a type Code: Copy Sub Process_Globals Dim mp As Map Type tp(a As String,z As String) End Sub Sub AppStart (Form1 As Form, Args() As String) mp.Initialize Dim t As tp t.Initialize t.a="aaa" t.z="AAA" mp.Put(1,t) Dim t As tp t.Initialize t.a="bbb" t.z="BBB" mp.Put(2,t) Dim t As tp t.Initialize t.a="ccc" t.z="CCC" mp.Put(3,t) Dim js As JSONGenerator Dim s As String js.Initialize(mp) s=js.ToPrettyString(3) Log(s) Dim msg As Msgboxes msg.Show("done","") End Sub the log output is: Code: Copy Program started. { "1": "[IsInitialized=true, a=aaa, z=AAA\n]", "2": "[IsInitialized=true, a=bbb, z=BBB\n]", "3": "[IsInitialized=true, a=ccc, z=CCC\n]" } Why the Isinitialize appears, and how to get rid of it? Why the "\n" and how to get rid of it?
rwblinn Well-Known Member Licensed User Longtime User Dec 5, 2015 #2 Hi, do not think JSON can handle types as it requires strings. What output do you want? You can test JSON formats by using the JSON Online Parser - it will provide example code and outputs. Upvote 0
Hi, do not think JSON can handle types as it requires strings. What output do you want? You can test JSON formats by using the JSON Online Parser - it will provide example code and outputs.
S sorex Expert Licensed User Longtime User Dec 5, 2015 #3 that's normal behaviour I guess as the isInitialized is a property value just like the other tho. you can always write your own json string builder to get rid of it if that jsongenerator doesn't have a way to add items one by one. Upvote 0
that's normal behaviour I guess as the isInitialized is a property value just like the other tho. you can always write your own json string builder to get rid of it if that jsongenerator doesn't have a way to add items one by one.
R Roycefer Well-Known Member Licensed User Longtime User Dec 5, 2015 #4 It is often convenient to write one's own ToJSONString() method for custom classes and types. This is especially easy for a simple type like yours: Code: Copy Sub tpToJSONString(teepee as tp) As String Return "{a:" & teepee.a & ",z:" & teepee.z & "}" End Sub Then, while building your larger JSON String, call Code: Copy mp.Put(1,tpToJSONString(t)) 'instead of 'mp.Put(1,t) Upvote 0
It is often convenient to write one's own ToJSONString() method for custom classes and types. This is especially easy for a simple type like yours: Code: Copy Sub tpToJSONString(teepee as tp) As String Return "{a:" & teepee.a & ",z:" & teepee.z & "}" End Sub Then, while building your larger JSON String, call Code: Copy mp.Put(1,tpToJSONString(t)) 'instead of 'mp.Put(1,t)
Erel B4X founder Staff member Licensed User Longtime User Dec 6, 2015 #5 If you want to serialize custom types with json then you should create a sub that converts the type to a Map: Code: Copy Sub TpToMap(teepee As TP) As Map Return CreateMap("a": teepe.a, "b": teepe.b) End Sub Now you can convert the types to json with: Code: Copy Dim t1, t2, t3 As TP ... fill the types Dim jg As JsonGenerator jg.Initialize(CreateMap(1: TpToMap(t1), 2: TpToMap(t2), 3: TpToMap(t3))) Upvote 0
If you want to serialize custom types with json then you should create a sub that converts the type to a Map: Code: Copy Sub TpToMap(teepee As TP) As Map Return CreateMap("a": teepe.a, "b": teepe.b) End Sub Now you can convert the types to json with: Code: Copy Dim t1, t2, t3 As TP ... fill the types Dim jg As JsonGenerator jg.Initialize(CreateMap(1: TpToMap(t1), 2: TpToMap(t2), 3: TpToMap(t3)))
boten Active Member Licensed User Longtime User Dec 6, 2015 #6 So essentially for the json at question - create map of maps Erel said: If you want to serialize custom types with json then you should create a sub that converts the type to a Map: Code: Copy Sub TpToMap(teepee As TP) As Map Return CreateMap("a": teepe.a, "b": teepe.b) End Sub Now you can convert the types to json with: Code: Copy Dim t1, t2, t3 As TP ... fill the types Dim jg As JsonGenerator jg.Initialize(CreateMap(1: TpToMap(t1), 2: TpToMap(t2), 3: TpToMap(t3))) Click to expand... Upvote 0
So essentially for the json at question - create map of maps Erel said: If you want to serialize custom types with json then you should create a sub that converts the type to a Map: Code: Copy Sub TpToMap(teepee As TP) As Map Return CreateMap("a": teepe.a, "b": teepe.b) End Sub Now you can convert the types to json with: Code: Copy Dim t1, t2, t3 As TP ... fill the types Dim jg As JsonGenerator jg.Initialize(CreateMap(1: TpToMap(t1), 2: TpToMap(t2), 3: TpToMap(t3))) Click to expand...