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 json.Core 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 xml.Core 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 98.99%. Comparing base (3dc1cd6) to head (d26e68b).
Report is 86 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3391      +/-   ##
==========================================
- Coverage   99.21%   98.99%   -0.22%     
==========================================
  Files          42       44       +2     
  Lines        3182     3393     +211     
==========================================
+ Hits         3157     3359     +202     
- Misses         17       24       +7     
- Partials        8       10       +2     
Flag Coverage Δ
?
-tags "sonic avx" 98.99% <100.00%> (?)
-tags go_json 98.99% <100.00%> (?)
-tags nomsgpack 98.98% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.22 98.99% <100.00%> (?)
go-1.23 98.98% <100.00%> (?)
macos-latest 98.99% <100.00%> (-0.23%) ⬇️
ubuntu-latest 98.98% <100.00%> (-0.24%) ⬇️

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
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

docs/doc.md Outdated

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

docs/doc.md Outdated

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

@timandy
Copy link
Author

timandy commented Dec 30, 2024

cc @appleboy

@appleboy
Copy link
Member

@timandy Please try to rebase the latest master branch to fix the lint error.

@timandy
Copy link
Author

timandy commented Dec 31, 2024

Hi @appleboy , rebase has done.

@appleboy
Copy link
Member

testing error.

@timandy
Copy link
Author

timandy commented Jan 2, 2025

Hi @appleboy , The testing error was not caused by this PR, it has already occurred in previous version 3f818c3.

@timandy timandy requested a review from appleboy January 8, 2025 03:17
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