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

Comments

  1. The funny thing is - just by glancing over the code before posting this, I spotted a couple of bugs...

    ReplyDelete
  2. I usually find that stuff after posting.

    ReplyDelete
  3. Or 2 moths after deployment. Ironically i "find/spot" them before the ~5000 users even gets close :)

    ReplyDelete
  4. 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.

    "L\u00F3pez" // <-- yours (PrettyPrint)
    "López" // <-- theirs (TJson.Format)

    ReplyDelete
  5. 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" :-)

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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.

    ReplyDelete
  8. for debugging purposes it's perfect, thx for sharing

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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.

    ReplyDelete

Post a Comment