On Thu, Jun 11, 2015 at 8:10 AM, Devin Jeanpierre <jeanpierr...@gmail.com> wrote: > The problem is that there are two different ways repr might write out > a dict equal to {'a': 1, 'b': 2}. This can make tests brittle -- e.g. > it's why doctest fails badly at examples involving dictionaries. Text > format protocol buffers output everything sorted, so that you can do > textual diffs for compatibility tests and such.
With Python's JSON module [1], you can pass sort_keys=True to stipulate that the keys be lexically ordered, which should make the output "canonical". Pike's Standards.JSON.encode() [2] can take a flag value to canonicalize the output, which currently has the same effect (sort mappings by their indices). I did a quick check for Ruby and didn't find anything in its standard library JSON module, but knowing Ruby, it'll be available somewhere in a gem. A web search for 'perl json' brought up a CPAN link [4] that has a canonicalize option for sorting by keys. So that's three out of four definite, one uncertain, where it's pretty easy to ensure that you get byte-for-byte identical output from a JSON encoder. Even though failing doctests are a separate problem, it's useful to have canonical output. Your diffs get less noisy, for instance. Coupled with a human-readability flag (eg "indent=4" in Python, "Standards.JSON.HUMAN_READABLE" in Pike) that splits the result over multiple lines, it can make a pretty easy to diff file. Definitely worth doing... and definitely worth using a JSON encoder rather than repr(). ChrisA [1] https://docs.python.org/3/library/json.html#json.dump [2] http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/Standards/JSON.html [3] http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html [4] http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm#PERL_-%3E_JSON -- https://mail.python.org/mailman/listinfo/python-list