| 1 |
package json |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"strconv" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
type funcN struct { |
| 12 |
Arg1 int `json:"arg1"` |
| 13 |
Arg2 int `json:"arg2"` |
| 14 |
} |
| 15 |
|
| 16 |
type funcs struct { |
| 17 |
Func2 *funcN `json:"$func2"` |
| 18 |
Func1 *funcN `json:"$func1"` |
| 19 |
} |
| 20 |
|
| 21 |
type funcsText struct { |
| 22 |
Func1 jsonText `json:"$func1"` |
| 23 |
Func2 jsonText `json:"$func2"` |
| 24 |
} |
| 25 |
|
| 26 |
type jsonText struct { |
| 27 |
json string |
| 28 |
} |
| 29 |
|
| 30 |
func (jt *jsonText) UnmarshalJSON(data []byte) error { |
| 31 |
jt.json = string(data) |
| 32 |
return nil |
| 33 |
} |
| 34 |
|
| 35 |
type nestedText struct { |
| 36 |
F jsonText |
| 37 |
B bool |
| 38 |
} |
| 39 |
|
| 40 |
type unquotedKey struct { |
| 41 |
S string `json:"$k_1"` |
| 42 |
} |
| 43 |
|
| 44 |
var ext Extension |
| 45 |
|
| 46 |
type keyed string |
| 47 |
|
| 48 |
func decodeKeyed(data []byte) (interface{}, error) { |
| 49 |
return keyed(data), nil |
| 50 |
} |
| 51 |
|
| 52 |
type keyedType struct { |
| 53 |
K keyed |
| 54 |
I int |
| 55 |
} |
| 56 |
|
| 57 |
type docint int |
| 58 |
|
| 59 |
type const1Type struct{} |
| 60 |
|
| 61 |
var const1 = new(const1Type) |
| 62 |
|
| 63 |
func init() { |
| 64 |
ext.DecodeFunc("Func1", "$func1") |
| 65 |
ext.DecodeFunc("Func2", "$func2", "arg1", "arg2") |
| 66 |
ext.DecodeFunc("Func3", "$func3", "arg1") |
| 67 |
ext.DecodeFunc("new Func4", "$func4", "arg1") |
| 68 |
|
| 69 |
ext.DecodeConst("Const1", const1) |
| 70 |
|
| 71 |
ext.DecodeKeyed("$key1", decodeKeyed) |
| 72 |
ext.DecodeKeyed("$func3", decodeKeyed) |
| 73 |
|
| 74 |
ext.EncodeType(docint(0), func(v interface{}) ([]byte, error) { |
| 75 |
s := `{"$docint": ` + strconv.Itoa(int(v.(docint))) + `}` |
| 76 |
return []byte(s), nil |
| 77 |
}) |
| 78 |
|
| 79 |
ext.DecodeUnquotedKeys(true) |
| 80 |
ext.DecodeTrailingCommas(true) |
| 81 |
} |
| 82 |
|
| 83 |
type extDecodeTest struct { |
| 84 |
in string |
| 85 |
ptr interface{} |
| 86 |
out interface{} |
| 87 |
err error |
| 88 |
|
| 89 |
noext bool |
| 90 |
} |
| 91 |
|
| 92 |
var extDecodeTests = []extDecodeTest{ |
| 93 |
// Functions |
| 94 |
{in: `Func1()`, ptr: new(interface{}), out: map[string]interface{}{ |
| 95 |
"$func1": map[string]interface{}{}, |
| 96 |
}}, |
| 97 |
{in: `{"v": Func1()}`, ptr: new(interface{}), out: map[string]interface{}{ |
| 98 |
"v": map[string]interface{}{"$func1": map[string]interface{}{}}, |
| 99 |
}}, |
| 100 |
{in: `Func2(1)`, ptr: new(interface{}), out: map[string]interface{}{ |
| 101 |
"$func2": map[string]interface{}{"arg1": float64(1)}, |
| 102 |
}}, |
| 103 |
{in: `Func2(1, 2)`, ptr: new(interface{}), out: map[string]interface{}{ |
| 104 |
"$func2": map[string]interface{}{"arg1": float64(1), "arg2": float64(2)}, |
| 105 |
}}, |
| 106 |
{in: `Func2(Func1())`, ptr: new(interface{}), out: map[string]interface{}{ |
| 107 |
"$func2": map[string]interface{}{"arg1": map[string]interface{}{"$func1": map[string]interface{}{}}}, |
| 108 |
}}, |
| 109 |
{in: `Func2(1, 2, 3)`, ptr: new(interface{}), err: fmt.Errorf("json: too many arguments for function Func2")}, |
| 110 |
{in: `BadFunc()`, ptr: new(interface{}), err: fmt.Errorf(`json: unknown function "BadFunc"`)}, |
| 111 |
|
| 112 |
{in: `Func1()`, ptr: new(funcs), out: funcs{Func1: &funcN{}}}, |
| 113 |
{in: `Func2(1)`, ptr: new(funcs), out: funcs{Func2: &funcN{Arg1: 1}}}, |
| 114 |
{in: `Func2(1, 2)`, ptr: new(funcs), out: funcs{Func2: &funcN{Arg1: 1, Arg2: 2}}}, |
| 115 |
|
| 116 |
{in: `Func2(1, 2, 3)`, ptr: new(funcs), err: fmt.Errorf("json: too many arguments for function Func2")}, |
| 117 |
{in: `BadFunc()`, ptr: new(funcs), err: fmt.Errorf(`json: unknown function "BadFunc"`)}, |
| 118 |
|
| 119 |
{in: `Func2(1)`, ptr: new(jsonText), out: jsonText{"Func2(1)"}}, |
| 120 |
{in: `Func2(1, 2)`, ptr: new(funcsText), out: funcsText{Func2: jsonText{"Func2(1, 2)"}}}, |
| 121 |
{in: `{"f": Func2(1, 2), "b": true}`, ptr: new(nestedText), out: nestedText{jsonText{"Func2(1, 2)"}, true}}, |
| 122 |
|
| 123 |
{in: `Func1()`, ptr: new(struct{}), out: struct{}{}}, |
| 124 |
|
| 125 |
// Functions with "new" prefix |
| 126 |
{in: `new Func4(1)`, ptr: new(interface{}), out: map[string]interface{}{ |
| 127 |
"$func4": map[string]interface{}{"arg1": float64(1)}, |
| 128 |
}}, |
| 129 |
|
| 130 |
// Constants |
| 131 |
{in: `Const1`, ptr: new(interface{}), out: const1}, |
| 132 |
{in: `{"c": Const1}`, ptr: new(struct{ C *const1Type }), out: struct{ C *const1Type }{const1}}, |
| 133 |
|
| 134 |
// Keyed documents |
| 135 |
{in: `{"v": {"$key1": 1}}`, ptr: new(interface{}), out: map[string]interface{}{"v": keyed(`{"$key1": 1}`)}}, |
| 136 |
{in: `{"k": {"$key1": 1}}`, ptr: new(keyedType), out: keyedType{K: keyed(`{"$key1": 1}`)}}, |
| 137 |
{in: `{"i": {"$key1": 1}}`, ptr: new(keyedType), err: &UnmarshalTypeError{"object", reflect.TypeOf(0), 18}}, |
| 138 |
|
| 139 |
// Keyed function documents |
| 140 |
{in: `{"v": Func3()}`, ptr: new(interface{}), out: map[string]interface{}{"v": keyed(`Func3()`)}}, |
| 141 |
{in: `{"k": Func3()}`, ptr: new(keyedType), out: keyedType{K: keyed(`Func3()`)}}, |
| 142 |
{in: `{"i": Func3()}`, ptr: new(keyedType), err: &UnmarshalTypeError{"object", reflect.TypeOf(0), 13}}, |
| 143 |
|
| 144 |
// Unquoted keys |
| 145 |
{in: `{$k_1: "bar"}`, ptr: new(interface{}), out: map[string]interface{}{"$k_1": "bar"}}, |
| 146 |
{in: `{$k_1: "bar"}`, ptr: new(unquotedKey), out: unquotedKey{"bar"}}, |
| 147 |
|
| 148 |
{in: `{$k_1: "bar"}`, noext: true, ptr: new(interface{}), |
| 149 |
err: &SyntaxError{"invalid character '$' looking for beginning of object key string", 2}}, |
| 150 |
{in: `{$k_1: "bar"}`, noext: true, ptr: new(unquotedKey), |
| 151 |
err: &SyntaxError{"invalid character '$' looking for beginning of object key string", 2}}, |
| 152 |
|
| 153 |
// Trailing commas |
| 154 |
{in: `{"k": "v",}`, ptr: new(interface{}), out: map[string]interface{}{"k": "v"}}, |
| 155 |
{in: `{"k": "v",}`, ptr: new(struct{}), out: struct{}{}}, |
| 156 |
{in: `["v",]`, ptr: new(interface{}), out: []interface{}{"v"}}, |
| 157 |
|
| 158 |
{in: `{"k": "v",}`, noext: true, ptr: new(interface{}), |
| 159 |
err: &SyntaxError{"invalid character '}' looking for beginning of object key string", 11}}, |
| 160 |
{in: `{"k": "v",}`, noext: true, ptr: new(struct{}), |
| 161 |
err: &SyntaxError{"invalid character '}' looking for beginning of object key string", 11}}, |
| 162 |
{in: `["a",]`, noext: true, ptr: new(interface{}), |
| 163 |
err: &SyntaxError{"invalid character ']' looking for beginning of value", 6}}, |
| 164 |
} |
| 165 |
|
| 166 |
type extEncodeTest struct { |
| 167 |
in interface{} |
| 168 |
out string |
| 169 |
err error |
| 170 |
} |
| 171 |
|
| 172 |
var extEncodeTests = []extEncodeTest{ |
| 173 |
{in: docint(13), out: "{\"$docint\":13}\n"}, |
| 174 |
} |
| 175 |
|
| 176 |
func TestExtensionDecode(t *testing.T) { |
| 177 |
for i, tt := range extDecodeTests { |
| 178 |
in := []byte(tt.in) |
| 179 |
|
| 180 |
// v = new(right-type) |
| 181 |
v := reflect.New(reflect.TypeOf(tt.ptr).Elem()) |
| 182 |
dec := NewDecoder(bytes.NewReader(in)) |
| 183 |
if !tt.noext { |
| 184 |
dec.Extend(&ext) |
| 185 |
} |
| 186 |
if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.err) { |
| 187 |
t.Errorf("#%d: %v, want %v", i, err, tt.err) |
| 188 |
continue |
| 189 |
} else if err != nil { |
| 190 |
continue |
| 191 |
} |
| 192 |
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { |
| 193 |
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out) |
| 194 |
data, _ := Marshal(v.Elem().Interface()) |
| 195 |
t.Logf("%s", string(data)) |
| 196 |
data, _ = Marshal(tt.out) |
| 197 |
t.Logf("%s", string(data)) |
| 198 |
continue |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func TestExtensionEncode(t *testing.T) { |
| 204 |
var buf bytes.Buffer |
| 205 |
for i, tt := range extEncodeTests { |
| 206 |
buf.Truncate(0) |
| 207 |
enc := NewEncoder(&buf) |
| 208 |
enc.Extend(&ext) |
| 209 |
err := enc.Encode(tt.in) |
| 210 |
if !reflect.DeepEqual(err, tt.err) { |
| 211 |
t.Errorf("#%d: %v, want %v", i, err, tt.err) |
| 212 |
continue |
| 213 |
} |
| 214 |
if buf.String() != tt.out { |
| 215 |
t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, buf.String(), tt.out) |
| 216 |
} |
| 217 |
} |
| 218 |
} |