github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/encoding/json/example_text_marshaling_test.go (about) 1 // Copyright 2018 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 5 package json_test 6 7 import ( 8 "github.com/shogo82148/std/encoding/json" 9 "github.com/shogo82148/std/fmt" 10 "github.com/shogo82148/std/log" 11 ) 12 13 const ( 14 Unrecognized Size = iota 15 Small 16 Large 17 ) 18 19 func Example_textMarshalJSON() { 20 blob := `["small","regular","large","unrecognized","small","normal","small","large"]` 21 var inventory []Size 22 if err := json.Unmarshal([]byte(blob), &inventory); err != nil { 23 log.Fatal(err) 24 } 25 26 counts := make(map[Size]int) 27 for _, size := range inventory { 28 counts[size] += 1 29 } 30 31 fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n", 32 counts[Small], counts[Large], counts[Unrecognized]) 33 34 // Output: 35 // Inventory Counts: 36 // * Small: 3 37 // * Large: 2 38 // * Unrecognized: 3 39 }