github.com/jxskiss/gopkg@v0.17.3/json/ext_test.go (about) 1 package json 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 var malformedJSONData = ` 9 { 10 // A comment! You normally can't put these in JSON 11 "obj1": { 12 "foo": "bar", // <-- A trailing comma! No worries. 13 }, 14 /* 15 This style of comments will also be safely removed. 16 */ 17 "array": [1, 2, 3, ], // Trailing comma in array. 18 "import": @import("testdata.json"), // Import another json file. 19 identifier_simple1: 1234, 20 $identifierSimple2: "abc", 21 "obj2": { 22 "foo": "bar", /* Another style inline comment. */ 23 }, // <-- Another trailing comma! 24 } 25 ` 26 27 func TestUnmarshalExt_comment_trailingComma(t *testing.T) { 28 want := map[string]interface{}{ 29 "obj1": map[string]interface{}{ 30 "foo": "bar", 31 }, 32 "array": []interface{}{float64(1), float64(2), float64(3)}, 33 "import": map[string]interface{}{ 34 "foo": "bar", 35 }, 36 "identifier_simple1": float64(1234), 37 "$identifierSimple2": "abc", 38 "obj2": map[string]interface{}{ 39 "foo": "bar", 40 }, 41 } 42 got := make(map[string]interface{}) 43 err := UnmarshalExt([]byte(malformedJSONData), &got, "") 44 if err != nil { 45 t.Fatalf("failed unmarshal malformed json: %v", err) 46 } 47 if !reflect.DeepEqual(got, want) { 48 t.Fatalf("expecting equal: got = %v, want = %v", got, want) 49 } 50 } 51 52 func TestUnmarshalExt_UnicodeEscape(t *testing.T) { 53 jsonData := `["Grammar \u0026 Mechanics \/ Word Work"]` 54 got := make([]string, 0) 55 err := UnmarshalExt([]byte(jsonData), &got, "") 56 if err != nil { 57 t.Errorf("failed unmarshal unicode escape char: %v", err) 58 } 59 } 60 61 func TestUnmarshalExt_SingleQuote(t *testing.T) { 62 jsonData := `{'key\'': 'value"'}` 63 got := make(map[string]string) 64 err := UnmarshalExt([]byte(jsonData), &got, "") 65 if err != nil { 66 t.Errorf("failed unmarshal single quoted string: %v", err) 67 } 68 if got["key'"] != "value\"" { 69 t.Errorf("unmarshal single quoted string: incorrect key value") 70 } 71 }