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

codec: allow user to override default JSON serializer (#157) #159

Closed

Conversation

Andrew-M-C
Copy link
Contributor

As an alternative of #157, not removing json-iterator, but allow user to override default JSON serializer.

Copy link

codecov bot commented Jan 16, 2024

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (65a60b1) 85.52917% compared to head (26b9e07) 85.60010%.

Files Patch % Lines
restful/serialize_jsonpb.go 46.15385% 6 Missing and 1 partial ⚠️
codec/serialization_json.go 0.00000% 4 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                 @@
##                main        #159         +/-   ##
===================================================
+ Coverage   85.52917%   85.60010%   +0.07093%     
===================================================
  Files            189         189                 
  Lines          16129       16139         +10     
===================================================
+ Hits           13795       13815         +20     
+ Misses          1774        1762         -12     
- Partials         560         562          +2     
Flag Coverage Δ
unittests 85.60010% <35.29412%> (+0.07093%) ⬆️

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.

jsoniter "github.com/json-iterator/go"
)

// JSONAPI is json packing and unpacking object, users can change
// the internal parameter.
var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary
var JSONAPI JSONSerializer = jsoniter.ConfigCompatibleWithStandardLibrary
Copy link
Contributor

Choose a reason for hiding this comment

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

The type of JSONAPI still changes.

Compatibility means the name and the type of an exported entity both cannot be changed.

Copy link
Contributor

Choose a reason for hiding this comment

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

The scenario you are using is for RESTful? Since for the trpc protocol, the json implementation used is actually codec.JSONPBSerializer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The scenario you are using is for RESTful? Since for the trpc protocol, the json implementation used is actually codec.JSONPBSerializer.

@WineChord I changed both JSONAPI simple because I saw them both defined as jsoniter typped variable. Should I just change one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type of JSONAPI still changes.

Compatibility means the name and the type of an exported entity both cannot be changed.

I modified JSONAPI just trying to follow the programming style of trpc. However I still feel confused of why JSONAPI, JSONSerialization is exportable.

Copy link
Contributor Author

@Andrew-M-C Andrew-M-C Jan 16, 2024

Choose a reason for hiding this comment

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

Comments of JSONAPI said: JSONAPI is json packing and unpacking object, users can change the internal parameter.

As JSONAPI is claimed to used to 'change', I am afraid it should be an interface instead of jsoniter-typped variable.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand your perspective, and what I'm trying to highlight here is the conundrum of strict compatibility. Strict compatibility implies that nothing about the exported entities can be altered.

Indeed, JSONAPI should not have been exported initially. The PR was submitted 4 years ago (July 16th, 2020 1:59 PM) to allow users to modify the codec.JSONAPI to a custom one (e.g., codec.JSONAPI = jsoniter.Config{EscapeHTML: false, SortMapKeys: true, ValidateJsonRawMessage: true}.Froze()). However, I fail to see why this should be incorporated into the framework rather than the user's code. Nonetheless, it was merged and has continued in this manner.

Regarding the type, it should not be the native type of jsoniter. It should be an interface, and I believe that the following would suffice for your needs:

Suggested change
var JSONAPI JSONSerializer = jsoniter.ConfigCompatibleWithStandardLibrary
var JSONAPI Serializer = jsoniter.ConfigCompatibleWithStandardLibrary

Here, codec.Serializer is an already defined interface. I believe that the framework only requires plain Marshal/Unmarshal.

However, we're still discussing the matter of strict compatibility, which means this change would still break compatibility.

It might be decided that changing the type is acceptable since the framework only uses the Marshal/Unmarshal utility. However, users may directly use other methods provided by the exported JSONAPI variable. After changing its type, users' code would break because the JSONAPI variable no longer has those methods it previously defined.

It's a tough decision.

cc @tocrafty

@Andrew-M-C
Copy link
Contributor Author

Close due to discussion in #157

@Andrew-M-C Andrew-M-C closed this Jan 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants