JSON Functions

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
4 posts • Page 1 of 1
RudiKreiner
Posts:198
Joined: Mon May 16, 2011 2:10 pm

JSON Functions

Post by RudiKreiner »

I am writing a control script application with data exchange in files with JSON formatting.
I see that there are new functions jsonEncode and jsonDecode in V3.14 but I have the following problems when working with them:
1/ The strings that jsonEncode returns have no new lines in them which makes the generated files difficult to read.
2/ When passing a mapping to jsonEncode, the returned string always puts the elements in alphabetical order.
This is probably due to the mapping object so it would be nice to be able to pass a dyn_dyn_string with the entries sorted in the order that I want them to appear in the file.
3/ When I put the string returned by jsonEncode into a mapping as part of nested file construction for the jsonEncode function, then it adds a \\ before each " character in the string,

Here is an example script that shows the problems described above:
main()
{
dyn_mapping dm;
dm[1]["side"]= "Both";
dm[1]["type"]= "Green";
dm[2]["side"]= "Inside";
dm[2]["type"]= "Blue";
dm[3]["side"]= "Outside";
dm[3]["type"]= "Red";

file f = fopen("test9a.txt", "w");
fputs(jsonEncode(dm), f);
fclose(f);

dyn_mapping dm2;
dm2[1]["size"] = 7766;
dm2[2]["colors"] = jsonEncode(dm);
dm2[3]["weight"] = 123.4;

file f = fopen("test9b.txt", "w");
fputs(jsonEncode(dm2), f);
fclose(f);
}


and here is the resulting output
test9a.txt:
[{"side":"Both","type":"Green"},{"side":"Inside","type":"Blue"},{"side":"Outside","type":"Red"}]
test9b.txt
[{"size":7766},{"colors":"[{\\"side\\":\\"Both\\",\\"type\\":\\"Green\\"},{\\"side\\":\\"Inside\\",\\"type\\":\\"Blue\\"},{\\"side\\":\\"Outside\\",\\"type\\":\\"Red\\"}]"},{"weight":123.40000000000001}]

Are there plans to add additional functionality to the JSON functions in the future, to make them more useable

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: JSON Functions

Post by mkoller »

ad 1) In 3.15 jsonEncode() has a new optional bool argument which defines whether to use the default compact format or the Indented human readable format.

ad 2) JSON defines an object to be an unordered set of key/value pairs. Therefore you should never rely on any order. If you need an ordered list, use an array inside JSON

ad 3) The backslash characters are needed to know where a string ends or if a quote is a character inside a string. See http://json.org/

ad "Are there plans to add additional functionality to the JSON functions in the future, to make them more useable")
What are you missing ?

RudiKreiner
Posts:198
Joined: Mon May 16, 2011 2:10 pm

Re: JSON Functions

Post by RudiKreiner »

ad 1) the human readable option should help us and I would expect the output to look something like in the attached file then.
ad 2) I talked to the guys who read the files, they also use a public library for that and say the the order is irrelevant so I should be OK with that
ad 3) Could be that the backslashes they don't cause any problems either, we'll have to try that out.

I'm just starting to work with this JSON format so I'm not sure yet if we need any further functionality than the human readable format that you have added.
If we find something that we think is missing, I will let you know asap.
We definitely don't want or need anything that more than is described in the rules at json.org

For more complex output, I sure that I will need to nest the JSCON functions to generate the files.
I'm hoping the backslashes that they introduce don't cause any problems, but am not so sure about the quotation marks in lines 4 and 17 of the attached file.
If they are conform to the rules at json.org then they should cause no problems. Again, we will have to test that. Or do you have any tips for me on how to program this nesting? https://www.winccoa.com/fileadmin/image ... test9b.txt

Since the attached file is not readable, here are the contents:

[
{"size":7766},
{"colors":
"[
{
\\"side\\":\\"Both\\",
\\"type\\":\\"Green\\"
},
{
\\"side\\":\\"Inside\\",
\\"type\\":\\"Blue\\"
},
{
\\"side\\":\\"Outside\\",
\\"type\\":\\"Red\\"
}
]"
},
{
"weight":123.40000000000001
}
]

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: JSON Functions

Post by mkoller »

For whatever reason, I can not download your file (not found).

JSON can nest arrays and objects, just use our mapping or dyn_anytype classes.
You don't need to do anything special here.

P.S.:We are using Qt internally for JSON. See http://doc.qt.io/archives/qt-5.5/qjsondocument.html

4 posts • Page 1 of 1