27
May
2006

JSON - better readable output

This is a modified version of JSON in JavaScript which adds an option to pretty print the JSON output.

Download: JSON-js-prettyPrint (js, 5 KB)

  object.toJSONString(prettyPrint)
  array.toJSONString(prettyPrint)
    prettyPrint ... if set to true the resulting string will be formated
                    with tabs and returns to be more human readable.
Here is an example data object:

   var data = {
      "eins" : "Eins",
      "int" : 2,
      "number" : 2.234,
      "obj" : {
         "a" : "A", b:"B", c:"C"
      },
      "array" : ["eins","zwei","drei",{"vier":4,"null":null,"undefined":undefined},{"vier":4,"null":null,"undefined":undefined},"zwei","drei",{"vier":4,"null":null,"undefined":undefined}],
      isBoolean : true
   };
data.toJSONString() will ouput:

{"array":["eins","zwei","drei",{"vier":4,"null":null},{"vier":4,"null":null},"zwei","drei",{"vier":4,"null":null}],"eins":"Eins","obj":{"a":"A","b":"B","c":"C"},"number":2.234,"isBoolean":true,"int":2}
data.toJSONString(true) will ouput:

{
	"array" : [
		"eins",
		"zwei",
		"drei",
		{
			"vier" : 4,
			"null" : null
		},
		{
			"vier" : 4,
			"null" : null
		},
		"zwei",
		"drei",
		{
			"vier" : 4,
			"null" : null
		}
	],
	"eins" : "Eins",
	"obj" : {
		"a" : "A",
		"b" : "B",
		"c" : "C"
	},
	"number" : 2.234,
	"isBoolean" : true,
	"int" : 2
}
why i did it?
on the client side it doesn't seem to make sense to pretty print the JSON output, because in most cases you will send it back to a server. but we use the script on the server side (using Helma/Rhino) so it makes perfectly sense for us.

Trackback URL:
http://www.matsblog.com/stories/2075888/modTrackback

Typo?

Did you mean "INDENT" when you wrote "INTEND" in the source? Also, it's dangerous to modify the base Object.prototype because it makes for ... in loops behave badly. I'm going to mix your modifications in with a version of JSON that doesn't modify Object.prototype and I'll post the source on my blog. Awesome work!

logo

Mat's Blog

California Dreaming

Users Status

You are not logged in.

Search

 

Weather

Aktuelles Wetter in Kitzbuhel:


Temperatur: -3 C
UV Index: 0
Luftfeuchte: 100 %
Sichtweite: 10.0 km
Luftdruck: 1019.0 mb
Windstärke: 3 km/h

Weather data provided by weather.com

Get Firefox

Get Firefox!

Currently Reading


William Gibson, Jarreth Merz, Matthias Scherwenikas
Neuromancer, 3 Audio-CDs

Archive

May 2006
Sun
Mon
Tue
Wed
Thu
Fri
Sat
 
 1 
 2 
 6 
 7 
 9 
10
11
12
13
14
15
16
18
19
20
21
22
24
25
26
28
29
31
 
 
 
 

Recent Updates

das verpasse ich leider......
das verpasse ich leider... http://twitter.com/flog/st atus/998712029
flog - 10. Nov, 21:38
bin schon sehr gespannt!...
bin schon sehr gespannt! bis dann
barbarao - 10. Nov, 15:59
hab mich auch gerade...
hab mich auch gerade angemeldet ;)
barbarao - 26. Aug, 10:57
minute 33 is auch sehr...
minute 33 is auch sehr interessant.
MasterOfDesaster - 7. Jul, 16:00
ja der Vortrag ist sehr...
ja der Vortrag ist sehr nett und zum testen kann man...
niepi - 4. Jul, 13:14
BTW: TrimJunction isn't...
BTW: TrimJunction isn't the Rhino port of Rails which...
robert42 - 27. Jun, 16:59
danke :)
danke :)
matthias - 5. Jun, 08:41
up again...magic ;)
up again...magic ;)
MasterOfDesaster - 3. Jun, 10:43

Status

Online for 1497 days
Last update: 10. Nov, 21:38

Credits

Knallgrau New Media Solutions - Web Agentur für neue Medien

powered by Antville powered by Helma


Creative Commons License

xml version of this page

twoday.net AGB