OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [json/] [encode.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
// Package json implements encoding and decoding of JSON objects as defined in
6
// RFC 4627.
7
//
8
// See "JSON and Go" for an introduction to this package:
9
// http://blog.golang.org/2011/01/json-and-go.html
10
package json
11
 
12
import (
13
        "bytes"
14
        "encoding/base64"
15
        "math"
16
        "reflect"
17
        "runtime"
18
        "sort"
19
        "strconv"
20
        "sync"
21
        "unicode"
22
        "unicode/utf8"
23
)
24
 
25
// Marshal returns the JSON encoding of v.
26
//
27
// Marshal traverses the value v recursively.
28
// If an encountered value implements the Marshaler interface
29
// and is not a nil pointer, Marshal calls its MarshalJSON method
30
// to produce JSON.  The nil pointer exception is not strictly necessary
31
// but mimics a similar, necessary exception in the behavior of
32
// UnmarshalJSON.
33
//
34
// Otherwise, Marshal uses the following type-dependent default encodings:
35
//
36
// Boolean values encode as JSON booleans.
37
//
38
// Floating point and integer values encode as JSON numbers.
39
//
40
// String values encode as JSON strings, with each invalid UTF-8 sequence
41
// replaced by the encoding of the Unicode replacement character U+FFFD.
42
// The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
43
// to keep some browsers from misinterpreting JSON output as HTML.
44
//
45
// Array and slice values encode as JSON arrays, except that
46
// []byte encodes as a base64-encoded string.
47
//
48
// Struct values encode as JSON objects. Each exported struct field
49
// becomes a member of the object unless
50
//   - the field's tag is "-", or
51
//   - the field is empty and its tag specifies the "omitempty" option.
52
// The empty values are false, 0, any
53
// nil pointer or interface value, and any array, slice, map, or string of
54
// length zero. The object's default key string is the struct field name
55
// but can be specified in the struct field's tag value. The "json" key in
56
// struct field's tag value is the key name, followed by an optional comma
57
// and options. Examples:
58
//
59
//   // Field is ignored by this package.
60
//   Field int `json:"-"`
61
//
62
//   // Field appears in JSON as key "myName".
63
//   Field int `json:"myName"`
64
//
65
//   // Field appears in JSON as key "myName" and
66
//   // the field is omitted from the object if its value is empty,
67
//   // as defined above.
68
//   Field int `json:"myName,omitempty"`
69
//
70
//   // Field appears in JSON as key "Field" (the default), but
71
//   // the field is skipped if empty.
72
//   // Note the leading comma.
73
//   Field int `json:",omitempty"`
74
//
75
// The "string" option signals that a field is stored as JSON inside a
76
// JSON-encoded string.  This extra level of encoding is sometimes
77
// used when communicating with JavaScript programs:
78
//
79
//    Int64String int64 `json:",string"`
80
//
81
// The key name will be used if it's a non-empty string consisting of
82
// only Unicode letters, digits, dollar signs, percent signs, hyphens,
83
// underscores and slashes.
84
//
85
// Map values encode as JSON objects.
86
// The map's key type must be string; the object keys are used directly
87
// as map keys.
88
//
89
// Pointer values encode as the value pointed to.
90
// A nil pointer encodes as the null JSON object.
91
//
92
// Interface values encode as the value contained in the interface.
93
// A nil interface value encodes as the null JSON object.
94
//
95
// Channel, complex, and function values cannot be encoded in JSON.
96
// Attempting to encode such a value causes Marshal to return
97
// an InvalidTypeError.
98
//
99
// JSON cannot represent cyclic data structures and Marshal does not
100
// handle them.  Passing cyclic structures to Marshal will result in
101
// an infinite recursion.
102
//
103
func Marshal(v interface{}) ([]byte, error) {
104
        e := &encodeState{}
105
        err := e.marshal(v)
106
        if err != nil {
107
                return nil, err
108
        }
109
        return e.Bytes(), nil
110
}
111
 
112
// MarshalIndent is like Marshal but applies Indent to format the output.
113
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
114
        b, err := Marshal(v)
115
        if err != nil {
116
                return nil, err
117
        }
118
        var buf bytes.Buffer
119
        err = Indent(&buf, b, prefix, indent)
120
        if err != nil {
121
                return nil, err
122
        }
123
        return buf.Bytes(), nil
124
}
125
 
126
// MarshalForHTML is like Marshal but applies HTMLEscape to the output.
127
func MarshalForHTML(v interface{}) ([]byte, error) {
128
        b, err := Marshal(v)
129
        if err != nil {
130
                return nil, err
131
        }
132
        var buf bytes.Buffer
133
        HTMLEscape(&buf, b)
134
        return buf.Bytes(), nil
135
}
136
 
137
// HTMLEscape appends to dst the JSON-encoded src with <, >, and &
138
// characters inside string literals changed to \u003c, \u003e, \u0026
139
// so that the JSON will be safe to embed inside HTML 



powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.