Skip to content

feat: add custom json marshaler for sdk.Statement #1936

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions sdk/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ func (a Assertion) GetHash() ([]byte, error) {
return ocrypto.SHA256AsHex(transformedJSON), nil
}

func (s *Statement) MarshalJSON() ([]byte, error) {
// Define a custom struct for serialization
type Alias Statement
aux := &struct {
Value interface{} `json:"value,omitempty"`
*Alias
}{
Alias: (*Alias)(s),
}

if s.Format == "json-structured" {
raw := json.RawMessage(s.Value)
var tmp interface{}
// Attempt to decode Value to validate it's actual json
if err := json.Unmarshal(raw, &tmp); err == nil {
aux.Value = raw
} else {
aux.Value = s.Value
}
} else {
aux.Value = s.Value
}
Comment on lines +136 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed our implementation of assertions compared to our spec, and it looks like we missed the enum of the statement.format. I've raised this with our architects, and we're going to review the alignment of the ZTDF/BaseTDF spec with the official SDKs (Go, Java, and Web).

I'd suggest implementing the marshaling within your PEP to support this in the short term. Something like:

type MyStatement struct {
  opentdf.Statement
}

func (s *MyStatement) MarshalJSON() ([]byte, error) {
  // ...
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively you should be able to just marshal the data since we have the json tags on the Statement.


return json.Marshal(aux)
}

func (s *Statement) UnmarshalJSON(data []byte) error {
// Define a custom struct for deserialization
type Alias Statement
Expand Down