-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoder.go
51 lines (45 loc) · 1.28 KB
/
encoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package jzon
// Encoder is almost standard library compatible
// The following standard methods are not implemented
// - SetIndent
type Encoder struct {
s *Streamer
err error
}
// Release encoder, encoder should not be reused after call
func (e *Encoder) Release() {
e.s.Release()
e.s = nil
}
func encodeInternal(s *Streamer, v interface{}) error {
if err := s.Value(v).Flush(); err != nil {
return err
}
_, err := s.writer.Write([]byte{'\n'})
if err != nil {
return err
}
s.Reset(s.writer)
return nil
}
// Encode writes the JSON encoding of v to the stream,
// followed by a newline character.
func (e *Encoder) Encode(v interface{}) error {
if e.err == nil {
e.err = encodeInternal(e.s, v)
}
return e.err
}
// SetEscapeHTML specifies whether problematic HTML characters
// should be escaped inside JSON quoted strings.
// The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e
// to avoid certain safety problems that can arise when embedding JSON in HTML.
//
// In non-HTML settings where the escaping interferes with the readability
// of the output, SetEscapeHTML(false) disables this behavior.
func (e *Encoder) SetEscapeHTML(on bool) {
e.s.EscapeHTML(on)
}
// func (e *Encoder) SetIndent(prefix, indent string) {
// e.s.SetIndent(prefix, indent)
// }