-
Notifications
You must be signed in to change notification settings - Fork 0
add custom JSON marshaling for DefaultMetadata #72
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
Conversation
🦋 Changeset detectedLatest commit: 6ad0a2b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
59ecf68
to
6137389
Compare
6137389
to
a167228
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You got a lint error
|
||
// MarshalJSON can handle both JSON and non-JSON data in the Data field. | ||
// Without custom marshaling, the Data field would be double-encoded if it contains JSON. | ||
func (dm DefaultMetadata) MarshalJSON() ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then unmarshaling would not work right since the Json can't be converted back to a string unless we implement Unmarshaljson too? Let me check with @giogam to understand this a bit more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point - we'd need to implement that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @akuzni2! Can you provide more context on why this is necessary? The Data field is intended to store a JSON-encoded string representing a domain-specific metadata type, so there shouldn’t be a need to store non-JSON data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there shouldn’t be a need to store non-JSON data.
This is I believe the intent as well.
Without correct serialization JSON is double escaped.
{
"contractMetadataStore": {
"records": [
{
"address": "0x1234",
"chainSelector": 3478487238524512106,
"metadata": {
"data": "{\"content\":{\"metadata\":{\"deployBlock\":150906864},\"view\":{\"field1\":[\"0x1234567890abcdef\"],\"field2\":{\"key1\":\"value1\"}}}}"
}
}
]
},
}
With this custom serializer in this PR
{
"contractMetadataStore": {
"records": [
{
"address": "0x1234",
"chainSelector": 3478487238524512106,
"metadata": {
"data": {
"content": {
"metadata": {
"deployBlock": 150906864
},
"view": {
"field1": [
"0x1234567890abcdef"
],
"field2": {
"key1": "value1"
}
}
}
}
}
}
]
},
}
Isn't this the intended format if the whole output should be serialized to JSON?
The double escaping is happening now because ToDefault does a cast string(data)
Data: string(jsonData), |
if the Data
field of DefaultMetadata is really intended to be JSON - we should change this to Go's json.RawMessage which will handle the double encoding for us.
So theres 2 choices:
- custom serialization (implemented in PR)
- break the data-type and hope no one is using it directly and go and update it everywhere
Thanks Alex, I will leave this to @giogam as he has a lot more context on the design of datastore. |
Closing this, as we ended up taking a different approach. |
Added a custom
MarshalJSON
method toDefaultMetadata
. This ensures that if theData
field contains JSON, it is not double-encoded, while non-JSON data is marshaled as-is.