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 😉
The funny thing is – just by glancing over the code before posting this, I spotted a couple of bugs…
LikeLike
I usually find that stuff after posting.
LikeLike
Or 2 moths after deployment. Ironically i “find/spot” them before the ~5000 users even gets close 🙂
LikeLike
I have tested PrettyFormat as a replacement for this:
function 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.
“Lu00F3pez” // <– yours (PrettyPrint)
“López” // <– theirs (TJson.Format)
LikeLike
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” 🙂
LikeLike
Gustav Schubert I don’t unescape those, but that could be an improvement.
Edit: it would be pretty straightforward, actually, but I prefer to see exactly what we send to the client.
LikeLike
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.
LikeLike
for debugging purposes it’s perfect, thx for sharing
LikeLike
Lars 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
LikeLike
Russell Weetch I use RTTI and Rest.Json.
aJsonString := 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.
LikeLike