Closed FZambia closed 7 years ago
Do you have an UnmarshalJSON method for your customtype Raw
?
I think later this code was added
if _, ok := outVal.(interface {
UnmarshalJSON([]byte) error
}); ok {
if err := json.Unmarshal(inputValue, outVal); err != nil {
return err
}
target.Set(outRef.Elem())
return nil
}
To check for a customtype with an underlying []byte type
Do you have an UnmarshalJSON method for your customtype Raw
Here is this Raw type - so yes, it has UnmarshalJSON method and its called.
Btw looks like MarshalJSON never called as default info encoded as base64 after MarshalToString
This line is called when marshalling ClientInfo
to string but it does not call my MarshalJSON
method. I am not too familiar with reflection, so can't figure out why this happens:)
Have you tried removing the pointer from Raw for the MarshalJSON method? As the test Uuid type does here https://github.com/gogo/protobuf/blob/master/test/uuid.go#L86
So basically you will have
// MarshalJSON returns *r as the JSON encoding of r.
func (r Raw) MarshalJSON() ([]byte, error) {
return r, nil
}
Yep, I tried this before - and tests were failing.. But what I tried now is removing pointer as you suggested and also changed marshaling to:
// Default handling defers to the encoding/json library.
var b []byte
var err error
if m, ok := v.Interface().(json.Marshaler); ok {
b, err = m.MarshalJSON()
} else {
b, err = json.Marshal(v.Interface())
}
if err != nil {
return err
}
/*
b, err := json.Marshal(v.Interface())
if err != nil {
return err
}
*/
And now tests pass...
update: sorry, looks like I did a mistake, removing pointer is enough, I will check again)) Give me some time
Ok great. So is removing the pointer an option for you, i.o.w. can we close the issue?
Yes, sorry for disturbing... Removing pointer is enough, looks like when I tried to do this before I also broke something in another place attempting to find solution.
Actually when I did this type I saw that uuid example had no pointer in MarshalJSON
but RawMessage
in std lib has. So I decided to write it with pointer.
Thanks!
I have to say that this solution is not ideal and causes confusion, but it is what we have. Have you tried using casttype instead of customtype?
It might be possible for you to delete quite a bit of code.
No, even have not known about it before this moment, thanks - will try to play with it.
I think it works better than customtype.
Hi, just tried to use casttype
for one of the fields.
string Channel = 3 [(gogoproto.casttype) = "Channel", (gogoproto.jsontag) = "channel"];
where Channel
is:
type Channel string
There is a generated function randStringMessage
- it returns string
so code does not compile because of wrong type used in NewPopulatedX
func:
func randStringMessage(r randyMessage) string {
v3 := r.Intn(100)
tmps := make([]rune, v3)
for i := 0; i < v3; i++ {
tmps[i] = randUTF8RuneMessage(r)
}
return string(tmps)
}
func NewPopulatedMessage(r randyMessage, easy bool) *Message {
this := &Message{}
...
this.Channel = randStringMessage(r)
...
return this
}
Looks like some addition type casting needed here
this.Channel = Channel(randStringMessage(r))
This should fix the issue https://github.com/gogo/protobuf/commit/a9cd0c35b97daf74d0ebf3514c5254814b2703b4
Hi, I just came across an issue with custom type. I saw current custom type issues but I have not understood is my case different or not.
I have custom
Raw
type that behaves the same way asjson.RawMessage
, so it's a new type derived from[]byte
.My struct that uses this type looks like:
I just updated
gogoprotobuf
library (previously I used proto2, now moving to proto3) and my autogenerated tests start failing.Raw
message encoded as base64 sojsondata
looks like:But after unmarshalling from string equality test fails because
DefaultInfo
not unmarshalled from that base64 string to[]byte
.As far as I understand my tests were passing previously because
jsonpb
did not even try to encode bytes before cd72cb90120d2d7157d866d5b27cfc122efb4d40, right?Is this issue related to #201 ?