github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue465_test.go (about) 1 /** 2 * Copyright 2023 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package issue_test 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "testing" 23 24 "github.com/goshafaq/sonic" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestIssue465(t *testing.T) { 29 data := ` 30 { 31 "data": { 32 "1": "", 33 "2": true, 34 "3": "", 35 "4": "", 36 "5": { 37 "a": {}, 38 "b": "hello", 39 "c": "" 40 }, 41 "6": "", 42 "7": false, 43 "8": true, 44 "9": 7, 45 "10": "", 46 "11": {}, 47 "12": 0, 48 "13": {}, 49 "14": "", 50 "15": "", 51 "16": true, 52 "17": {"d":{"e":1}} 53 } 54 }` 55 root, _ := sonic.GetFromString(data) 56 sp := root.GetByPath("data", "5") 57 node := sp.Get("a") 58 require.True(t, node.Exists()) 59 root.GetByPath("data", "17") 60 node2 := sp.Get("b") 61 get2, err2 := node2.String() 62 require.Nil(t, err2) 63 require.Equal(t, get2, "hello") 64 b, err := root.MarshalJSON() 65 require.NoError(t, err) 66 buf := bytes.NewBuffer(nil) 67 json.Compact(buf, b) 68 require.Equal(t, buf.String(), string(b)) 69 }