diff --git a/assetid.go b/assetid.go index ce5a6fc..f8434af 100644 --- a/assetid.go +++ b/assetid.go @@ -1,6 +1,8 @@ package identifiers import ( + "encoding/json" + "github.com/pkg/errors" ) @@ -108,7 +110,7 @@ func (asset AssetID) Identifier() (AssetIdentifier, error) { // MarshalText implements the encoding.TextMarshaler interface for AssetID func (asset AssetID) MarshalText() ([]byte, error) { - return []byte("0x" + string(asset)), nil + return []byte(asset.String()), nil } // UnmarshalText implements the encoding.TextUnmarshaler interface for AssetID @@ -129,3 +131,34 @@ func (asset *AssetID) UnmarshalText(text []byte) error { return nil } + +// MarshalJSON implements the json.Marshaler interface for AssetID +func (asset AssetID) MarshalJSON() ([]byte, error) { + return json.Marshal(asset.String()) +} + +// UnmarshalJSON implements the json.Unmarshaler interface for AssetID +func (asset *AssetID) UnmarshalJSON(data []byte) error { + var decoded string + + // Decode the JSON data into a string + if err := json.Unmarshal(data, &decoded); err != nil { + return err + } + + // Assert that the 0x prefix exists + if !has0xPrefixString(decoded) { + return ErrMissing0xPrefix + } + + // Trim the 0x prefix + decoded = trim0xPrefixString(decoded) + // Generate an identifier for the AssetID + if _, err := AssetID(decoded).Identifier(); err != nil { + return err + } + + *asset = AssetID(decoded) + + return nil +} diff --git a/common.go b/common.go index 8030723..cb81911 100644 --- a/common.go +++ b/common.go @@ -16,6 +16,10 @@ func trim0xPrefixString(value string) string { return strings.TrimPrefix(value, prefix0xString) } +func has0xPrefixString(value string) bool { + return strings.HasPrefix(value, prefix0xString) +} + func trim0xPrefixBytes(value []byte) []byte { return bytes.TrimPrefix(value, prefix0xBytes) } diff --git a/logicid.go b/logicid.go index 80f6f7d..c795ad2 100644 --- a/logicid.go +++ b/logicid.go @@ -1,6 +1,8 @@ package identifiers import ( + "encoding/json" + "github.com/pkg/errors" ) @@ -110,7 +112,7 @@ func (logic LogicID) Identifier() (LogicIdentifier, error) { // MarshalText implements the encoding.TextMarshaler interface for LogicID func (logic LogicID) MarshalText() ([]byte, error) { - return []byte("0x" + string(logic)), nil + return []byte(logic.String()), nil } // UnmarshalText implements the encoding.TextUnmarshaler interface for LogicID @@ -131,3 +133,34 @@ func (logic *LogicID) UnmarshalText(text []byte) error { return nil } + +// MarshalJSON implements the json.Marshaler interface for LogicID +func (logic LogicID) MarshalJSON() ([]byte, error) { + return json.Marshal(logic.String()) +} + +// UnmarshalJSON implements the json.Unmarshaler interface for LogicID +func (logic *LogicID) UnmarshalJSON(data []byte) error { + var decoded string + + // Decode the JSON data into a string + if err := json.Unmarshal(data, &decoded); err != nil { + return err + } + + // Assert that the 0x prefix exists + if !has0xPrefixString(decoded) { + return ErrMissing0xPrefix + } + + // Trim the 0x prefix + decoded = trim0xPrefixString(decoded) + // Generate an identifier for the AssetID + if _, err := LogicID(decoded).Identifier(); err != nil { + return err + } + + *logic = LogicID(decoded) + + return nil +}