]> Cypherpunks repositories - gostls13.git/commitdiff
json: encode \r and \n in strings as e.g. "\n", not "\u000A"
authorEvan Martin <evan.martin@gmail.com>
Mon, 11 Jul 2011 14:31:08 +0000 (07:31 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 11 Jul 2011 14:31:08 +0000 (07:31 -0700)
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

src/pkg/json/encode.go

index adc0f0f371ec3cf89bcdbc424f53d395ac8634d1..3e4532cee4ea9ff16c174810428ec62e9bdd809b 100644 (file)
@@ -344,10 +344,17 @@ func (e *encodeState) string(s string) {
                        if start < i {
                                e.WriteString(s[start:i])
                        }
-                       if b == '\\' || b == '"' {
+                       switch b {
+                       case '\\', '"':
                                e.WriteByte('\\')
                                e.WriteByte(b)
-                       } else {
+                       case '\n':
+                               e.WriteByte('\\')
+                               e.WriteByte('n')
+                       case '\r':
+                               e.WriteByte('\\')
+                               e.WriteByte('r')
+                       default:
                                e.WriteString(`\u00`)
                                e.WriteByte(hex[b>>4])
                                e.WriteByte(hex[b&0xF])