diff options
Diffstat (limited to 'src/k8splugin/db/DB.go')
-rw-r--r-- | src/k8splugin/db/DB.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/k8splugin/db/DB.go b/src/k8splugin/db/DB.go index c8895088..d92b5953 100644 --- a/src/k8splugin/db/DB.go +++ b/src/k8splugin/db/DB.go @@ -14,6 +14,9 @@ limitations under the License. package db import ( + "encoding/json" + "reflect" + pkgerrors "github.com/pkg/errors" ) @@ -40,3 +43,21 @@ var CreateDBClient = func(dbType string) error { return pkgerrors.New(dbType + "DB not supported") } } + +// Serialize converts given data into a JSON string +func Serialize(v interface{}) (string, error) { + out, err := json.Marshal(v) + if err != nil { + return "", pkgerrors.Wrap(err, "Error serializing "+reflect.TypeOf(v).String()) + } + return string(out), nil +} + +// DeSerialize converts string to a json object specified by type +func DeSerialize(str string, v interface{}) error { + err := json.Unmarshal([]byte(str), &v) + if err != nil { + return pkgerrors.Wrap(err, "Error deSerializing "+str) + } + return nil +} |