Skip to content

Commit 68d3b6e

Browse files
skibaarsc
authored andcommitted
Handle \r as a whitespace when parsing JSON string.
Fixes #272. R=rsc https://golang.org/cl/161061
1 parent c78710f commit 68d3b6e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/pkg/json/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func punct(c byte) bool {
198198
return c == '"' || c == '[' || c == ']' || c == ':' || c == '{' || c == '}' || c == ','
199199
}
200200

201-
func white(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\v' }
201+
func white(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' }
202202

203203
func skipwhite(p string, i int) int {
204204
for i < len(p) && white(p[i]) {

src/pkg/json/struct_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ func check(t *testing.T, ok bool, name string, v interface{}) {
6666
}
6767
}
6868

69+
const whiteSpaceEncoded = " \t{\n\"s\"\r:\"string\"\v}"
70+
71+
func TestUnmarshalWhitespace(t *testing.T) {
72+
var m myStruct;
73+
ok, errtok := Unmarshal(whiteSpaceEncoded, &m);
74+
if !ok {
75+
t.Fatalf("Unmarshal failed near %s", errtok)
76+
}
77+
check(t, m.S == "string", "string", m.S);
78+
}
79+
6980
func TestUnmarshal(t *testing.T) {
7081
var m myStruct;
7182
m.F = true;

0 commit comments

Comments
 (0)