example_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. )
  7. func ExampleMarshal() {
  8. type ColorGroup struct {
  9. ID int
  10. Name string
  11. Colors []string
  12. }
  13. group := ColorGroup{
  14. ID: 1,
  15. Name: "Reds",
  16. Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
  17. }
  18. b, err := Marshal(group)
  19. if err != nil {
  20. fmt.Println("error:", err)
  21. }
  22. os.Stdout.Write(b)
  23. // Output:
  24. // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
  25. }
  26. func ExampleUnmarshal() {
  27. var jsonBlob = []byte(`[
  28. {"Name": "Platypus", "Order": "Monotremata"},
  29. {"Name": "Quoll", "Order": "Dasyuromorphia"}
  30. ]`)
  31. type Animal struct {
  32. Name string
  33. Order string
  34. }
  35. var animals []Animal
  36. err := Unmarshal(jsonBlob, &animals)
  37. if err != nil {
  38. fmt.Println("error:", err)
  39. }
  40. fmt.Printf("%+v", animals)
  41. // Output:
  42. // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
  43. }
  44. func ExampleConfigFastest_Marshal() {
  45. type ColorGroup struct {
  46. ID int
  47. Name string
  48. Colors []string
  49. }
  50. group := ColorGroup{
  51. ID: 1,
  52. Name: "Reds",
  53. Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
  54. }
  55. stream := ConfigFastest.BorrowStream(nil)
  56. defer ConfigFastest.ReturnStream(stream)
  57. stream.WriteVal(group)
  58. if stream.Error != nil {
  59. fmt.Println("error:", stream.Error)
  60. }
  61. os.Stdout.Write(stream.Buffer())
  62. // Output:
  63. // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
  64. }
  65. func ExampleConfigFastest_Unmarshal() {
  66. var jsonBlob = []byte(`[
  67. {"Name": "Platypus", "Order": "Monotremata"},
  68. {"Name": "Quoll", "Order": "Dasyuromorphia"}
  69. ]`)
  70. type Animal struct {
  71. Name string
  72. Order string
  73. }
  74. var animals []Animal
  75. iter := ConfigFastest.BorrowIterator(jsonBlob)
  76. defer ConfigFastest.ReturnIterator(iter)
  77. iter.ReadVal(&animals)
  78. if iter.Error != nil {
  79. fmt.Println("error:", iter.Error)
  80. }
  81. fmt.Printf("%+v", animals)
  82. // Output:
  83. // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
  84. }
  85. func ExampleGet() {
  86. val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
  87. fmt.Printf(Get(val, "Colors", 0).ToString())
  88. // Output:
  89. // Crimson
  90. }
  91. func ExampleMyKey() {
  92. hello := MyKey("hello")
  93. output, _ := Marshal(map[*MyKey]string{&hello: "world"})
  94. fmt.Println(string(output))
  95. obj := map[*MyKey]string{}
  96. Unmarshal(output, &obj)
  97. for k, v := range obj {
  98. fmt.Println(*k, v)
  99. }
  100. // Output:
  101. // {"Hello":"world"}
  102. // Hel world
  103. }
  104. type MyKey string
  105. func (m *MyKey) MarshalText() ([]byte, error) {
  106. return []byte(strings.Replace(string(*m), "h", "H", -1)), nil
  107. }
  108. func (m *MyKey) UnmarshalText(text []byte) error {
  109. *m = MyKey(text[:3])
  110. return nil
  111. }