Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON escape space characters

How would I escape space characters in a JSON string? Basically my problem is that I've gotten into a situation where the program that reads the string can use HTML tags for formatting, but I need to be able to use these HTML tags without adding more spaces to the string. so things like

<u>text</u>

is fine, for adding underline formatting

but something like

<font size="14">text</font>

is not fine, because the <font> tag with the size attribute adds an extra space to the string. I know, funny criteria, but at this point thats what has happened.

My first speculative solution would be to have some kind of \escape character that JSON can put in between font and size that will solve my "space" problems, something that the HTML will ignore but leave the human readable string in the code without actual spaces.

 ex. <font\&size="14">text</font>

displays as: text

kind of like &nbsp; but better?

any solutions?

like image 331
CQM Avatar asked May 19 '11 22:05

CQM


People also ask

What characters should be escaped in JSON?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.

How do you escape space in JSON?

You can use \u0020 to escape the ' ' character in JSON.

How do I escape a Unicode character in JSON?

Escapes characters of a UTF-8 encoded Unicode string using JSON-style escape sequences. The escaping rules are as follows, in priority order: If the code point is the double quote (0x22), it is escaped as \" (backslash double quote). If the code point is the backslash (0x5C), it is escaped as \\ (double backslash).


1 Answers

You can use \u0020 to escape the ' ' character in JSON.

like image 75
carlosfigueira Avatar answered Sep 27 '22 21:09

carlosfigueira