Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Serialization: Change Libraries #3225

Merged
merged 12 commits into from
Oct 19, 2023
Prev Previous commit
Next Next commit
Add marshal error type and marshal helper function that wraps json-it…
…erator marshal call
  • Loading branch information
bsardo committed Oct 16, 2023
commit 0fcd2f8fc84899cc3161d45b5dcf8f901518637c
1 change: 1 addition & 0 deletions errortypes/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
MalformedAcctErrorCode
ModuleRejectionErrorCode
TmaxTimeoutErrorCode
FailedToMarshalErrorCode
FailedToUnmarshalErrorCode
)

Expand Down
17 changes: 17 additions & 0 deletions errortypes/errortypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,20 @@ func (err *FailedToUnmarshal) Code() int {
func (err *FailedToUnmarshal) Severity() Severity {
return SeverityFatal
}

// FailedToMarshal should be used to represent errors that occur when marshaling to a byte slice.
type FailedToMarshal struct {
Message string
}

func (err *FailedToMarshal) Error() string {
return err.Message
}

func (err *FailedToMarshal) Code() int {
return FailedToMarshalErrorCode
}

func (err *FailedToMarshal) Severity() Severity {
return SeverityFatal
}
12 changes: 12 additions & 0 deletions util/jsonutil/jsonutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,15 @@ func UnmarshalValid(data []byte, v interface{}) error {
}
return nil
}

// Marshal marshals a data structure into a byte slice without performing any validation
// on the data. A marshal error is returned if a non-validation error occurs.
func Marshal(v interface{}) ([]byte, error) {
data, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(v)
if err != nil {
return nil, &errortypes.FailedToMarshal{
Message: err.Error(),
}
}
return data, nil
}