Build proto message object in go

24 views
Skip to first unread message

Anil Dasari

unread,
Jul 15, 2019, 10:44:40 PM7/15/19
to Protocol Buffers
HI,

i am playing with proto. and created following message - 

message Generic {
Value val =1;
}

// The JSON representation for `Value` is JSON value.
message Value {
oneof value {
string StringVal = 1;
int64 IntVal = 2;
float Floatval = 3;
}
}

I would like to know how this will be represented in json. will be it like 

1. {
  "val": 1
 }
or
 {
  "val" : "100"
 }

2. {"val":{"IntVal":"10"}}

i am using following code to test my protobuff json representation and it is generating - {"val":{"IntVal":"10"}}

g := v1.Generic{
        Val: &v1.Value{
            Value: &v1.Value_IntVal{IntVal: 10},
        },
    }

 jsonReq = toJSON(&g)
 fmt.Println("json request is: ", jsonReq) // json request is: {"val":{"IntVal":"10"}}

func toJSON(pb proto.Message) string {
    marshaler := jsonpb.Marshaler{}
    out, err := marshaler.MarshalToString(pb)
    if err != nil {
        fmt.Println("failed to marshal:", err)
        return ""
    }
    return out
}




i am looking for {"val" : 1} or {"val": "10"}. Can you help me in using correct way to initialise v1.Generic ? thanks.


Thanks. 



Reply all
Reply to author
Forward
0 new messages