tags:

views:

41

answers:

2

I want to clean strings that are retrieved from a database.

I ran into this issue where a property value (a name from a database) had an embedded TAB character, and Chrome gave me an invalid TOKEN error while trying to load the JSON object.

So now, I went to http://www.json.org/ and on the side it has a specification. But I'm having trouble understanding how to write a cleanser using this spec:

string

  • ""
  • " chars "

chars

  • char
  • char chars

char

  • any-Unicode-character- except-"-or--or- control-character
  • \"
  • \\
  • \/
  • \b
  • \f
  • \n
  • \r
  • \t
  • \u four-hex-digits

Given a string, how can I "clean" it such that I conform to this spec?

Specifically, I am confused: does the spec allow TAB (0x0900) characters? If so, why did Chrome given an invalid TOKEN error?

+2  A: 

This maybe what you are looking for it shows how to use the JavaScriptSerializer class in C#.

How to create JSON String in C#

Rodney Foley
+1  A: 

Tab characters (actual 0x09, not escapes) cannot appear inside of quotes in JSON (though they are valid whitespace outside of quotes). You'll need to escape them with \t or \u0009 (the former being preferable).

json.org says an unescaped character of a string must be:

Any UNICODE character except " or \ or control character

Tab counts as a control character.

Joey Adams