github.com/goccy/go-json@v0.10.3-0.20240509105655-5e2ae3f23c1d/tagkey_test.go (about) 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 5 package json_test 6 7 import ( 8 "testing" 9 10 "github.com/goccy/go-json" 11 ) 12 13 type basicLatin2xTag struct { 14 V string `json:"$%-/"` 15 } 16 17 type basicLatin3xTag struct { 18 V string `json:"0123456789"` 19 } 20 21 type basicLatin4xTag struct { 22 V string `json:"ABCDEFGHIJKLMO"` 23 } 24 25 type basicLatin5xTag struct { 26 V string `json:"PQRSTUVWXYZ_"` 27 } 28 29 type basicLatin6xTag struct { 30 V string `json:"abcdefghijklmno"` 31 } 32 33 type basicLatin7xTag struct { 34 V string `json:"pqrstuvwxyz"` 35 } 36 37 type miscPlaneTag struct { 38 V string `json:"色は匂へど"` 39 } 40 41 type percentSlashTag struct { 42 V string `json:"text/html%"` // https://golang.org/issue/2718 43 } 44 45 type punctuationTag struct { 46 V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // https://golang.org/issue/3546 47 } 48 49 type dashTag struct { 50 V string `json:"-,"` 51 } 52 53 type emptyTag struct { 54 W string 55 } 56 57 type misnamedTag struct { 58 X string `jsom:"Misnamed"` 59 } 60 61 type badFormatTag struct { 62 Y string `:"BadFormat"` 63 } 64 65 type badCodeTag struct { 66 Z string `json:" !\"#&'()*+,."` 67 } 68 69 type spaceTag struct { 70 Q string `json:"With space"` 71 } 72 73 type unicodeTag struct { 74 W string `json:"Ελλάδα"` 75 } 76 77 var structTagObjectKeyTests = []struct { 78 raw interface{} 79 value string 80 key string 81 }{ 82 {basicLatin2xTag{"2x"}, "2x", "$%-/"}, 83 {basicLatin3xTag{"3x"}, "3x", "0123456789"}, 84 {basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"}, 85 {basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"}, 86 {basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"}, 87 {basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"}, 88 {miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"}, 89 {dashTag{"foo"}, "foo", "-"}, 90 {emptyTag{"Pour Moi"}, "Pour Moi", "W"}, 91 {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"}, 92 {badFormatTag{"Orfevre"}, "Orfevre", "Y"}, 93 {badCodeTag{"Reliable Man"}, "Reliable Man", "Z"}, 94 {percentSlashTag{"brut"}, "brut", "text/html%"}, 95 {punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~"}, 96 {spaceTag{"Perreddu"}, "Perreddu", "With space"}, 97 {unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"}, 98 } 99 100 func TestStructTagObjectKey(t *testing.T) { 101 for _, tt := range structTagObjectKeyTests { 102 b, err := json.Marshal(tt.raw) 103 if err != nil { 104 t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err) 105 } 106 var f interface{} 107 err = json.Unmarshal(b, &f) 108 if err != nil { 109 t.Fatalf("Unmarshal(%#q) failed: %v", b, err) 110 } 111 for i, v := range f.(map[string]interface{}) { 112 switch i { 113 case tt.key: 114 if s, ok := v.(string); !ok || s != tt.value { 115 t.Fatalf("Unexpected value: %#q, want %v", s, tt.value) 116 } 117 default: 118 t.Fatalf("Unexpected key: %#q, from %#q", i, b) 119 } 120 } 121 } 122 }