Open shuqingzai opened 2 weeks ago
@shuqingzai 我粗略看了下,这里有两种改进方案。
Server
增加编解码接口定义这种方案不需要动框架源码,灵活性比较高,推荐这种。
通过前置中间件获取Body
数据进行解析([]byte->map[string]any
),并设置到自定义请求参数中,并清空Body
数据。
通过后置中间件编码获取到的返回对象,对该对象进行自定义编码。
json
编解码接口目前框架的所有json
编解码都已经封装到了这个未公开的内部包https://github.com/gogf/gf/blob/2461ef9f29c6d72e316bc0f695d8901ce379cb00/internal/json/json.go 我们只需要对其进行接口化解耦改造,然后在某个地方定义一个设置编解码接口实现的方法(想好放哪儿),该方法内部再设置这个内部json
包的接口实现就可以了。
看你觉得哪种比较适合你的场景呢?
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
@shuqingzai I took a quick look and found two improvement options.
Server
This solution does not require touching the framework source code and is highly flexible. It is recommended.
Obtain the Body
data through the front-end middleware and parse it ([]byte->map[string]any
), set it to the custom request parameters, and clear the Body
data.
Customize the returned object obtained through post-middleware encoding.
json
encoding and decoding interfaceAt present, all the json
codecs of the framework have been encapsulated into this undisclosed internal package https://github.com/gogf/gf/blob/2461ef9f29c6d72e316bc0f695d8901ce379cb00/internal/json/json.go. We only need to interface it. Decoupling transformation, then define a method to set the encoding and decoding interface implementation somewhere (think about where to put it), and then set the interface implementation of the internal json
package inside the method.
Which one do you think is more suitable for your scenario?
@gqcn 抱歉,可能我的描述比较粗糙,导致表达不清晰,我的本意是参考 gRPC
的做法,抽象统一的 encoding.Codec
,项目内部使用各种序列化时,都是调用该包即可,这与 #3121 所提出的建议是一致,开发者/GF 内部(特别是 conv
或 gjson
)需要序列化与反序列化数据时,都使用统一的方式,以此保证它们的处理结果一致性,用户也可以自行注册自己的序列化器
// yaml
encoding.GetCodec(yaml.Name).Marshal() || encoding.GetCodec(yaml.Name).Unmarshal()
// json
encoding.GetCodec(json.Name).Marshal() || encoding.GetCodec(json.Name).Unmarshal()
// more...
遇到的问题
在 ghttp 中,默认使用标准库的 JSON 包,参考: https://github.com/gogf/gf/blob/e3e5c89ba7df6ae26823927bcab8cc919dbc8f3a/net/ghttp/ghttp_request_param.go#L242
会导致一些 HTML 标签被转义之类的问题,然后在 conv
转换中又进行一次十分复杂转换,导致我最终获取到的业务参数可能与客户端提交不一致,我希望 GF 可以统一这些参数转换的方式,保证其结果的一致性
比如 JSON ,在我的项目中,统一不需要进行 HTML 标签 或 Unicode 的转义,我可能会自行实现序列化器,设置一些额外参数,甚至我们正在将 JSON 序列化的工作迁移至 bytedance/sonic 以获取更好的性能,因为我们项目 90% 以上的数据序列化都依赖 JSON
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
@gqcn Sorry, maybe my description is rough, resulting in unclear expression. My original intention is to refer to the practice of
gRPC
, the abstract and unifiedencoding.Codec
. When various serializations are used within the project, this package is called That's it, this is consistent with the suggestion made in #3121. Developers/GF internally (especiallyconv
orgjson
) need to use a unified method to serialize and deserialize data to ensure that they are The processing results are consistent. Users can also register their own serializers.
//yaml
encoding.GetCodec(yaml.Name).Marshal() || encoding.GetCodec(yaml.Name).Unmarshal()
//json
encoding.GetCodec(json.Name).Marshal() || encoding.GetCodec(json.Name).Unmarshal()
// more...
Problems encountered
In ghttp, the JSON package of the standard library is used by default. Reference: https://github.com/gogf/gf/blob/e3e5c89ba7df6ae26823927bcab8cc919dbc8f3a/net/ghttp/ghttp_request_param.go#L242
It will cause problems such as some HTML tags being escaped, and then a very complicated conversion is performed in the conv
conversion. As a result, the business parameters I finally obtain may be inconsistent with those submitted by the client. I hope GF can unify these parameter conversions. way to ensure the consistency of the results
For example, JSON. In my project, there is no need to escape HTML tags. I may implement the serializer myself and set some additional parameters. We are even migrating the JSON serialization work to bytedance/sonic for better performance, because more than 90% of the data serialization in our project relies on JSON
https://github.com/gogf/gf/issues/3851#issuecomment-2406383697
@shuqingzai 可以,挺不错的建议。可以收归所有的编解码(比如json/xml/yaml
等)到一个注册机中,开发者也可以自定义编解码器到注册机中。这块也欢迎大佬一起来共建。
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
https://github.com/gogf/gf/issues/3851#issuecomment-2406383697
@shuqingzai Yes, that’s a pretty good suggestion. All codecs (such as json/xml/yaml
, etc.) can be collected into a registration machine, and developers can also customize codecs into the registration machine. Big guys are also welcome to come and build this together.
Hello @shuqingzai. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it! 你好 @shuqingzai。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。
Is your feature request related to a problem?
Option Yes
Describe the solution you'd like
目前 GF 对 JSON 的处理都是使用标准库的包,无法自定义实现,特别是
net/ghttp
的request
与response
希望
net/ghttp
、gjson
对 JSON 序列化可以自定义使用的包或函数,GF 可给出注册的规范,用户自定义注册即可这是一个十分有用的功能,开发者可以使用自己喜欢的 JSON 包进行处理
Describe alternatives you've considered
无
Additional
No response