| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package jsonutils
- import (
- "strconv"
- "strings"
- "time"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/pkg/sortedmap"
- "yunion.io/x/pkg/util/timeutils"
- )
- type JSONPair struct {
- key string
- val JSONObject
- }
- func NewDict(objs ...JSONPair) *JSONDict {
- dict := JSONDict{data: sortedmap.NewSortedMapWithCapa(len(objs))}
- for _, o := range objs {
- dict.Set(o.key, o.val)
- }
- return &dict
- }
- func NewArray(objs ...JSONObject) *JSONArray {
- arr := JSONArray{data: make([]JSONObject, 0, len(objs))}
- for _, o := range objs {
- arr.data = append(arr.data, o)
- }
- return &arr
- }
- func NewString(val string) *JSONString {
- return &JSONString{data: val}
- }
- func NewInt(val int64) *JSONInt {
- return &JSONInt{data: val}
- }
- //deprecated
- func NewFloat(val float64) *JSONFloat {
- return &JSONFloat{data: val, bit: 64}
- }
- func NewFloat64(val float64) *JSONFloat {
- return &JSONFloat{data: val, bit: 64}
- }
- func NewFloat32(val float32) *JSONFloat {
- return &JSONFloat{data: float64(val), bit: 32}
- }
- func NewBool(val bool) *JSONBool {
- if val {
- return JSONTrue
- }
- return JSONFalse
- }
- func (this *JSONDict) Set(key string, value JSONObject) {
- this.data = sortedmap.Add(this.data, key, value)
- }
- func (this *JSONDict) Remove(key string) bool {
- return this.remove(key, true)
- }
- func (this *JSONDict) RemoveIgnoreCase(key string) bool {
- someRemoved := false
- for {
- removed := this.remove(key, false)
- if !removed {
- break
- }
- if !someRemoved {
- someRemoved = true
- }
- }
- return someRemoved
- }
- func (this *JSONDict) remove(key string, caseSensitive bool) bool {
- exist := false
- if !caseSensitive {
- this.data, _, exist = sortedmap.DeleteIgnoreCase(this.data, key)
- } else {
- this.data, exist = sortedmap.Delete(this.data, key)
- }
- return exist
- }
- func (this *JSONDict) Add(o JSONObject, keys ...string) error {
- obj := this
- for i := 0; i < len(keys); i++ {
- if i == len(keys)-1 {
- obj.Set(keys[i], o)
- } else {
- o, ok := obj.data.Get(keys[i])
- if !ok || o == JSONNull {
- obj.Set(keys[i], NewDict())
- o, ok = obj.data.Get(keys[i])
- }
- if ok {
- obj, ok = o.(*JSONDict)
- if !ok {
- return ErrInvalidJsonDict
- }
- } else {
- return ErrJsonDictFailInsert
- }
- }
- }
- return nil
- }
- func (this *JSONArray) SetAt(idx int, obj JSONObject) {
- this.data[idx] = obj
- }
- func (this *JSONArray) Add(objs ...JSONObject) {
- for _, o := range objs {
- this.data = append(this.data, o)
- }
- }
- func (this *JSONValue) Contains(keys ...string) bool {
- return false
- }
- func (this *JSONValue) ContainsIgnoreCases(keys ...string) bool {
- return false
- }
- func (this *JSONValue) Get(keys ...string) (JSONObject, error) {
- return nil, ErrUnsupported
- }
- func (this *JSONValue) GetIgnoreCases(keys ...string) (JSONObject, error) {
- return nil, ErrUnsupported
- }
- func (this *JSONValue) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- return "", ErrOutOfKeyRange
- }
- return this.String(), nil
- }
- func (this *JSONValue) GetAt(i int, keys ...string) (JSONObject, error) {
- return nil, ErrUnsupported
- }
- func (this *JSONValue) Int(keys ...string) (int64, error) {
- return 0, ErrUnsupported
- }
- func (this *JSONValue) Float(keys ...string) (float64, error) {
- return 0.0, ErrUnsupported
- }
- func (this *JSONValue) Bool(keys ...string) (bool, error) {
- return false, ErrUnsupported
- }
- func (this *JSONValue) GetMap(keys ...string) (map[string]JSONObject, error) {
- return nil, ErrUnsupported
- }
- func (this *JSONValue) GetArray(keys ...string) ([]JSONObject, error) {
- return nil, ErrUnsupported
- }
- func (this *JSONDict) Contains(keys ...string) bool {
- obj, err := this._get(true, keys)
- if err == nil && obj != nil {
- return true
- }
- return false
- }
- func (this *JSONDict) ContainsIgnoreCases(keys ...string) bool {
- obj, err := this._get(false, keys)
- if err == nil && obj != nil {
- return true
- }
- return false
- }
- func (this *JSONDict) Get(keys ...string) (JSONObject, error) {
- return this._get(true, keys)
- }
- func (this *JSONDict) GetIgnoreCases(keys ...string) (JSONObject, error) {
- return this._get(false, keys)
- }
- func (this *JSONDict) _get(caseSensitive bool, keys []string) (JSONObject, error) {
- if len(keys) == 0 {
- return this, nil
- }
- for i := 0; i < len(keys); i++ {
- key := keys[i]
- var val interface{}
- var ok bool
- if caseSensitive {
- val, ok = this.data.Get(key)
- } else {
- val, _, ok = this.data.GetIgnoreCase(key)
- }
- if ok {
- if i == len(keys)-1 {
- return val.(JSONObject), nil
- } else {
- this, ok = val.(*JSONDict)
- if !ok {
- return nil, ErrInvalidJsonDict
- }
- }
- } else {
- return nil, ErrJsonDictKeyNotFound
- }
- }
- return nil, ErrOutOfKeyRange
- }
- func (this *JSONDict) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- obj, err := this.Get(keys...)
- if err != nil {
- return "", errors.Wrap(err, "Get")
- }
- return obj.GetString()
- } else {
- return this.String(), nil
- }
- }
- func (this *JSONDict) GetMap(keys ...string) (map[string]JSONObject, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return nil, errors.Wrap(err, "Get")
- }
- dict, ok := obj.(*JSONDict)
- if !ok {
- return nil, ErrInvalidJsonDict
- }
- // allocate a map to hold the return map
- ret := make(map[string]JSONObject, len(this.data))
- for iter := sortedmap.NewIterator(dict.data); iter.HasMore(); iter.Next() {
- k, v := iter.Get()
- ret[k] = v.(JSONObject)
- }
- return ret, nil
- }
- func (this *JSONArray) GetAt(i int, keys ...string) (JSONObject, error) {
- if len(keys) > 0 {
- return nil, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- if i < 0 {
- i = len(this.data) + i
- }
- if i >= 0 && i < len(this.data) {
- return this.data[i], nil
- } else {
- return nil, ErrOutOfIndexRange // fmt.Errorf("Out of range GetAt(%d)", i)
- }
- }
- func (this *JSONArray) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- return "", ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.String(), nil
- }
- func (this *JSONDict) GetAt(i int, keys ...string) (JSONObject, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return nil, errors.Wrap(err, "Get")
- }
- arr, ok := obj.(*JSONArray)
- if !ok {
- return nil, ErrInvalidJsonArray // fmt.Errorf("%s is not an Array", keys)
- }
- return arr.GetAt(i)
- }
- func (this *JSONArray) GetArray(keys ...string) ([]JSONObject, error) {
- if len(keys) > 0 {
- return nil, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- // Allocate a new array to hold the return array
- ret := make([]JSONObject, len(this.data))
- for i := range this.data {
- ret[i] = this.data[i]
- }
- return ret, nil
- }
- func (this *JSONDict) GetArray(keys ...string) ([]JSONObject, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return nil, errors.Wrap(err, "Get")
- }
- if _, ok := obj.(*JSONDict); ok {
- return nil, ErrInvalidJsonArray
- }
- return obj.GetArray()
- /* arr, ok := obj.(*JSONArray)
- if !ok {
- return nil, fmt.Errorf("%s is not an Array", keys)
- }
- return arr.GetArray() */
- }
- func _getarray(obj JSONObject, keys ...string) ([]JSONObject, error) {
- if len(keys) > 0 {
- return nil, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return []JSONObject{obj}, nil
- }
- func (this *JSONString) GetArray(keys ...string) ([]JSONObject, error) {
- return _getarray(this, keys...)
- }
- func (this *JSONInt) GetArray(keys ...string) ([]JSONObject, error) {
- return _getarray(this, keys...)
- }
- func (this *JSONFloat) GetArray(keys ...string) ([]JSONObject, error) {
- return _getarray(this, keys...)
- }
- func (this *JSONBool) GetArray(keys ...string) ([]JSONObject, error) {
- return _getarray(this, keys...)
- }
- func (this *JSONInt) Int(keys ...string) (int64, error) {
- if len(keys) > 0 {
- return 0, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.data, nil
- }
- func (this *JSONString) Int(keys ...string) (int64, error) {
- if len(keys) > 0 {
- return 0, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- val, e := strconv.ParseInt(this.data, 10, 64)
- if e != nil {
- return 0, ErrInvalidJsonInt // fmt.Errorf("Invalid number %s", this.data)
- } else {
- return val, nil
- }
- }
- func (this *JSONInt) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- return "", ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.String(), nil
- }
- func (this *JSONDict) Int(keys ...string) (int64, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return 0, errors.Wrap(err, "Get")
- }
- return obj.Int()
- /* jint, ok := obj.(*JSONInt)
- if ! ok {
- return 0, fmt.Errorf("%s is not an Int", keys)
- }
- return jint.Int() */
- }
- func (this *JSONFloat) Float(keys ...string) (float64, error) {
- if len(keys) > 0 {
- return 0.0, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.data, nil
- }
- func (this *JSONInt) Float(keys ...string) (float64, error) {
- if len(keys) > 0 {
- return 0.0, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return float64(this.data), nil
- }
- func (this *JSONString) Float(keys ...string) (float64, error) {
- if len(keys) > 0 {
- return 0.0, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- val, err := strconv.ParseFloat(this.data, 64)
- if err != nil {
- return 0.0, ErrInvalidJsonFloat // fmt.Errorf("Not a float %s", this.data)
- } else {
- return val, nil
- }
- }
- func (this *JSONFloat) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- return "", ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.String(), nil
- }
- func (this *JSONDict) Float(keys ...string) (float64, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return 0, errors.Wrap(err, "Get")
- }
- return obj.Float()
- /* jfloat, ok := obj.(*JSONFloat)
- if ! ok {
- return 0, fmt.Errorf("%s is not a float", keys)
- }
- return jfloat.Float() */
- }
- func (this *JSONBool) Bool(keys ...string) (bool, error) {
- if len(keys) > 0 {
- return false, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.data, nil
- }
- func (this *JSONString) Bool(keys ...string) (bool, error) {
- if len(keys) > 0 {
- return false, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- if strings.EqualFold(this.data, "true") || strings.EqualFold(this.data, "on") || strings.EqualFold(this.data, "yes") || this.data == "1" {
- return true, nil
- } else if strings.EqualFold(this.data, "false") || strings.EqualFold(this.data, "off") || strings.EqualFold(this.data, "no") || this.data == "0" {
- return false, nil
- } else {
- return false, ErrInvalidJsonBoolean // fmt.Errorf("Invalid boolean string %s", this.data)
- }
- }
- func (this *JSONBool) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- return "", ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.String(), nil
- }
- func (this *JSONDict) Bool(keys ...string) (bool, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return false, errors.Wrap(err, "Get")
- }
- return obj.Bool()
- /* jbool, ok := obj.(*JSONBool)
- if ! ok {
- return false, fmt.Errorf("%s is not a bool", keys)
- }
- return jbool.Bool() */
- }
- func (this *JSONValue) GetTime(keys ...string) (time.Time, error) {
- return time.Time{}, ErrUnsupported // fmt.Errorf("Unsupported operation GetTime")
- }
- func (this *JSONString) GetTime(keys ...string) (time.Time, error) {
- if len(keys) > 0 {
- return time.Time{}, ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- t, e := timeutils.ParseTimeStr(this.data)
- if e == nil {
- return t, nil
- }
- for _, tf := range []string{time.RFC3339, time.RFC1123, time.UnixDate,
- time.RFC822,
- } {
- t, e := time.Parse(tf, this.data)
- if e == nil {
- return t, nil
- }
- }
- return this.JSONValue.GetTime()
- }
- func (this *JSONString) GetString(keys ...string) (string, error) {
- if len(keys) > 0 {
- return "", ErrOutOfKeyRange // fmt.Errorf("Out of key range: %s", keys)
- }
- return this.data, nil
- }
- func (this *JSONDict) GetTime(keys ...string) (time.Time, error) {
- obj, err := this.Get(keys...)
- if err != nil {
- return time.Time{}, errors.Wrap(err, "Get")
- }
- str, ok := obj.(*JSONString)
- if !ok {
- return time.Time{}, ErrInvalidJsonString // fmt.Errorf("%s is not a string", keys)
- }
- return str.GetTime()
- }
- /*
- func (this *JSONDict) GetIgnoreCases(key ...string) (JSONObject, bool) {
- lkey := strings.ToLower(key)
- for k, v := range this.data {
- if strings.ToLower(k) == lkey {
- return v, true
- }
- }
- return nil, false
- }*/
|