本文整理匯總了Golang中github.com/gengo/grpc-gateway/runtime.JSONPb.Unmarshal方法的典型用法代碼示例。如果您正苦於以下問題:Golang JSONPb.Unmarshal方法的具體用法?Golang JSONPb.Unmarshal怎麽用?Golang JSONPb.Unmarshal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gengo/grpc-gateway/runtime.JSONPb
的用法示例。
在下文中一共展示了JSONPb.Unmarshal方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestJSONPbUnmarshalFields
func TestJSONPbUnmarshalFields(t *testing.T) {
var m runtime.JSONPb
for _, fixt := range fieldFixtures {
if fixt.skipUnmarshal {
continue
}
dest := reflect.New(reflect.TypeOf(fixt.data))
if err := m.Unmarshal([]byte(fixt.json), dest.Interface()); err != nil {
t.Errorf("m.Unmarshal(%q, %T) failed with %v; want success", fixt.json, dest.Interface(), err)
}
if got, want := dest.Elem().Interface(), fixt.data; !reflect.DeepEqual(got, want) {
t.Errorf("dest = %#v; want %#v; input = %v", got, want, fixt.json)
}
}
}
示例2: TestJSONPbUnmarshal
func TestJSONPbUnmarshal(t *testing.T) {
var (
m runtime.JSONPb
got examplepb.ABitOfEverything
)
for _, data := range []string{
`{
"uuid": "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
"nested": [
{"name": "foo", "amount": 12345}
],
"uint64Value": 18446744073709551615,
"enumValue": "ONE",
"oneofString": "bar",
"mapValue": {
"a": 1,
"b": 0
}
}`,
`{
"uuid": "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
"nested": [
{"name": "foo", "amount": 12345}
],
"uint64Value": "18446744073709551615",
"enumValue": "ONE",
"oneofString": "bar",
"mapValue": {
"a": 1,
"b": 0
}
}`,
`{
"uuid": "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
"nested": [
{"name": "foo", "amount": 12345}
],
"uint64Value": 18446744073709551615,
"enumValue": 1,
"oneofString": "bar",
"mapValue": {
"a": 1,
"b": 0
}
}`,
} {
if err := m.Unmarshal([]byte(data), &got); err != nil {
t.Errorf("m.Unmarshal(%q, &got) failed with %v; want success", data, err)
}
want := examplepb.ABitOfEverything{
Uuid: "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
Nested: []*examplepb.ABitOfEverything_Nested{
{
Name: "foo",
Amount: 12345,
},
},
Uint64Value: 0xFFFFFFFFFFFFFFFF,
EnumValue: examplepb.NumericEnum_ONE,
OneofValue: &examplepb.ABitOfEverything_OneofString{
OneofString: "bar",
},
MapValue: map[string]examplepb.NumericEnum{
"a": examplepb.NumericEnum_ONE,
"b": examplepb.NumericEnum_ZERO,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got = %v; want = %v", &got, &want)
}
}
}