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

[Feature]: Support custom json codec at runtime #3391

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

timandy
Copy link

@timandy timandy commented Nov 10, 2022

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

@timandy
Copy link
Author

timandy commented Nov 10, 2022

@appleboy Please take a look, thanks~

@codecov
Copy link

codecov bot commented Apr 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.19%. Comparing base (3dc1cd6) to head (2118c7c).
Report is 63 commits behind head on master.

Current head 2118c7c differs from pull request most recent head eeaa150

Please upload reports for the commit eeaa150 to get more accurate results.

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     
Flag Coverage Δ
?
-tags "sonic avx" 99.18% <100.00%> (?)
-tags go_json 99.18% <100.00%> (?)
-tags nomsgpack 99.17% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 99.19% <100.00%> (-0.03%) ⬇️
go-1.22 99.19% <100.00%> (?)
macos-latest 99.19% <100.00%> (-0.03%) ⬇️
ubuntu-latest 99.19% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@timandy timandy force-pushed the master branch 3 times, most recently from 965e7b1 to 0b20d8b Compare April 27, 2023 08:34
@aohanhongzhi
Copy link

good job! I need the feature.

@thinkerou thinkerou mentioned this pull request Nov 1, 2023
@timandy timandy force-pushed the master branch 2 times, most recently from 2679a12 to ec8ebbb Compare April 1, 2024 03:35
@appleboy appleboy added this to the v1.11 milestone Apr 1, 2024
@appleboy
Copy link
Member

appleboy commented Apr 1, 2024

@timandy We move to the next milestone v1.11

@fifsky
Copy link
Contributor

fifsky commented Apr 1, 2024

I recommend removing all third-party JSON packages after defining the JsonApi to reduce dependencies.
Related discussion: #3653 (comment)

We should not piling json libraries

@devhaozi
Copy link

devhaozi commented May 7, 2024

I recommend removing all third-party JSON packages after defining the JsonApi to reduce dependencies. Related discussion: #3653 (comment)

We should not piling json libraries

Agree with this, I don't want to see four different json implementations in one web framework.

@appleboy
Copy link
Member

appleboy commented May 7, 2024

@timandy Can you create a new PR? I can't handle this PR (maybe no permission to trigger CI testing)

@timandy
Copy link
Author

timandy commented May 8, 2024

@appleboy All right, I will rebase on the latest commit.

@appleboy
Copy link
Member

appleboy commented May 8, 2024

@timandy Thanks.

@timandy
Copy link
Author

timandy commented May 8, 2024

@aohanhongzhi I have rebased it, Please tabke a look.

@appleboy
Copy link
Member

appleboy commented May 9, 2024

related: #3766

@takanuva15
Copy link
Contributor

@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

@appleboy
Copy link
Member

@takanuva15 I will take it.

appleboy
appleboy previously approved these changes Dec 26, 2024
codec/api/json.go Outdated Show resolved Hide resolved
@appleboy appleboy requested a review from thinkerou December 26, 2024 23:43
var Api API

// API the api for json codec.
type API interface {
Copy link
Member

Choose a reason for hiding this comment

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

Some naming rules:

  1. type API interface -> type Core interface
  2. 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.
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

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

json.API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants