Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(38)

Side by Side Diff: src/pkg/encoding/json/encode.go

Issue 5707054: code review 5707054: encoding/json: escape output from Marshalers. (Closed)
Patch Set: diff -r 0e8f0096e631 https://go.googlecode.com/hg/ Created 13 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/pkg/encoding/json/encode_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 The Go Authors. All rights reserved. 1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Package json implements encoding and decoding of JSON objects as defined in 5 // Package json implements encoding and decoding of JSON objects as defined in
6 // RFC 4627. 6 // RFC 4627.
7 // 7 //
8 // See "JSON and Go" for an introduction to this package: 8 // See "JSON and Go" for an introduction to this package:
9 // http://blog.golang.org/2011/01/json-and-go.html 9 // http://blog.golang.org/2011/01/json-and-go.html
10 package json 10 package json
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 if v.Kind() != reflect.Ptr && v.CanAddr() { 257 if v.Kind() != reflect.Ptr && v.CanAddr() {
258 m, ok = v.Addr().Interface().(Marshaler) 258 m, ok = v.Addr().Interface().(Marshaler)
259 if ok { 259 if ok {
260 v = v.Addr() 260 v = v.Addr()
261 } 261 }
262 } 262 }
263 } 263 }
264 if ok && (v.Kind() != reflect.Ptr || !v.IsNil()) { 264 if ok && (v.Kind() != reflect.Ptr || !v.IsNil()) {
265 b, err := m.MarshalJSON() 265 b, err := m.MarshalJSON()
266 if err == nil { 266 if err == nil {
267 // Ensure the JSON is properly escaped.
268 // This could be more efficient.
269 var buf bytes.Buffer
r 2012/02/29 00:34:49 is it worth a call to bytes.IndexAny to avoid the
dsymonds 2012/02/29 00:53:39 It would improve this particular approach, yeah. I
rsc 2012/02/29 15:50:20 I don't want to slow things down noticeably here,
270 HTMLEscape(&buf, b)
271 b = buf.Bytes()
272 }
273 if err == nil {
267 // copy JSON into buffer, checking validity. 274 // copy JSON into buffer, checking validity.
268 err = Compact(&e.Buffer, b) 275 err = Compact(&e.Buffer, b)
269 } 276 }
270 if err != nil { 277 if err != nil {
271 e.error(&MarshalerError{v.Type(), err}) 278 e.error(&MarshalerError{v.Type(), err})
272 } 279 }
273 return 280 return
274 } 281 }
275 282
276 writeString := (*encodeState).WriteString 283 writeString := (*encodeState).WriteString
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 ef.tag = name 553 ef.tag = name
547 } 554 }
548 ef.omitEmpty = opts.Contains("omitempty") 555 ef.omitEmpty = opts.Contains("omitempty")
549 ef.quoted = opts.Contains("string") 556 ef.quoted = opts.Contains("string")
550 } 557 }
551 fs = append(fs, ef) 558 fs = append(fs, ef)
552 } 559 }
553 encodeFieldsCache[t] = fs 560 encodeFieldsCache[t] = fs
554 return fs 561 return fs
555 } 562 }
OLDNEW
« no previous file with comments | « no previous file | src/pkg/encoding/json/encode_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b