I wrote a function to make JSON more readable, and naturally I wonder if I missed something crucial so that it eventually will break?
I wrote a function to make JSON more readable, and naturally I wonder if I missed something crucial so that it eventually will break?
There are probably more efficient ways to work with strings than concatenating character by character, but performance was not a key goal.
I hereby commit it to public code review ;)
https://pastebin.com/Juks92Y2
https://pastebin.com/Juks92Y2
There are probably more efficient ways to work with strings than concatenating character by character, but performance was not a key goal.
I hereby commit it to public code review ;)
https://pastebin.com/Juks92Y2
https://pastebin.com/Juks92Y2
The funny thing is - just by glancing over the code before posting this, I spotted a couple of bugs...
ReplyDeleteI usually find that stuff after posting.
ReplyDeleteOr 2 moths after deployment. Ironically i "find/spot" them before the ~5000 users even gets close :)
ReplyDeleteI have tested PrettyFormat as a replacement for this:
ReplyDeletefunction JsonInfo.FormatJSON(const s: string): string;
var
tmp: TJsonValue;
begin
tmp := TJSONObject.ParseJSONValue(s);
Result := TJson.Format(tmp);
tmp.Free;
end;
PrettyFormat looks better!
(The array brackets are now properly aligned.)
"But", the international characters are 'presented' differently.
"L\u00F3pez" // <-- yours (PrettyPrint)
"López" // <-- theirs (TJson.Format)
all there in XSuperObject. By the way the JSON plugin for NotePad++ is pretty good too. But as they say "telling a programmer there is already a library for that is like telling a songwriter there is already a few songs about love" :-)
ReplyDeleteGustav Schubert I don't unescape those, but that could be an improvement.
ReplyDeleteEdit: it would be pretty straightforward, actually, but I prefer to see exactly what we send to the client.
Russell Weetch We don't use XSuperObject. This was written to make the JsonRPC requests / responses reasonably readable in the log viewer. It is pretty dumb, as it has no perception of hierarchy and treats objects and arrays the same way.
ReplyDeletefor debugging purposes it's perfect, thx for sharing
ReplyDeleteLars Fosdal I mostly use it for building JQuery structures as it had the Raw property which is needed. I do like the interface approach ( based on Superobject). It would be good to have a standard interface that implementations could fulfil
ReplyDeleteRussell Weetch I use RTTI and Rest.Json.
ReplyDeleteaJsonString := TJson.ObjectToJsonString(MyObject,
[joIgnoreEmptyStrings, joIgnoreEmptyArrays, joDateIsUTC, joDateFormatISO8601]);
MyObject := TJson.JsonToObject(aJsonString);
I am told that it is slow, but it seems fast enough for our purposes.