diff --git a/gjson_hack/precision.go b/gjson_hack/precision.go index 9423e5e..d2c3461 100644 --- a/gjson_hack/precision.go +++ b/gjson_hack/precision.go @@ -143,14 +143,21 @@ func Any(sourceValue gjson.Result) (any, error) { func getRealDataType(dataType consts.DataType, sourceValue gjson.Result) consts.DataType { // 指针数据类型, 转换为普通数据类型处理 dataType = consts.DataType(strings.TrimLeft(dataType.String(), "*")) - if dataType == consts.DataTypeAny { + // 没指定数据类型或者数据类型是any + if dataType == consts.DataTypeAny || dataType == "" { if sourceValue.IsObject() { dataType = consts.DataTypeMapAnyAny } else if sourceValue.IsArray() { dataType = consts.DataTypeSliceAny + } else if sourceValue.IsBool() { + dataType = consts.DataTypeBool } else { - if sourceValue.Num != 0 { + if sourceValue.Type == gjson.Number { dataType = consts.DataTypeFloat64 + } else if sourceValue.Type == gjson.String { + dataType = consts.DataTypeString + } else { + dataType = consts.DataTypeAny } } } @@ -362,9 +369,9 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any) dataType = getRealDataType(dataType, sourceValue) strVal := wrapper.String(sourceValue.String()) switch dataType { - case consts.DataTypeInt: + case consts.DataTypeInt, consts.DataTypeInt8, consts.DataTypeInt16, consts.DataTypeInt32, consts.DataTypeInt64: return Int(sourceValue) - case consts.DataTypeUint: + case consts.DataTypeUint, consts.DataTypeUint8, consts.DataTypeUint16, consts.DataTypeUint32, consts.DataTypeUint64: return Uint(sourceValue) case consts.DataTypeFloat64, consts.DataTypeFloat32: return Float64(sourceValue) @@ -374,6 +381,7 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any) case consts.DataTypeString: return sourceValue.String(), nil case consts.DataTypeAny: + // 经过getRealDataType类型处理后依旧是any,不考虑精度问题 return sourceValue.Value(), nil case consts.DataTypeSliceAny: // 任意类型的list