mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
* special handling for non-json types * added tests for json MarshalObject * another attempt * Fix marshal/unmarshal code for VERSION bucket * use short form * don't discard err * fix the json_test.go * remove duplicate string * added uuid tests * updated case for strings Co-authored-by: zees-dev <dev.786zshan@gmail.com>
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package boltdb
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
)
|
|
|
|
// MarshalObject encodes an object to binary format
|
|
func MarshalObject(object interface{}) ([]byte, error) {
|
|
// Special case for the VERSION bucket. Here we're not using json
|
|
if v, ok := object.(string); ok {
|
|
return []byte(v), nil
|
|
}
|
|
|
|
return json.Marshal(object)
|
|
}
|
|
|
|
// UnmarshalObject decodes an object from binary data
|
|
func UnmarshalObject(data []byte, object interface{}) error {
|
|
// Special case for the VERSION bucket. Here we're not using json
|
|
// So we need to return it as a string
|
|
err := json.Unmarshal(data, object)
|
|
if err != nil {
|
|
if s, ok := object.(*string); ok {
|
|
*s = string(data)
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UnmarshalObjectWithJsoniter decodes an object from binary data
|
|
// using the jsoniter library. It is mainly used to accelerate environment(endpoint)
|
|
// decoding at the moment.
|
|
func UnmarshalObjectWithJsoniter(data []byte, object interface{}) error {
|
|
var jsoni = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
return jsoni.Unmarshal(data, &object)
|
|
}
|