Files
kubesphere/pkg/utils/jsonutil/jsonutils.go
KubeSphere CI Bot 447a51f08b feat: kubesphere 4.0 (#6115)
* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

---------

Signed-off-by: ci-bot <ci-bot@kubesphere.io>
Co-authored-by: ks-ci-bot <ks-ci-bot@example.com>
Co-authored-by: joyceliu <joyceliu@yunify.com>
2024-09-06 11:05:52 +08:00

47 lines
957 B
Go

/*
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package jsonutil
import (
"encoding/json"
"strings"
"k8s.io/klog/v2"
)
type JsonRawMessage []byte
func (m JsonRawMessage) Find(key string) JsonRawMessage {
var objmap map[string]json.RawMessage
err := json.Unmarshal(m, &objmap)
if err != nil {
klog.Errorf("Resolve JSON Key failed, find key =%s, err=%s",
key, err)
return nil
}
return JsonRawMessage(objmap[key])
}
func (m JsonRawMessage) ToList() []JsonRawMessage {
var lists []json.RawMessage
err := json.Unmarshal(m, &lists)
if err != nil {
klog.Errorf("Resolve JSON List failed, err=%s",
err)
return nil
}
var res []JsonRawMessage
for _, v := range lists {
res = append(res, JsonRawMessage(v))
}
return res
}
func (m JsonRawMessage) ToString() string {
res := strings.Replace(string(m[:]), "\"", "", -1)
return res
}