Skip to content

Instantly share code, notes, and snippets.

@mpneuried
Created January 28, 2013 15:35
Show Gist options
  • Save mpneuried/4656513 to your computer and use it in GitHub Desktop.
Save mpneuried/4656513 to your computer and use it in GitHub Desktop.
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 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment