example_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_test
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "io"
  10. "log"
  11. "os"
  12. "strings"
  13. )
  14. func ExampleMarshal() {
  15. type ColorGroup struct {
  16. ID int
  17. Name string
  18. Colors []string
  19. }
  20. group := ColorGroup{
  21. ID: 1,
  22. Name: "Reds",
  23. Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
  24. }
  25. b, err := json.Marshal(group)
  26. if err != nil {
  27. fmt.Println("error:", err)
  28. }
  29. os.Stdout.Write(b)
  30. // Output:
  31. // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
  32. }
  33. func ExampleUnmarshal() {
  34. var jsonBlob = []byte(`[
  35. {"Name": "Platypus", "Order": "Monotremata"},
  36. {"Name": "Quoll", "Order": "Dasyuromorphia"}
  37. ]`)
  38. type Animal struct {
  39. Name string
  40. Order string
  41. }
  42. var animals []Animal
  43. err := json.Unmarshal(jsonBlob, &animals)
  44. if err != nil {
  45. fmt.Println("error:", err)
  46. }
  47. fmt.Printf("%+v", animals)
  48. // Output:
  49. // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
  50. }
  51. // This example uses a Decoder to decode a stream of distinct JSON values.
  52. func ExampleDecoder() {
  53. const jsonStream = `
  54. {"Name": "Ed", "Text": "Knock knock."}
  55. {"Name": "Sam", "Text": "Who's there?"}
  56. {"Name": "Ed", "Text": "Go fmt."}
  57. {"Name": "Sam", "Text": "Go fmt who?"}
  58. {"Name": "Ed", "Text": "Go fmt yourself!"}
  59. `
  60. type Message struct {
  61. Name, Text string
  62. }
  63. dec := json.NewDecoder(strings.NewReader(jsonStream))
  64. for {
  65. var m Message
  66. if err := dec.Decode(&m); err == io.EOF {
  67. break
  68. } else if err != nil {
  69. log.Fatal(err)
  70. }
  71. fmt.Printf("%s: %s\n", m.Name, m.Text)
  72. }
  73. // Output:
  74. // Ed: Knock knock.
  75. // Sam: Who's there?
  76. // Ed: Go fmt.
  77. // Sam: Go fmt who?
  78. // Ed: Go fmt yourself!
  79. }
  80. // This example uses RawMessage to delay parsing part of a JSON message.
  81. func ExampleRawMessage() {
  82. type Color struct {
  83. Space string
  84. Point json.RawMessage // delay parsing until we know the color space
  85. }
  86. type RGB struct {
  87. R uint8
  88. G uint8
  89. B uint8
  90. }
  91. type YCbCr struct {
  92. Y uint8
  93. Cb int8
  94. Cr int8
  95. }
  96. var j = []byte(`[
  97. {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}},
  98. {"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}}
  99. ]`)
  100. var colors []Color
  101. err := json.Unmarshal(j, &colors)
  102. if err != nil {
  103. log.Fatalln("error:", err)
  104. }
  105. for _, c := range colors {
  106. var dst interface{}
  107. switch c.Space {
  108. case "RGB":
  109. dst = new(RGB)
  110. case "YCbCr":
  111. dst = new(YCbCr)
  112. }
  113. err := json.Unmarshal(c.Point, dst)
  114. if err != nil {
  115. log.Fatalln("error:", err)
  116. }
  117. fmt.Println(c.Space, dst)
  118. }
  119. // Output:
  120. // YCbCr &{255 0 -10}
  121. // RGB &{98 218 255}
  122. }
  123. func ExampleIndent() {
  124. type Road struct {
  125. Name string
  126. Number int
  127. }
  128. roads := []Road{
  129. {"Diamond Fork", 29},
  130. {"Sheep Creek", 51},
  131. }
  132. b, err := json.Marshal(roads)
  133. if err != nil {
  134. log.Fatal(err)
  135. }
  136. var out bytes.Buffer
  137. json.Indent(&out, b, "=", "\t")
  138. out.WriteTo(os.Stdout)
  139. // Output:
  140. // [
  141. // = {
  142. // = "Name": "Diamond Fork",
  143. // = "Number": 29
  144. // = },
  145. // = {
  146. // = "Name": "Sheep Creek",
  147. // = "Number": 51
  148. // = }
  149. // =]
  150. }