-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[Feature]: Support custom json codec at runtime #3391
base: master
Are you sure you want to change the base?
Conversation
@appleboy Please take a look, thanks~ |
160b21c
to
965e7b1
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3391 +/- ##
==========================================
- Coverage 99.21% 99.19% -0.03%
==========================================
Files 42 43 +1
Lines 3182 2722 -460
==========================================
- Hits 3157 2700 -457
+ Misses 17 12 -5
- Partials 8 10 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
965e7b1
to
0b20d8b
Compare
good job! I need the feature. |
2679a12
to
ec8ebbb
Compare
@timandy We move to the next milestone v1.11 |
I recommend removing all third-party JSON packages after defining the JsonApi to reduce dependencies. We should not piling json libraries |
Agree with this, I don't want to see four different json implementations in one web framework. |
@timandy Can you create a new PR? I can't handle this PR (maybe no permission to trigger CI testing) |
@appleboy All right, I will rebase on the latest commit. |
@timandy Thanks. |
@aohanhongzhi I have rebased it, Please tabke a look. |
related: #3766 |
@appleboy I would also like to see this get merged so I can start using my own custom json marshallers in gin v1 immediately. It could take a year or more to get encoding/json/v2 released and even longer than that for gin v2, so it makes sense to have this merged and released now to give all of us the option to customize marshal/unmarshal for everyone using gin v1 currently |
@takanuva15 I will take it. |
var Api API | ||
|
||
// API the api for json codec. | ||
type API interface { |
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.
Some naming rules:
- type API interface -> type Core interface
- var Api API -> var API Core
|
||
Gin support custom json serialization and deserialization logic without using compile tags. | ||
|
||
1. Define a custom struct implements the `json.API` interface. |
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.
json.Core
|
||
1. Define a custom struct implements the `json.API` interface. | ||
|
||
2. Before your engine starts, assign values to `json.Api` using the custom struct. |
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.
json.API
Extract out an interface
api.JsonApi
to define all serialization and deserialization behaviors.Developers only need to implement the interface and set the value of
json.API
to custom json codec.In the future, we can define
api.XmlApi
in the same way.Solved #3217