Commit 2f69a735 authored by Evan Martin's avatar Evan Martin Committed by Brad Fitzpatrick

json: encode \r and \n in strings as e.g. "\n", not "\u000A"

This is allowed by the JSON spec and is shorter/easier to read.

R=golang-dev, bradfitz, rsc
CC=golang-dev
https://golang.org/cl/4678046
parent f19b24a1
...@@ -344,10 +344,17 @@ func (e *encodeState) string(s string) { ...@@ -344,10 +344,17 @@ func (e *encodeState) string(s string) {
if start < i { if start < i {
e.WriteString(s[start:i]) e.WriteString(s[start:i])
} }
if b == '\\' || b == '"' { switch b {
case '\\', '"':
e.WriteByte('\\') e.WriteByte('\\')
e.WriteByte(b) e.WriteByte(b)
} else { case '\n':
e.WriteByte('\\')
e.WriteByte('n')
case '\r':
e.WriteByte('\\')
e.WriteByte('r')
default:
e.WriteString(`\u00`) e.WriteString(`\u00`)
e.WriteByte(hex[b>>4]) e.WriteByte(hex[b>>4])
e.WriteByte(hex[b&0xF]) e.WriteByte(hex[b&0xF])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment