-
Notifications
You must be signed in to change notification settings - Fork 168
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
Is it possible add default Marshal option to Message.MarshalJSON #378
Comments
@SuperGod, I don't intend to add much more (maybe nothing more?) to this package or the The only thing I had planned to merge regarding these packages, in fact, is better/easier interop and compatibility with those packages, to make it easier for users to migrate their code to using those other packages. (In fact, I've had a working branch in #354 that I hope to merge soon but want to do a little more performance testing first.) I would recommend switching to using type dynamicMessageWithJSON dynamic.Message
func (m *dynamicMessageWithJSON) MarshalJSON() ([]byte, error) {
dm := (*dynamic.Message)(m)
return dm.MarshalJSONPB(&jsonpb.Marshaler{OrigName: true})
} |
"type dynamicMessageWithJSON dynamic.Message" maybe useful in some case,but not for me. My case is like below:
"google.golang.org/protobuf/types/dynamicpb" maybe a solution, I have to recompile all my proto file when I use "google.golang.org/protobuf/", If there is no other way, I can only do so |
I don't see why the msg := dynamic.NewMessage(typ)
// ...
data := make(map[string]interface{})
val := msg.GetField(field)
if dm, ok := val.(*dynamic.Message); ok {
val = (*dynamicMessageWithJSON)(dm)
}
data["a"] = val
buf,err := json.Marshal(data)
// ...
This is not true. The new runtime has an extensive ABI-compatibility layer (which is basically all of "github.com/golang/protobuf" v1.4+). So if you simply upgrade that dependency from 1.3, all of your existing There are some subtle incompatibilities, but the reason the version went from 1.3 to 1.4 (same major version) is because it is intended to be compatible. |
I will try your solution. There is a blog describe "github.com/golang/protobuf" v1.4+ pb.go problem,but in Chinese The root cause is "github.com/golang/protobuf" v1.4+ Deprecated "InternalMessageInfo",so if you use pb.go generate with v1.3.*, and use v1.4+ package , your code will panic |
While I don't read Chinese and didn't try to run the whole page through a translator, one thing I did see stood out: it was referring to v1.4.0 (the very first release of the new runtime). However I believe the issue to which you are referring was fixed in v1.4.2: https://github.com/golang/protobuf/releases/tag/v1.4.2 So you might just give it a shot. |
Ok,I will try it, thanks |
dynamic.Message has two method to marshal json:
json.Marshal function use MarshalJSON to marshal data to json, so user can't change the default json option when use go's encoding/json package,see below code:
So is it possible add a default Marshal option? like this:
The text was updated successfully, but these errors were encountered: