https://github.com/polytypic/pprint
https://github.com/polytypic/pprint
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/polytypic/pprint
- Owner: polytypic
- License: mit
- Created: 2014-05-05T15:39:52.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-03-02T14:26:29.000Z (almost 8 years ago)
- Last Synced: 2024-11-04T03:36:51.828Z (about 1 year ago)
- Language: F#
- Homepage: http://polytypic.github.io/PPrint/PPrint.html
- Size: 167 KB
- Stars: 3
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
PPrint is a pretty printing combinator library for text documents that can be
rendered to a desired maximum width.
### Docs
* [PPrint reference](http://polytypic.github.io/PPrint/PPrint.html)
* [](https://www.nuget.org/packages/PPrint/) [](https://www.nuget.org/packages/PPrint/)
* [PPrint.Console reference](http://polytypic.github.io/PPrint/PPrint.Console.html)
* [](https://www.nuget.org/packages/PPrint.Console/) [](https://www.nuget.org/packages/PPrint.Console/)
### Background
* [A prettier printer](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)
* [PPrint, a prettier printer](http://research.microsoft.com/en-us/um/people/daan/download/pprint/pprint.html)
### Example
Here is an example of how one could define a function with signature
```fsharp
module Json =
val pretty: JsonValue -> Doc
```
for pretty printing JSON values:
```fsharp
open System.Json
open PPrint
let inline (^) x = x
module Json =
let private prettySeq (l, r) toDoc xs =
if Seq.isEmpty xs
then l <^> r
else let ds = xs |> Seq.map toDoc |> punctuate comma |> vsep
l <..> ds |> nest 2 <..> r |> group
let rec pretty (json: JsonValue) =
match json with
| :? JsonObject as json ->
json
|> prettySeq lrbrace ^ fun kv ->
pretty ^ JsonPrimitive kv.Key <^> colon <+> pretty kv.Value
| :? JsonArray as json ->
json
|> prettySeq lrbracket pretty
| null ->
txt "null"
| _ ->
json
|> string
|> txt
```
Given a JSON value
```fsharp
let json = JsonValue.Parse """
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
"""
```
we can render it to different desired maximum widths. For example,
```fsharp
json |> Json.pretty |> println ^ Some 40
```
produces
```
{
"address": {
"city": "New York",
"postalCode": "10021-3100",
"state": "NY",
"streetAddress": "21 2nd Street"
},
"age": 25,
"children": [],
"firstName": "John",
"isAlive": true,
"lastName": "Smith",
"phoneNumbers": [
{
"number": "212 555-1234",
"type": "home"
},
{
"number": "646 555-4567",
"type": "office"
}
],
"spouse": null
}
```
and
```fsharp
json |> Json.pretty |> println ^ Some 80
```
produces
```
{
"address": {
"city": "New York",
"postalCode": "10021-3100",
"state": "NY",
"streetAddress": "21 2nd Street"
},
"age": 25,
"children": [],
"firstName": "John",
"isAlive": true,
"lastName": "Smith",
"phoneNumbers": [
{"number": "212 555-1234", "type": "home"},
{"number": "646 555-4567", "type": "office"}
],
"spouse": null
}
```