nats的安装docker方式

msgpack代替json防止丢失类型

妙音 posted @ 2021年6月30日 15:12 in golang , 390 阅读
 

描述

 
开发了一个通信协议, 协议对数据类型比较敏感, 根据不同数据数据类型使用不同格式。golang的数据类型能够满足需求。
 
然而,将协议转为http方式时,如果传入json,会发现数据类型不符合预期
 
例如,本来是应该int结果是float64; 本来应该是[]byte,结果是string
 

环境

 
http服务器: gin
 
开发语言: golang
 

解决方法探寻

 
什么传输协议会带上数据类型?
 
* json类型丢失
 
默认情况下gin的BindJSON,可将传入的参数绑定到具体的结构体,产生类型转换。然后,我的结果需要是动态的,不知道参数的具体内容。
 
* 自定义参数格式,将类型带上。例如 marchinery
 
Arg struct{
    Type string
    Value interface{}
}
 
缺点:太麻烦
 
* 常用协议中选择
 
xml         可以用,不理想
 
yaml        不能解决问题
 
protobuf    不能解决问题
 
messagepack 有详细的数据类型,完美
 
 

msgpack替代json

 
gin示例
 
func SendXX(c *gin.Context){
    rawData, _ := c.GetRawData()
    var param interface{}
    msgpack.Unmarshal(rawData, ¶m)

result := struct{
Name string
}{
Name: "xxx",
}
resultData, _ := msgpack.Marshal(result)
c.Data(200, "application/x-msgpack", resultData)
}
 

备注

 
* gin自带的msgpack版本是1.1.7不好用,不会自动进行类型转换。 最好改用  github.com/vmihailenco/msgpack/v5
 
* 替换gin中的部分接口就可以,不用全部改为msgpack
 
 
 
 
Avatar_small
psd letter 说:
2021年7月01日 18:54

i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. psd letter

Avatar_small
tozo t6 review 说:
2021年7月04日 04:42

The total battery life of 30 hours. Craved right and left initials to make it easier to place the earbuds. The touch system is more appropriate. Technically, yes, you can answer or reject calls tozo t6 review

Avatar_small
seo 说:
2021年7月06日 19:35 It is extremely nice to see the greatest details presented in an easy and understanding manner. 123movies.com official site
Avatar_small
seo 说:
2021年7月06日 19:40

It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content. 123 movies

Avatar_small
seo 说:
2021年7月06日 19:43

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. 123movies

Avatar_small
Digital_Work 说:
2021年7月08日 07:33

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. ver anime

Avatar_small
seo 说:
2021年7月08日 13:31

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! sa gaming

Avatar_small
seo 说:
2021年7月10日 20:16

It is extremely nice to see the greatest details presented in an easy and understanding manner. superslot

Avatar_small
seo 说:
2021年7月10日 20:22

That is very helpful for increasing my knowledge in this field. pg

Avatar_small
seo 说:
2021年7月13日 15:23 A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. joker gaming
Avatar_small
seo 说:
2021年7月13日 18:10

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! waec expo

Avatar_small
seo 说:
2021年7月13日 23:48

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. joker gaming

Avatar_small
Biofit 说:
2021年7月13日 23:56

cool stuff you have got and you keep update all of us.

Avatar_small
инженерные сети 说:
2021年7月15日 18:53

I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. инженерные сети

Avatar_small
48 port gigabit swit 说:
2021年7月17日 03:42

Buying a best 48 port switch requires one to consider its own demands for specific applications. Based on which, one should take based speed, uplink port configuration, PoE capability and other personal requirement into consideration. 48 port gigabit switch

Avatar_small
Loans for bad credit 说:
2021年7月17日 19:50

Efficiently written information. It will be profitable to anybody who utilizes it, counting me. Keep up the good work. For certain I will review out more posts day in and day out. Loans for bad credit

Avatar_small
best zero gravity ma 说:
2021年7月19日 21:36

Another benefit of zero gravity massage chairs is that they improve the flexibility in the body. They do this by combating conditions such as a trigger point, which are known to decrease the body’s flexibility. best zero gravity massage chair

Avatar_small
Digital_Zone 说:
2021年7月20日 03:38 I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... Aluminium ramen
Avatar_small
Digital_Zone 说:
2021年7月20日 03:49

Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. Vakantiehuis Limburg Belgie

Avatar_small
Digital_Zone 说:
2021年7月20日 03:55

A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. Ramen Antwerpen

Avatar_small
Digital_Zone 说:
2021年7月20日 03:59

Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck. Ramen Leuven

Avatar_small
Digital_Zone 说:
2021年7月20日 04:03

You delivered such an impressive piece to read, giving every subject enlightenment for us to gain information. Thanks for sharing such information with us due to which my several concepts have been cleared. Muurisolatie

Avatar_small
Digital_Zone 说:
2021年7月20日 04:07

I have been impressed after read this because of some quality work and informative thoughts. I just want to say thanks for the writer and wish you all the best for coming! Your exuberance is refreshing. Ramen en deuren Antwerpen

Avatar_small
Digital_Zone 说:
2021年7月20日 04:19

It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait reading. I am impressed with your work and skill. Thanks. Ramen en deuren Leuven

Avatar_small
Digital_Zone 说:
2021年7月20日 04:28

This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. Muur isoleren

Avatar_small
Digital_Zone 说:
2021年7月20日 04:31

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Webdesign

Avatar_small
Digital_Zone 说:
2021年7月20日 04:34

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Vakantiehuis Limburg België

Avatar_small
Digital_Zone 说:
2021年7月20日 04:38

This article is an appealing wealth of informative data that is interesting and well-written. I commend your hard work on this and thank you for this information. You’ve got what it takes to get attention. Webdesign

Avatar_small
Digital_Zone 说:
2021年7月20日 04:41

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. Webdesign Limburg

Avatar_small
Digital_Zone 说:
2021年7月20日 04:45

I am continually amazed by the amount of information available on this subject. What you presented was well researched and well worded in order to get your stand on this across to all your readers. Spouwmuurisolatie

Avatar_small
Digital_Zone 说:
2021年7月20日 04:49

Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. Zonnepanelen

Avatar_small
Digital_Zone 说:
2021年7月20日 04:52

Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. Webdesigner Limburg

Avatar_small
Digital_Zone 说:
2021年7月20日 04:57

I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing. Spouwmuur isoleren

Avatar_small
Digital_Zone 说:
2021年7月20日 05:01

Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. Webdesign

Avatar_small
Digital_Zone 说:
2021年7月20日 05:12

Wow! This could be one of the most useful blogs we have ever come across on thesubject. Actually excellent info! I’m also an expert in this topic so I can understand your effort. Zonnepanelen kopen

Avatar_small
Digital_Zone 说:
2021年7月20日 05:16

nice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this! Ramen en deuren

Avatar_small
Digital_Zone 说:
2021年7月20日 05:20

Thank you so much as you have been willing to share information with us. We will forever admire all you have done here because you have made my work as easy as ABC. Zonnepanelen-centrale.be

Avatar_small
Digital_Zone 说:
2021年7月20日 05:24

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this... Ramen en deuren kopen

Avatar_small
Digital_Zone 说:
2021年7月20日 05:28

Remarkable article, it is particularly useful! I quietly began in this, and I'm becoming more acquainted with it better! Delights, keep doing more and extra impressive! Webdesign webshop

Avatar_small
Digital_Zone 说:
2021年7月20日 05:32

Thank you for such a well written article. It’s full of insightful information and entertaining descriptions. Your point of view is the best among many. Ramen deuren

Avatar_small
Digital_Zone 说:
2021年7月20日 05:37

Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. Aluminium ramen en deuren

Avatar_small
Digital_Zone 说:
2021年7月20日 05:41

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! Airco

Avatar_small
best smart water bot 说:
2021年7月22日 04:08

However, they have one thing in common: smart water bottles of all kinds are one of the most important necessities for their specific consumers – anyone who would buy a product like this must be highly interested in acquiring one and will pay a premium. best smart water bottle

Avatar_small
best whole house hum 说:
2021年7月23日 23:09

For some people, a humidifier can also reduce the severity of allergies and asthma, although you should always consult with your doctor first. And make sure to keep up on regular maintenance, as a poorly maintained humidifier can spread mold or bacteria, making allergies and asthma worse. best whole house humidifier

Avatar_small
havit cooling pad re 说:
2021年7月26日 03:41

Building an eye-catching gaming setup is fantastic, especially for your home office. With the ability to customize the colors and light settings of your cooling pad, you’ll have a unique setup. havit cooling pad review

Avatar_small
토토사이트 说:
2021年7月29日 13:57

I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! 토토사이트


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter