github.com/lxt1045/json@v0.0.0-20231013032136-54d6b1d6e525/api.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2021 Xiantu Li 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package json 24 25 import ( 26 "fmt" 27 "reflect" 28 "sync/atomic" 29 30 lxterrs "github.com/lxt1045/errors" 31 ) 32 33 //Unmarshal 转成struct 34 func Unmarshal(bsIn []byte, in interface{}) (err error) { 35 return UnmarshalString(bytesString(bsIn), in) 36 } 37 38 //UnmarshalString Unmarshal string 39 func UnmarshalString(bs string, in interface{}) (err error) { 40 defer func() { 41 if e := recover(); e != nil { 42 if err1, ok := e.(*lxterrs.Code); ok { 43 err = err1 44 } else { 45 err = lxterrs.New("%+v", e) 46 } 47 return 48 } 49 }() 50 i := trimSpace(bs) 51 if mIn, ok := in.(*interface{}); ok { 52 if bs[i] != '{' { 53 err = fmt.Errorf("json must start with '{' or '[', %s", ErrStream(bs[i:])) 54 return 55 } 56 m, _, _ := parseMapInterface(-1, bs[i+1:]) 57 *mIn = m 58 return nil 59 } 60 if mIn, ok := in.(*map[string]interface{}); ok { 61 if bs[i] != '{' { 62 err = fmt.Errorf("json must start with '{' or '[', %s", ErrStream(bs[i:])) 63 return 64 } 65 m, _, _ := parseMapInterface(-1, bs[i+1:]) 66 *mIn = m 67 return nil 68 } 69 if _, ok := in.(*[]interface{}); ok { 70 if bs[i] != '[' { 71 err = fmt.Errorf("json must start with '{' or '[', %s", ErrStream(bs[i:])) 72 return 73 } 74 out := make([]interface{}, 0, 32) 75 parseObjToSlice(bs[i+1:], out) 76 return 77 } 78 79 // 解引用; TODO: 用 Value 的方式提高性能 80 vi := reflect.Indirect(reflect.ValueOf(in)) 81 if !vi.CanSet() { 82 err = fmt.Errorf("%T cannot set", in) 83 return 84 } 85 prv := reflectValueToValue(&vi) 86 goType := prv.typ 87 tag, err := LoadTagNode(vi, goType.Hash) 88 if err != nil { 89 return 90 } 91 92 store := PoolStore{ 93 tag: tag, 94 obj: prv.ptr, // eface.Value, 95 // pointerPool: tag.ptrCache.Get(), 96 } 97 if tag.ptrCache != nil { 98 store.pointerPool = tag.ptrCache.Get() 99 } 100 101 err = parseRoot(bs[i:], store) 102 103 return 104 } 105 106 //Marshal []byte 107 func Marshal(in interface{}) (bs []byte, err error) { 108 defer func() { 109 if e := recover(); e != nil { 110 if err1, ok := e.(*lxterrs.Code); ok { 111 err = err1 112 } else { 113 err = lxterrs.New("%+v", e) 114 } 115 return 116 } 117 }() 118 119 pbs := bsPool.Get().(*[]byte) 120 bs = *pbs 121 var lLeft int32 = 1024 122 defer func() { 123 if cap(bs)-len(bs) >= int(lLeft) { 124 *pbs = bs[len(bs):] 125 bs = bs[:len(bs):len(bs)] 126 bsPool.Put(pbs) 127 } 128 }() 129 130 if mIn, ok := in.(*interface{}); ok { 131 bs = marshalInterface(bs[:0], *mIn) 132 return 133 } 134 if mIn, ok := in.(*map[string]interface{}); ok { 135 bs = marshalMapInterface(bs[:0], *mIn) 136 return 137 } 138 if _, ok := in.(*[]interface{}); ok { 139 140 return 141 } 142 143 vi := reflect.Indirect(reflect.ValueOf(in)) 144 if !vi.CanSet() { 145 err = fmt.Errorf("%T cannot set", in) 146 return 147 } 148 prv := reflectValueToValue(&vi) 149 goType := prv.typ 150 tag, err := LoadTagNode(vi, goType.Hash) 151 if err != nil { 152 return 153 } 154 155 store := Store{ 156 tag: tag, 157 obj: prv.ptr, // eface.Value, 158 } 159 160 bs = marshalStruct(store, bs[:0]) 161 162 l := int32(len(bs)) 163 lLeft = atomic.LoadInt32(&tag.bsMarshalLen) 164 if lLeft > l*2 { 165 bsHaftCount := atomic.AddInt32(&tag.bsHaftCount, -1) 166 if bsHaftCount < 1000 { 167 atomic.StoreInt32(&tag.bsMarshalLen, l) 168 lLeft = l 169 } 170 } else if lLeft < l { 171 atomic.StoreInt32(&tag.bsMarshalLen, l) 172 lLeft = l 173 } else { 174 atomic.AddInt32(&tag.bsHaftCount, 1) 175 } 176 return 177 }