github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/json/json_object.go (about)

     1  package json
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/isc"
     6  	"reflect"
     7  	"strings"
     8  )
     9  
    10  type Object struct {
    11  	ValueMap     map[string]any
    12  	ValueDeepMap map[string]any
    13  }
    14  
    15  func (jsonObject *Object) Load(jsonContent string) error {
    16  	if jsonObject.ValueMap == nil {
    17  		jsonObject.ValueMap = make(map[string]any)
    18  	}
    19  
    20  	if jsonObject.ValueDeepMap == nil {
    21  		jsonObject.ValueDeepMap = make(map[string]any)
    22  	}
    23  
    24  	yamlStr, _ := isc.JsonToYaml(jsonContent)
    25  	property, _ := isc.YamlToProperties(yamlStr)
    26  	valueMap, _ := isc.PropertiesToMap(property)
    27  	jsonObject.ValueMap = valueMap
    28  
    29  	yamlMap, _ := isc.YamlToMap(yamlStr)
    30  	jsonObject.ValueDeepMap = yamlMap
    31  	return nil
    32  }
    33  
    34  func (jsonObject *Object) Put(key string, value any) {
    35  	if nil == value {
    36  		return
    37  	}
    38  
    39  	if oldValue, exist := jsonObject.ValueMap[key]; exist {
    40  		if reflect.TypeOf(oldValue) != reflect.TypeOf(oldValue) {
    41  			return
    42  		}
    43  	}
    44  	jsonObject.ValueMap[key] = value
    45  	jsonObject.doPut(key, value)
    46  }
    47  
    48  func (jsonObject *Object) doPut(key string, value any) {
    49  	innerPutValue(jsonObject.ValueDeepMap, key, value)
    50  }
    51  
    52  func innerPutValue(valueMap map[string]any, key string, newValue any) {
    53  	if nil == valueMap {
    54  		valueMap = make(map[string]any)
    55  	}
    56  	if !strings.Contains(key, ".") {
    57  		if oldValue, exist := valueMap[key]; exist {
    58  			if reflect.TypeOf(oldValue) != reflect.TypeOf(newValue) {
    59  				return
    60  			}
    61  		}
    62  		valueMap[key] = newValue
    63  		return
    64  	}
    65  
    66  	lastIndex := strings.Index(key, ".")
    67  	startKey := key[:lastIndex]
    68  	endKey := key[lastIndex+1:]
    69  	if oldValue, exist := valueMap[startKey]; exist {
    70  		if reflect.TypeOf(oldValue).Kind() != reflect.Map {
    71  			return
    72  		} else {
    73  			oldValueMap := isc.ToMap(oldValue)
    74  			innerPutValue(oldValueMap, endKey, newValue)
    75  			valueMap[startKey] = oldValueMap
    76  		}
    77  	} else {
    78  		oldValueMap := make(map[string]any)
    79  		innerPutValue(oldValueMap, endKey, newValue)
    80  		valueMap[startKey] = oldValueMap
    81  	}
    82  }
    83  
    84  func (jsonObject *Object) Get(key string) any {
    85  	return jsonObject.doGet(jsonObject.ValueDeepMap, key)
    86  }
    87  
    88  func (jsonObject *Object) initValue(key string) {
    89  	jsonObject.doInitValue(jsonObject.ValueDeepMap, key)
    90  }
    91  
    92  func (jsonObject *Object) doInitValue(parentValue any, key string) {
    93  	if key == "" {
    94  		return
    95  	}
    96  	parentValueKind := reflect.ValueOf(parentValue).Kind()
    97  	if parentValueKind == reflect.Map {
    98  		keys := strings.SplitN(key, ".", 2)
    99  		v1 := reflect.ValueOf(parentValue).MapIndex(reflect.ValueOf(keys[0]))
   100  		emptyValue := reflect.Value{}
   101  		if v1 == emptyValue {
   102  			return
   103  		}
   104  		if len(keys) == 1 {
   105  			jsonObject.doInitValue(v1.Interface(), "")
   106  		} else {
   107  			jsonObject.doInitValue(v1.Interface(), fmt.Sprintf("%v", keys[1]))
   108  		}
   109  	}
   110  	return
   111  }
   112  
   113  func (jsonObject *Object) doGet(parentValue any, key string) any {
   114  	if key == "" {
   115  		return parentValue
   116  	}
   117  	parentValueKind := reflect.ValueOf(parentValue).Kind()
   118  	if parentValueKind == reflect.Map {
   119  		keys := strings.SplitN(key, ".", 2)
   120  		v1 := reflect.ValueOf(parentValue).MapIndex(reflect.ValueOf(keys[0]))
   121  		emptyValue := reflect.Value{}
   122  		if v1 == emptyValue {
   123  			return nil
   124  		}
   125  		if len(keys) == 1 {
   126  			return jsonObject.doGet(v1.Interface(), "")
   127  		} else {
   128  			return jsonObject.doGet(v1.Interface(), fmt.Sprintf("%v", keys[1]))
   129  		}
   130  	}
   131  	return nil
   132  }
   133  
   134  func (jsonObject *Object) GetString(key string) string {
   135  	return isc.ToString(jsonObject.Get(key))
   136  }
   137  
   138  func (jsonObject *Object) GetInt(key string) int {
   139  	return isc.ToInt(jsonObject.Get(key))
   140  }
   141  
   142  func (jsonObject *Object) GetInt8(key string) int8 {
   143  	return isc.ToInt8(jsonObject.Get(key))
   144  }
   145  
   146  func (jsonObject *Object) GetInt16(key string) int16 {
   147  	return isc.ToInt16(jsonObject.Get(key))
   148  }
   149  
   150  func (jsonObject *Object) GetInt32(key string) int32 {
   151  	return isc.ToInt32(jsonObject.Get(key))
   152  }
   153  
   154  func (jsonObject *Object) GetInt64(key string) int64 {
   155  	return isc.ToInt64(jsonObject.Get(key))
   156  }
   157  
   158  func (jsonObject *Object) GetUInt(key string) uint {
   159  	return isc.ToUInt(jsonObject.Get(key))
   160  }
   161  
   162  func (jsonObject *Object) GetUInt8(key string) uint8 {
   163  	return isc.ToUInt8(jsonObject.Get(key))
   164  }
   165  
   166  func (jsonObject *Object) GetUInt16(key string) uint16 {
   167  	return isc.ToUInt16(jsonObject.Get(key))
   168  }
   169  
   170  func (jsonObject *Object) GetUInt32(key string) uint32 {
   171  	return isc.ToUInt32(jsonObject.Get(key))
   172  }
   173  
   174  func (jsonObject *Object) GetUInt64(key string) uint64 {
   175  	return isc.ToUInt64(jsonObject.Get(key))
   176  }
   177  
   178  func (jsonObject *Object) GetFloat32(key string) float32 {
   179  	return isc.ToFloat32(jsonObject.Get(key))
   180  }
   181  
   182  func (jsonObject *Object) GetFloat64(key string) float64 {
   183  	return isc.ToFloat64(jsonObject.Get(key))
   184  }
   185  
   186  func (jsonObject *Object) GetBool(key string) bool {
   187  	return isc.ToBool(jsonObject.Get(key))
   188  }
   189  
   190  func (jsonObject *Object) GetObject(key string, targetPtrObj any) error {
   191  	data := jsonObject.Get(key)
   192  	err := isc.DataToObject(data, targetPtrObj)
   193  	if err != nil {
   194  		return err
   195  	}
   196  	return nil
   197  }
   198  
   199  func (jsonObject *Object) GetArray(key string) []any {
   200  	var arrayResult = []any{}
   201  	data := jsonObject.Get(key)
   202  	err := isc.DataToObject(data, &arrayResult)
   203  	if err != nil {
   204  		return arrayResult
   205  	}
   206  	return arrayResult
   207  }