You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm looking for a way to use https://github.com/google/jsonapi as a JSON (un)serializer. To be able to have custom bindings would be great but even though I looked for way to manage this, I couldn't guess how.
My idea then was to use a middleware to achieve this.
set a custom "resource" value (a model) in the context by using a custom middleware
populate this model by unmarshaling the request body in a common/generic middleware
I really like this pattern as it makes my deserializing logic reusable but I'm going to add my own logic for validation too and it does not make sense as Gin binding are supposed to be the right way.
The text was updated successfully, but these errors were encountered:
As an alternate solution to the above, you can also customize how json serialization occurs by defining a custom renderer and then passing that to the c.Render function. There is a detailed comment here.
Basically, instead of calling c.JSON, you would call c.Render(200, WithJSONv2(res)). (The implementation of WithJSONv2 is in the linked comment)
Note: This doesn't apply globally, so you would need to invoke it wherever you are returning a response
I'm looking for a way to use https://github.com/google/jsonapi as a JSON (un)serializer. To be able to have custom bindings would be great but even though I looked for way to manage this, I couldn't guess how.
My idea then was to use a middleware to achieve this.
Here is what my I made to make it working:
In main.go
The JsonAPI middleware:
Then in my handler I can get the unmarshaled struct doing that:
I really like this pattern as it makes my deserializing logic reusable but I'm going to add my own logic for validation too and it does not make sense as Gin binding are supposed to be the right way.
The text was updated successfully, but these errors were encountered: