11
2
Jason has a big JSON but it's unreadable, so he needs to prettify it.
Formatting Spec
The JSON has 4 different types:
- Numbers; Just
0-9
- Strings; Double quoted
"
strings escaped with\
- Arrays; Delimited by
[]
, with items separated by,
, items can be any of these types - Objects; Delimited by
{}
, format iskey: value
where key is a string and value is any of these types
Spacing
- Arrays should have exactly one space after the commas between items
- Objects should have have just one space between the key and the value, after the
:
Indentation
- Each nesting level is indented 2 more than the previous
- Each object key/value pair is always on its own line. Objects are indented
- An array is indented across multiple lines if it contains another array or object. Otherwise the array remains on one line
Rules
- Built-ins which trivialize this task are not allowed.
- As always standard loopholes are disallowed
Examples
[1,2,3]
[1, 2, 3]
{"a":1,"b":4}
{
"a": 1,
"b": 4
}
"foo"
"foo"
56
56
{"a":[{"b":1,"c":"foo"},{"d":[2,3,4,1], "a":["abc","def",{"d":{"f":[3,4]}}]}]}
{
"a": [
{
"b": 1,
"c": "foo"
},
{
"d": [2, 3, 4, 1],
"a": [
"abc",
"def",
{
"d": {
"f": [3, 4]
}
}
]
}
]
}
[2,["foo123 ' bar \" baz\\", [1,2,3]]]
[
2,
[
"foo123 ' bar \" baz\\",
[1, 2, 3]
]
]
[1,2,3,"4[4,5]"]
[1, 2, 3, "4[4,5]"]
[1,2,3,{"b":["{\"c\":[2,5,6]}",4,5]}]
[
1,
2,
3,
{
"b": ["{\"c\":[2,5,6]}", 4, 5]
}
]
1Are JSON parsing builtins allowed? – PurkkaKoodari – 2016-03-01T07:45:22.030
Can objects/arrays be empty? Can we still print a space after commas in arrays if they split over multiple lines? – Martin Ender – 2016-03-01T08:40:11.237
@MartinBüttner no, and yes – Downgoat – 2016-03-01T14:59:26.487
@Pietu1998 hm, I'm going to say no – Downgoat – 2016-03-01T14:59:58.797
Are language parser languages allowed? – Mama Fun Roll – 2016-03-01T16:27:46.860
@ӍѲꝆΛҐӍΛПҒЦꝆ if it is a built in hat is used to parse a JSON then no, because it's a JSON parsing built in – Downgoat – 2016-03-01T22:46:50.513
Re Numbers; Just
0-9
— JSON actually allows floating-pointing numbers as well as negative. Can we safely assume the input won’t contain any of those? – Timwi – 2016-03-06T21:50:20.880@Timwi yeah, you can assume that. – Downgoat – 2016-03-06T22:01:08.373