encode_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // Copyright 2011 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. package json
  5. import (
  6. "bytes"
  7. "math"
  8. "reflect"
  9. "testing"
  10. "unicode"
  11. )
  12. type Optionals struct {
  13. Sr string `json:"sr"`
  14. So string `json:"so,omitempty"`
  15. Sw string `json:"-"`
  16. Ir int `json:"omitempty"` // actually named omitempty, not an option
  17. Io int `json:"io,omitempty"`
  18. Slr []string `json:"slr,random"`
  19. Slo []string `json:"slo,omitempty"`
  20. Mr map[string]interface{} `json:"mr"`
  21. Mo map[string]interface{} `json:",omitempty"`
  22. Fr float64 `json:"fr"`
  23. Fo float64 `json:"fo,omitempty"`
  24. Br bool `json:"br"`
  25. Bo bool `json:"bo,omitempty"`
  26. Ur uint `json:"ur"`
  27. Uo uint `json:"uo,omitempty"`
  28. Str struct{} `json:"str"`
  29. Sto struct{} `json:"sto,omitempty"`
  30. }
  31. var optionalsExpected = `{
  32. "sr": "",
  33. "omitempty": 0,
  34. "slr": null,
  35. "mr": {},
  36. "fr": 0,
  37. "br": false,
  38. "ur": 0,
  39. "str": {},
  40. "sto": {}
  41. }`
  42. func TestOmitEmpty(t *testing.T) {
  43. var o Optionals
  44. o.Sw = "something"
  45. o.Mr = map[string]interface{}{}
  46. o.Mo = map[string]interface{}{}
  47. got, err := MarshalIndent(&o, "", " ")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. if got := string(got); got != optionalsExpected {
  52. t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected)
  53. }
  54. }
  55. type StringTag struct {
  56. BoolStr bool `json:",string"`
  57. IntStr int64 `json:",string"`
  58. StrStr string `json:",string"`
  59. }
  60. var stringTagExpected = `{
  61. "BoolStr": "true",
  62. "IntStr": "42",
  63. "StrStr": "\"xzbit\""
  64. }`
  65. func TestStringTag(t *testing.T) {
  66. var s StringTag
  67. s.BoolStr = true
  68. s.IntStr = 42
  69. s.StrStr = "xzbit"
  70. got, err := MarshalIndent(&s, "", " ")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if got := string(got); got != stringTagExpected {
  75. t.Fatalf(" got: %s\nwant: %s\n", got, stringTagExpected)
  76. }
  77. // Verify that it round-trips.
  78. var s2 StringTag
  79. err = NewDecoder(bytes.NewReader(got)).Decode(&s2)
  80. if err != nil {
  81. t.Fatalf("Decode: %v", err)
  82. }
  83. if !reflect.DeepEqual(s, s2) {
  84. t.Fatalf("decode didn't match.\nsource: %#v\nEncoded as:\n%s\ndecode: %#v", s, string(got), s2)
  85. }
  86. }
  87. // byte slices are special even if they're renamed types.
  88. type renamedByte byte
  89. type renamedByteSlice []byte
  90. type renamedRenamedByteSlice []renamedByte
  91. func TestEncodeRenamedByteSlice(t *testing.T) {
  92. s := renamedByteSlice("abc")
  93. result, err := Marshal(s)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. expect := `"YWJj"`
  98. if string(result) != expect {
  99. t.Errorf(" got %s want %s", result, expect)
  100. }
  101. r := renamedRenamedByteSlice("abc")
  102. result, err = Marshal(r)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if string(result) != expect {
  107. t.Errorf(" got %s want %s", result, expect)
  108. }
  109. }
  110. var unsupportedValues = []interface{}{
  111. math.NaN(),
  112. math.Inf(-1),
  113. math.Inf(1),
  114. }
  115. func TestUnsupportedValues(t *testing.T) {
  116. for _, v := range unsupportedValues {
  117. if _, err := Marshal(v); err != nil {
  118. if _, ok := err.(*UnsupportedValueError); !ok {
  119. t.Errorf("for %v, got %T want UnsupportedValueError", v, err)
  120. }
  121. } else {
  122. t.Errorf("for %v, expected error", v)
  123. }
  124. }
  125. }
  126. // Ref has Marshaler and Unmarshaler methods with pointer receiver.
  127. type Ref int
  128. func (*Ref) MarshalJSON() ([]byte, error) {
  129. return []byte(`"ref"`), nil
  130. }
  131. func (r *Ref) UnmarshalJSON([]byte) error {
  132. *r = 12
  133. return nil
  134. }
  135. // Val has Marshaler methods with value receiver.
  136. type Val int
  137. func (Val) MarshalJSON() ([]byte, error) {
  138. return []byte(`"val"`), nil
  139. }
  140. // RefText has Marshaler and Unmarshaler methods with pointer receiver.
  141. type RefText int
  142. func (*RefText) MarshalText() ([]byte, error) {
  143. return []byte(`"ref"`), nil
  144. }
  145. func (r *RefText) UnmarshalText([]byte) error {
  146. *r = 13
  147. return nil
  148. }
  149. // ValText has Marshaler methods with value receiver.
  150. type ValText int
  151. func (ValText) MarshalText() ([]byte, error) {
  152. return []byte(`"val"`), nil
  153. }
  154. func TestRefValMarshal(t *testing.T) {
  155. var s = struct {
  156. R0 Ref
  157. R1 *Ref
  158. R2 RefText
  159. R3 *RefText
  160. V0 Val
  161. V1 *Val
  162. V2 ValText
  163. V3 *ValText
  164. }{
  165. R0: 12,
  166. R1: new(Ref),
  167. R2: 14,
  168. R3: new(RefText),
  169. V0: 13,
  170. V1: new(Val),
  171. V2: 15,
  172. V3: new(ValText),
  173. }
  174. const want = `{"R0":"ref","R1":"ref","R2":"\"ref\"","R3":"\"ref\"","V0":"val","V1":"val","V2":"\"val\"","V3":"\"val\""}`
  175. b, err := Marshal(&s)
  176. if err != nil {
  177. t.Fatalf("Marshal: %v", err)
  178. }
  179. if got := string(b); got != want {
  180. t.Errorf("got %q, want %q", got, want)
  181. }
  182. }
  183. // C implements Marshaler and returns unescaped JSON.
  184. type C int
  185. func (C) MarshalJSON() ([]byte, error) {
  186. return []byte(`"<&>"`), nil
  187. }
  188. // CText implements Marshaler and returns unescaped text.
  189. type CText int
  190. func (CText) MarshalText() ([]byte, error) {
  191. return []byte(`"<&>"`), nil
  192. }
  193. func TestMarshalerEscaping(t *testing.T) {
  194. var c C
  195. want := `"\u003c\u0026\u003e"`
  196. b, err := Marshal(c)
  197. if err != nil {
  198. t.Fatalf("Marshal(c): %v", err)
  199. }
  200. if got := string(b); got != want {
  201. t.Errorf("Marshal(c) = %#q, want %#q", got, want)
  202. }
  203. var ct CText
  204. want = `"\"\u003c\u0026\u003e\""`
  205. b, err = Marshal(ct)
  206. if err != nil {
  207. t.Fatalf("Marshal(ct): %v", err)
  208. }
  209. if got := string(b); got != want {
  210. t.Errorf("Marshal(ct) = %#q, want %#q", got, want)
  211. }
  212. }
  213. type IntType int
  214. type MyStruct struct {
  215. IntType
  216. }
  217. func TestAnonymousNonstruct(t *testing.T) {
  218. var i IntType = 11
  219. a := MyStruct{i}
  220. const want = `{"IntType":11}`
  221. b, err := Marshal(a)
  222. if err != nil {
  223. t.Fatalf("Marshal: %v", err)
  224. }
  225. if got := string(b); got != want {
  226. t.Errorf("got %q, want %q", got, want)
  227. }
  228. }
  229. type BugA struct {
  230. S string
  231. }
  232. type BugB struct {
  233. BugA
  234. S string
  235. }
  236. type BugC struct {
  237. S string
  238. }
  239. // Legal Go: We never use the repeated embedded field (S).
  240. type BugX struct {
  241. A int
  242. BugA
  243. BugB
  244. }
  245. // Issue 5245.
  246. func TestEmbeddedBug(t *testing.T) {
  247. v := BugB{
  248. BugA{"A"},
  249. "B",
  250. }
  251. b, err := Marshal(v)
  252. if err != nil {
  253. t.Fatal("Marshal:", err)
  254. }
  255. want := `{"S":"B"}`
  256. got := string(b)
  257. if got != want {
  258. t.Fatalf("Marshal: got %s want %s", got, want)
  259. }
  260. // Now check that the duplicate field, S, does not appear.
  261. x := BugX{
  262. A: 23,
  263. }
  264. b, err = Marshal(x)
  265. if err != nil {
  266. t.Fatal("Marshal:", err)
  267. }
  268. want = `{"A":23}`
  269. got = string(b)
  270. if got != want {
  271. t.Fatalf("Marshal: got %s want %s", got, want)
  272. }
  273. }
  274. type BugD struct { // Same as BugA after tagging.
  275. XXX string `json:"S"`
  276. }
  277. // BugD's tagged S field should dominate BugA's.
  278. type BugY struct {
  279. BugA
  280. BugD
  281. }
  282. // Test that a field with a tag dominates untagged fields.
  283. func TestTaggedFieldDominates(t *testing.T) {
  284. v := BugY{
  285. BugA{"BugA"},
  286. BugD{"BugD"},
  287. }
  288. b, err := Marshal(v)
  289. if err != nil {
  290. t.Fatal("Marshal:", err)
  291. }
  292. want := `{"S":"BugD"}`
  293. got := string(b)
  294. if got != want {
  295. t.Fatalf("Marshal: got %s want %s", got, want)
  296. }
  297. }
  298. // There are no tags here, so S should not appear.
  299. type BugZ struct {
  300. BugA
  301. BugC
  302. BugY // Contains a tagged S field through BugD; should not dominate.
  303. }
  304. func TestDuplicatedFieldDisappears(t *testing.T) {
  305. v := BugZ{
  306. BugA{"BugA"},
  307. BugC{"BugC"},
  308. BugY{
  309. BugA{"nested BugA"},
  310. BugD{"nested BugD"},
  311. },
  312. }
  313. b, err := Marshal(v)
  314. if err != nil {
  315. t.Fatal("Marshal:", err)
  316. }
  317. want := `{}`
  318. got := string(b)
  319. if got != want {
  320. t.Fatalf("Marshal: got %s want %s", got, want)
  321. }
  322. }
  323. func TestStringBytes(t *testing.T) {
  324. // Test that encodeState.stringBytes and encodeState.string use the same encoding.
  325. es := &encodeState{}
  326. var r []rune
  327. for i := '\u0000'; i <= unicode.MaxRune; i++ {
  328. r = append(r, i)
  329. }
  330. s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
  331. _, err := es.string(s)
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. esBytes := &encodeState{}
  336. _, err = esBytes.stringBytes([]byte(s))
  337. if err != nil {
  338. t.Fatal(err)
  339. }
  340. enc := es.Buffer.String()
  341. encBytes := esBytes.Buffer.String()
  342. if enc != encBytes {
  343. i := 0
  344. for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
  345. i++
  346. }
  347. enc = enc[i:]
  348. encBytes = encBytes[i:]
  349. i = 0
  350. for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
  351. i++
  352. }
  353. enc = enc[:len(enc)-i]
  354. encBytes = encBytes[:len(encBytes)-i]
  355. if len(enc) > 20 {
  356. enc = enc[:20] + "..."
  357. }
  358. if len(encBytes) > 20 {
  359. encBytes = encBytes[:20] + "..."
  360. }
  361. t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
  362. }
  363. }
  364. func TestIssue6458(t *testing.T) {
  365. type Foo struct {
  366. M RawMessage
  367. }
  368. x := Foo{RawMessage(`"foo"`)}
  369. b, err := Marshal(&x)
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. if want := `{"M":"foo"}`; string(b) != want {
  374. t.Errorf("Marshal(&x) = %#q; want %#q", b, want)
  375. }
  376. b, err = Marshal(x)
  377. if err != nil {
  378. t.Fatal(err)
  379. }
  380. if want := `{"M":"ImZvbyI="}`; string(b) != want {
  381. t.Errorf("Marshal(x) = %#q; want %#q", b, want)
  382. }
  383. }
  384. func TestHTMLEscape(t *testing.T) {
  385. var b, want bytes.Buffer
  386. m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}`
  387. want.Write([]byte(`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
  388. HTMLEscape(&b, []byte(m))
  389. if !bytes.Equal(b.Bytes(), want.Bytes()) {
  390. t.Errorf("HTMLEscape(&b, []byte(m)) = %s; want %s", b.Bytes(), want.Bytes())
  391. }
  392. }
  393. // golang.org/issue/8582
  394. func TestEncodePointerString(t *testing.T) {
  395. type stringPointer struct {
  396. N *int64 `json:"n,string"`
  397. }
  398. var n int64 = 42
  399. b, err := Marshal(stringPointer{N: &n})
  400. if err != nil {
  401. t.Fatalf("Marshal: %v", err)
  402. }
  403. if got, want := string(b), `{"n":"42"}`; got != want {
  404. t.Errorf("Marshal = %s, want %s", got, want)
  405. }
  406. var back stringPointer
  407. err = Unmarshal(b, &back)
  408. if err != nil {
  409. t.Fatalf("Unmarshal: %v", err)
  410. }
  411. if back.N == nil {
  412. t.Fatalf("Unmarshalled nil N field")
  413. }
  414. if *back.N != 42 {
  415. t.Fatalf("*N = %d; want 42", *back.N)
  416. }
  417. }
  418. var encodeStringTests = []struct {
  419. in string
  420. out string
  421. }{
  422. {"\x00", `"\u0000"`},
  423. {"\x01", `"\u0001"`},
  424. {"\x02", `"\u0002"`},
  425. {"\x03", `"\u0003"`},
  426. {"\x04", `"\u0004"`},
  427. {"\x05", `"\u0005"`},
  428. {"\x06", `"\u0006"`},
  429. {"\x07", `"\u0007"`},
  430. {"\x08", `"\u0008"`},
  431. {"\x09", `"\t"`},
  432. {"\x0a", `"\n"`},
  433. {"\x0b", `"\u000b"`},
  434. {"\x0c", `"\u000c"`},
  435. {"\x0d", `"\r"`},
  436. {"\x0e", `"\u000e"`},
  437. {"\x0f", `"\u000f"`},
  438. {"\x10", `"\u0010"`},
  439. {"\x11", `"\u0011"`},
  440. {"\x12", `"\u0012"`},
  441. {"\x13", `"\u0013"`},
  442. {"\x14", `"\u0014"`},
  443. {"\x15", `"\u0015"`},
  444. {"\x16", `"\u0016"`},
  445. {"\x17", `"\u0017"`},
  446. {"\x18", `"\u0018"`},
  447. {"\x19", `"\u0019"`},
  448. {"\x1a", `"\u001a"`},
  449. {"\x1b", `"\u001b"`},
  450. {"\x1c", `"\u001c"`},
  451. {"\x1d", `"\u001d"`},
  452. {"\x1e", `"\u001e"`},
  453. {"\x1f", `"\u001f"`},
  454. }
  455. func TestEncodeString(t *testing.T) {
  456. for _, tt := range encodeStringTests {
  457. b, err := Marshal(tt.in)
  458. if err != nil {
  459. t.Errorf("Marshal(%q): %v", tt.in, err)
  460. continue
  461. }
  462. out := string(b)
  463. if out != tt.out {
  464. t.Errorf("Marshal(%q) = %#q, want %#q", tt.in, out, tt.out)
  465. }
  466. }
  467. }