1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package json implements encoding and decoding of JSON objects as defined in
8 // See "JSON and Go" for an introduction to this package:
9 // http://blog.golang.org/2011/01/json-and-go.html
24 // Marshal returns the JSON encoding of v.
26 // Marshal traverses the value v recursively.
27 // If an encountered value implements the Marshaler interface,
28 // Marshal calls its MarshalJSON method to produce JSON.
30 // Otherwise, Marshal uses the following type-dependent default encodings:
32 // Boolean values encode as JSON booleans.
34 // Floating point and integer values encode as JSON numbers.
36 // String values encode as JSON strings, with each invalid UTF-8 sequence
37 // replaced by the encoding of the Unicode replacement character U+FFFD.
39 // Array and slice values encode as JSON arrays, except that
40 // []byte encodes as a base64-encoded string.
42 // Struct values encode as JSON objects. Each exported struct field
43 // becomes a member of the object unless the field is empty and its tag
44 // specifies the "omitempty" option. The empty values are false, 0, any
45 // nil pointer or interface value, and any array, slice, map, or string of
46 // length zero. The object's default key string is the struct field name
47 // but can be specified in the struct field's tag value. The "json" key in
48 // struct field's tag value is the key name, followed by an optional comma
49 // and options. Examples:
51 // // Specifies that Field appears in JSON as key "myName"
52 // Field int `json:"myName"`
54 // // Specifies that Field appears in JSON as key "myName" and
55 // // the field is omitted from the object if its value is empty,
56 // // as defined above.
57 // Field int `json:"myName,omitempty"`
59 // // Field appears in JSON as key "Field" (the default), but
60 // // the field is skipped if empty.
61 // // Note the leading comma.
62 // Field int `json:",omitempty"`
64 // The "string" option signals that a field is stored as JSON inside a
65 // JSON-encoded string. This extra level of encoding is sometimes
66 // used when communicating with JavaScript programs:
68 // Int64String int64 `json:",string"`
70 // The key name will be used if it's a non-empty string consisting of
71 // only Unicode letters, digits, dollar signs, hyphens, and underscores.
73 // Map values encode as JSON objects.
74 // The map's key type must be string; the object keys are used directly
77 // Pointer values encode as the value pointed to.
78 // A nil pointer encodes as the null JSON object.
80 // Interface values encode as the value contained in the interface.
81 // A nil interface value encodes as the null JSON object.
83 // Channel, complex, and function values cannot be encoded in JSON.
84 // Attempting to encode such a value causes Marshal to return
85 // an InvalidTypeError.
87 // JSON cannot represent cyclic data structures and Marshal does not
88 // handle them. Passing cyclic structures to Marshal will result in
89 // an infinite recursion.
91 func Marshal(v interface{}) ([]byte, os.Error) {
100 // MarshalIndent is like Marshal but applies Indent to format the output.
101 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, os.Error) {
107 err = Indent(&buf, b, prefix, indent)
111 return buf.Bytes(), nil
114 // MarshalForHTML is like Marshal but applies HTMLEscape to the output.
115 func MarshalForHTML(v interface{}) ([]byte, os.Error) {
122 return buf.Bytes(), nil
125 // HTMLEscape appends to dst the JSON-encoded src with <, >, and &
126 // characters inside string literals changed to \u003c, \u003e, \u0026
127 // so that the JSON will be safe to embed inside HTML <script> tags.
128 // For historical reasons, web browsers don't honor standard HTML
129 // escaping within <script> tags, so an alternative JSON encoding must
131 func HTMLEscape(dst *bytes.Buffer, src []byte) {
132 // < > & can only appear in string literals,
133 // so just scan the string one byte at a time.
135 for i, c := range src {
136 if c == '<' || c == '>' || c == '&' {
138 dst.Write(src[start:i])
140 dst.WriteString(`\u00`)
141 dst.WriteByte(hex[c>>4])
142 dst.WriteByte(hex[c&0xF])
146 if start < len(src) {
147 dst.Write(src[start:])
151 // Marshaler is the interface implemented by objects that
152 // can marshal themselves into valid JSON.
153 type Marshaler interface {
154 MarshalJSON() ([]byte, os.Error)
157 type UnsupportedTypeError struct {
161 func (e *UnsupportedTypeError) String() string {
162 return "json: unsupported type: " + e.Type.String()
165 type InvalidUTF8Error struct {
169 func (e *InvalidUTF8Error) String() string {
170 return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
173 type MarshalerError struct {
178 func (e *MarshalerError) String() string {
179 return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Error.String()
182 type interfaceOrPtrValue interface {
187 var hex = "0123456789abcdef"
189 // An encodeState encodes JSON into a bytes.Buffer.
190 type encodeState struct {
191 bytes.Buffer // accumulated output
194 func (e *encodeState) marshal(v interface{}) (err os.Error) {
196 if r := recover(); r != nil {
197 if _, ok := r.(runtime.Error); ok {
203 e.reflectValue(reflect.ValueOf(v))
207 func (e *encodeState) error(err os.Error) {
211 var byteSliceType = reflect.TypeOf([]byte(nil))
213 func isEmptyValue(v reflect.Value) bool {
215 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
219 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
221 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
223 case reflect.Float32, reflect.Float64:
224 return v.Float() == 0
225 case reflect.Interface, reflect.Ptr:
231 func (e *encodeState) reflectValue(v reflect.Value) {
232 e.reflectValueQuoted(v, false)
235 // reflectValueQuoted writes the value in v to the output.
236 // If quoted is true, the serialization is wrapped in a JSON string.
237 func (e *encodeState) reflectValueQuoted(v reflect.Value, quoted bool) {
239 e.WriteString("null")
243 if j, ok := v.Interface().(Marshaler); ok {
244 b, err := j.MarshalJSON()
246 // copy JSON into buffer, checking validity.
247 err = Compact(&e.Buffer, b)
250 e.error(&MarshalerError{v.Type(), err})
255 writeString := (*encodeState).WriteString
257 writeString = (*encodeState).string
264 writeString(e, "true")
266 writeString(e, "false")
269 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
270 writeString(e, strconv.Itoa64(v.Int()))
272 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
273 writeString(e, strconv.Uitoa64(v.Uint()))
275 case reflect.Float32, reflect.Float64:
276 writeString(e, strconv.FtoaN(v.Float(), 'g', -1, v.Type().Bits()))
280 sb, err := Marshal(v.String())
294 for i := 0; i < n; i++ {
299 tag, omitEmpty, quoted := f.Name, false, false
300 if tv := f.Tag.Get("json"); tv != "" {
301 name, opts := parseTag(tv)
302 if isValidTag(name) {
305 omitEmpty = opts.Contains("omitempty")
306 quoted = opts.Contains("string")
308 fieldValue := v.Field(i)
309 if omitEmpty && isEmptyValue(fieldValue) {
319 e.reflectValueQuoted(fieldValue, quoted)
324 if v.Type().Key().Kind() != reflect.String {
325 e.error(&UnsupportedTypeError{v.Type()})
328 e.WriteString("null")
332 var sv stringValues = v.MapKeys()
334 for i, k := range sv {
340 e.reflectValue(v.MapIndex(k))
344 case reflect.Array, reflect.Slice:
345 if v.Type() == byteSliceType {
347 s := v.Interface().([]byte)
349 // for small buffers, using Encode directly is much faster.
350 dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
351 base64.StdEncoding.Encode(dst, s)
354 // for large buffers, avoid unnecessary extra temporary
356 enc := base64.NewEncoder(base64.StdEncoding, e)
365 for i := 0; i < n; i++ {
369 e.reflectValue(v.Index(i))
373 case reflect.Interface, reflect.Ptr:
375 e.WriteString("null")
378 e.reflectValue(v.Elem())
381 e.error(&UnsupportedTypeError{v.Type()})
386 func isValidTag(s string) bool {
390 for _, c := range s {
391 if c != '$' && c != '-' && c != '_' && !unicode.IsLetter(c) && !unicode.IsDigit(c) {
398 // stringValues is a slice of reflect.Value holding *reflect.StringValue.
399 // It implements the methods to sort by string.
400 type stringValues []reflect.Value
402 func (sv stringValues) Len() int { return len(sv) }
403 func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
404 func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
405 func (sv stringValues) get(i int) string { return sv[i].String() }
407 func (e *encodeState) string(s string) (int, os.Error) {
411 for i := 0; i < len(s); {
412 if b := s[i]; b < utf8.RuneSelf {
413 if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' {
418 e.WriteString(s[start:i])
431 // This encodes bytes < 0x20 except for \n and \r,
432 // as well as < and >. The latter are escaped because they
433 // can lead to security holes when user-controlled strings
434 // are rendered into JSON and served to some browsers.
435 e.WriteString(`\u00`)
436 e.WriteByte(hex[b>>4])
437 e.WriteByte(hex[b&0xF])
443 c, size := utf8.DecodeRuneInString(s[i:])
444 if c == utf8.RuneError && size == 1 {
445 e.error(&InvalidUTF8Error{s})
450 e.WriteString(s[start:])
453 return e.Len() - len0, nil