本文整理匯總了Golang中github.com/grpc-ecosystem/grpc-gateway/runtime.JSONBuiltin.NewEncoder方法的典型用法代碼示例。如果您正苦於以下問題:Golang JSONBuiltin.NewEncoder方法的具體用法?Golang JSONBuiltin.NewEncoder怎麽用?Golang JSONBuiltin.NewEncoder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/grpc-ecosystem/grpc-gateway/runtime.JSONBuiltin
的用法示例。
在下文中一共展示了JSONBuiltin.NewEncoder方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestJSONBuiltinEncoderFields
func TestJSONBuiltinEncoderFields(t *testing.T) {
var m runtime.JSONBuiltin
for _, fixt := range builtinFieldFixtures {
var buf bytes.Buffer
enc := m.NewEncoder(&buf)
if err := enc.Encode(fixt.data); err != nil {
t.Errorf("enc.Encode(%#v) failed with %v; want success", fixt.data, err)
}
if got, want := buf.String(), fixt.json+"\n"; got != want {
t.Errorf("got = %q; want %q; data = %#v", got, want, fixt.data)
}
}
}
示例2: TestJSONBuiltinEncoder
func TestJSONBuiltinEncoder(t *testing.T) {
var m runtime.JSONBuiltin
msg := examplepb.SimpleMessage{
Id: "foo",
}
var buf bytes.Buffer
enc := m.NewEncoder(&buf)
if err := enc.Encode(&msg); err != nil {
t.Errorf("enc.Encode(%v) failed with %v; want success", &msg, err)
}
var got examplepb.SimpleMessage
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Errorf("json.Unmarshal(%q, &got) failed with %v; want success", buf.String(), err)
}
if want := msg; !reflect.DeepEqual(got, want) {
t.Errorf("got = %v; want %v", &got, &want)
}
}