[racket] Json pretty-print

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Tue Apr 16 14:45:16 EDT 2013

Nick,

I've just built pretty-printing for JSON in a Gist you can see here:

  https://gist.github.com/carl-eastlund/5398440

It's built on top of my own "stylish printing" library, mischief/stylish,
that you can install by running:

  raco pkg install mischief

I'll add this to the mischief package soon, for now you can copy it from
the Gist site and modify as needed.  I don't know if this indents things
the best possible way, but hopefully it's a good start.

Carl Eastlund

On Tue, Apr 16, 2013 at 2:15 PM, Nick Shelley <nickmshelley at gmail.com>wrote:

> I'm pretty sure what I want is the easy thing. We have data represented as
> json, and we want changes to it to be easy to see in a difftool. Everything
> on one line makes this hard. We just have a bunch of nested arrays and
> dictionaries that need to be tabbed and newlined with no regard to width.
>
> As far as order goes, I think Python alphabetizes the keys, but for our
> purposes, order isn't important as long as it's consistent (so we don't get
> diffs that just change the order).
>
> I'm willing to implement it, and I might enjoy learning the process of
> contributing with something as easy as this. But if someone else gets to it
> first because it's easier to just do it right the first time than rewrite
> mine, I wouldn't mind.
>
> Thanks for the responses and sorry for my unclear initial question.
>
>
> On Tue, Apr 16, 2013 at 12:05 PM, Sean McBeth <sean.mcbeth at gmail.com>wrote:
>
>> I've had good luck porting simple Python examples to Racket. Perhaps you
>> could start with the Python code that works as you expect, port it to
>> Racket, then refactor it to more Rackety ways:
>> http://hg.python.org/cpython/file/cc0e72082e52/Lib/json
>>
>>
>> On Tue, Apr 16, 2013 at 1:54 PM, Eli Barzilay <eli at barzilay.org> wrote:
>>
>>> Just now, Nick Shelley wrote:
>>> > In talking with an experienced Racketeer, I realized that
>>> > pretty-print has a specific meaning in Racket that takes width into
>>> > account.
>>>
>>> That's exactly what I thought you were talking about.  Doing it from
>>> scratch seems wrong, and I thought that the right way to do it is to
>>> hook into the pretty-printer that we already have.
>>>
>>>
>>> > What I really want is json with properly placed newlines and tabs
>>> > (or spaces).
>>>
>>> This sounds somewhere between very easy (if all you want is
>>> newlines+indentation up to a specific level) to something more
>>> complicated if you want to control other aspects like spaces around
>>> colons, sort keys in tables, etc.
>>>
>>>
>>> > I'm willing to take a stab at it if it doesn't exist. I just wanted
>>> > to make sure I wasn't missing it somewhere (which happens often with
>>> > me).
>>>
>>> If you can summarize the python-equivalent features in a rackety way,
>>> I can see if it's easy to add them to our printer.
>>>
>>> IIUC, this interface means that `write-json' would get a bunch of
>>> additional keywords, but I'm not sure about it, since in Racket
>>> `write' usually refers to something closer to serializing a value in a
>>> readable way rather than printing it in a readable way.  So maybe the
>>> right thing is to add a `print-json' function even if it duplicates
>>> the functionality of `write-json'.
>>>
>>> --
>>>           ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
>>>                     http://barzilay.org/                   Maze is Life!
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>>
>>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130416/86798073/attachment-0001.html>

Posted on the users mailing list.