mpneuried
1/28/2013 - 3:35 PM

encode json with linebreaks

encode json with linebreaks

o = {
  "ID": "BgQfL",
	"jsonSettings": "{\"weight\":49,\"height\":158,\"heartrate_max\":168,\"phonebusiness\":\"03423/667840\",\"usergroups\":\"Mitglied\",\"colors\":{\"plantargets\":[],\"usergroups\":[],\"pain\":[],\"damage\":[],\"activeplan_targets\":[]},\"comment\":\"Skoliose\r\n\r\nPatientin ist hypermobil\r\n\r\nbd Clavicula gebrochen; links 2011, recht 1997\",\"plantargets\":\"Muskelaufbau,Ausdauer\"}",
	"firstname": "Heike",
	"lastname": "Jüttner"
}
console.log(  o.jsonSettings )

_json = 
	encode: ( s )->
		JSON.parse( s.replace( /\r|\n/g, ( _f, _idx, s  )->
			switch _f 
				when "\n" then "\\\\n"
				when "\r" then "\\\\r"
				when "\t" then "\\\\t"
				else _f
			))

	decode: ( s )->
		JSON.stringify( s ).replace( /\\\\r|\\\\n/g, ( _f, _idx, s  )->
			switch _f 
				when "\\\\n" then "\n"
				when "\\\\r" then "\r"
				when "\\\\t" then "\t"
				else _f
		)


o.jsonSettings_obj = _json.encode( o.jsonSettings )
o.jsonSettings_str = _json.decode( o.jsonSettings_o )


console.log( o )