github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/example_test.go (about) 1 package jsoni 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "os" 8 "strings" 9 ) 10 11 func ExampleMarshal() { 12 type ColorGroup struct { 13 ID int 14 Name string 15 Colors []string 16 } 17 group := ColorGroup{ 18 ID: 1, 19 Name: "Reds", 20 Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, 21 } 22 b, err := Marshal(group) 23 if err != nil { 24 fmt.Println("error:", err) 25 } 26 fmt.Println(string(b)) 27 28 var p *int 29 b, err = json.Marshal(p) 30 if err != nil { 31 fmt.Println(err) 32 return 33 } 34 fmt.Println(string(b)) 35 36 b, err = Marshal(p) 37 if err != nil { 38 fmt.Println(err) 39 return 40 } 41 fmt.Println(string(b)) 42 // Output: 43 // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]} 44 // null 45 // null 46 } 47 48 func ExampleUnmarshal() { 49 jsonBlob := []byte(`[ 50 {"Name": "Platypus", "Order": "Monotremata"}, 51 {"Name": "Quoll", "Order": "Dasyuromorphia"} 52 ]`) 53 type Animal struct { 54 Name string 55 Order string 56 } 57 var animals []Animal 58 err := Unmarshal(jsonBlob, &animals) 59 if err != nil { 60 fmt.Println("error:", err) 61 } 62 fmt.Printf("%+v\n", animals) 63 64 type A struct { 65 Bar string `json:"Bar"` 66 } 67 68 var a A 69 c := Config{EscapeHTML: true, CaseSensitive: true}.Froze() 70 c.Unmarshal(context.Background(), []byte(`{"Bar": "1", "bar": "2" }`), &a) 71 fmt.Println(a.Bar) 72 73 // Output: 74 // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}] 75 // 1 76 } 77 78 func ExampleConfigFastest_Marshal() { 79 type ColorGroup struct { 80 ID int 81 Name string 82 Colors []string 83 } 84 group := ColorGroup{ 85 ID: 1, 86 Name: "Reds", 87 Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, 88 } 89 stream := ConfigFastest.BorrowStream(nil) 90 defer ConfigFastest.ReturnStream(stream) 91 stream.WriteVal(context.Background(), group) 92 if stream.Error != nil { 93 fmt.Println("error:", stream.Error) 94 } 95 os.Stdout.Write(stream.Buffer()) 96 // Output: 97 // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]} 98 } 99 100 func ExampleConfigFastest_Unmarshal() { 101 jsonBlob := []byte(`[ 102 {"Name": "Platypus", "Order": "Monotremata"}, 103 {"Name": "Quoll", "Order": "Dasyuromorphia"} 104 ]`) 105 type Animal struct { 106 Name string 107 Order string 108 } 109 var animals []Animal 110 iter := ConfigFastest.BorrowIterator(jsonBlob) 111 defer ConfigFastest.ReturnIterator(iter) 112 iter.ReadVal(context.Background(), &animals) 113 if iter.Error != nil { 114 fmt.Println("error:", iter.Error) 115 } 116 fmt.Printf("%+v", animals) 117 // Output: 118 // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}] 119 } 120 121 func ExampleGet() { 122 val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`) 123 fmt.Printf(Get(val, "Colors", 0).ToString()) 124 // Output: 125 // Crimson 126 } 127 128 func ExampleMyKey() { 129 hello := MyKey("hello") 130 output, _ := Marshal(map[*MyKey]string{&hello: "world"}) 131 fmt.Println(string(output)) 132 obj := map[*MyKey]string{} 133 Unmarshal(output, &obj) 134 for k, v := range obj { 135 fmt.Println(*k, v) 136 } 137 // Output: 138 // {"Hello":"world"} 139 // Hel world 140 } 141 142 type MyKey string 143 144 func (m *MyKey) MarshalText() ([]byte, error) { 145 return []byte(strings.Replace(string(*m), "h", "H", -1)), nil 146 } 147 148 func (m *MyKey) UnmarshalText(text []byte) error { 149 *m = MyKey(text[:3]) 150 return nil 151 }