本文整理汇总了Golang中github.com/ugorji/go/codec.NewEncoderBytes函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoderBytes函数的具体用法?Golang NewEncoderBytes怎么用?Golang NewEncoderBytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoderBytes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: MarshalJSON
// MarshalJSON returns a JSON representation of a data dictionary.
// If any of the dictionary's embedded values is a cborrpc.PythonTuple
// or a uuid.UUID, returns a base64-encoded CBOR string; otherwise
// returns a normal JSON object.
func (d DataDict) MarshalJSON() (out []byte, err error) {
var v interface{}
if needsCBOREncoding(reflect.ValueOf(d)) {
// Do CBOR encoding to a byte array
var intermediate []byte
cborHandle := &codec.CborHandle{}
err = cborrpc.SetExts(cborHandle)
if err != nil {
return nil, err
}
encoder := codec.NewEncoderBytes(&intermediate, cborHandle)
err = encoder.Encode(map[string]interface{}(d))
if err != nil {
return nil, err
}
// base64 encode that byte array
s := base64.StdEncoding.EncodeToString(intermediate)
// Then we will JSON encode that string
v = s
} else {
// We will JSON encode the base object
v = map[string]interface{}(d)
}
codecHandle := &codec.JsonHandle{}
encoder := codec.NewEncoderBytes(&out, codecHandle)
err = encoder.Encode(v)
return
}
开发者ID:diffeo,项目名称:go-coordinate,代码行数:34,代码来源:marshal.go
示例2: DeserializeOmgrReq
/*
* Not able to install custom decoder for indefite length objects with the codec.
* So we need to decode the whole response, and then re-encode the newtmgr response
* part.
*/
func DeserializeOmgrReq(data []byte) (*NmgrReq, error) {
req := coap.Message{}
err := req.UnmarshalBinary(data)
if err != nil {
return nil, util.NewNewtError(fmt.Sprintf(
"Oicmgr request invalid %s", err.Error()))
}
if req.Code == coap.GET || req.Code == coap.PUT {
return nil, nil
}
if req.Code != coap.Created && req.Code != coap.Deleted &&
req.Code != coap.Valid && req.Code != coap.Changed &&
req.Code != coap.Content {
return nil, util.NewNewtError(fmt.Sprintf(
"OIC error rsp: %s", req.Code.String()))
}
var rsp OicRsp
err = codec.NewDecoderBytes(req.Payload, new(codec.CborHandle)).Decode(&rsp)
if err != nil {
return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming cbor: %s",
err.Error()))
}
log.Debugf("Deserialized response %+v", rsp)
nmr := &NmgrReq{}
var ndata []byte = make([]byte, 0)
if rsp.Read != nil {
err = codec.NewEncoderBytes(&ndata,
new(codec.CborHandle)).Encode(rsp.Read)
nmr.Op = NMGR_OP_READ_RSP
} else {
err = codec.NewEncoderBytes(&ndata,
new(codec.CborHandle)).Encode(rsp.Write)
nmr.Op = NMGR_OP_WRITE_RSP
}
if err != nil {
return nil, util.NewNewtError(fmt.Sprintf("Internal error: %s",
err.Error()))
}
nmr.Len = uint16(len(ndata))
nmr.Flags = 0
nmr.Group = 0
nmr.Seq = 0
nmr.Id = 0
nmr.Data = ndata
log.Debugf("Deserialized response %+v", nmr)
return nmr, nil
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:60,代码来源:omgr.go
示例3: EncodeRequest
func (c *Config) EncodeRequest() (*NmgrReq, error) {
nmr, err := NewNmgrReq()
if err != nil {
return nil, err
}
nmr.Op = NMGR_OP_WRITE
nmr.Flags = 0
nmr.Group = NMGR_GROUP_ID_CONFIG
nmr.Id = 0
nmr.Len = 0
data := make([]byte, 0)
enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
if c.Value == "" {
type ConfigReadReq struct {
Name string `codec:"name"`
}
readReq := &ConfigReadReq{
Name: c.Name,
}
enc.Encode(readReq)
nmr.Data = data
nmr.Op = NMGR_OP_READ
} else {
enc.Encode(c)
nmr.Data = data
}
nmr.Len = uint16(len(nmr.Data))
return nmr, nil
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:35,代码来源:config.go
示例4: encoderTest
func (s *Suite) encoderTest(c *check.C, obj interface{}, expecteds ...[]byte) {
var actual []byte
encoder := codec.NewEncoderBytes(&actual, s.cbor)
err := encoder.Encode(obj)
c.Assert(err, check.IsNil)
c.Check(actual, DeepEqualAny, expecteds)
}
开发者ID:dmaze,项目名称:goordinate,代码行数:7,代码来源:cborrpc_test.go
示例5: IfcToJSON
// IfcToJSON encodes interface{} to a JSON byte slice
func IfcToJSON(ifcVal interface{}) (jsonOut []byte, err error) {
jsonOut = make([]byte, 0, unsafe.Sizeof(ifcVal))
handle := new(codec.JsonHandle)
encoder := codec.NewEncoderBytes(&(jsonOut), handle)
err = encoder.Encode(ifcVal)
return
}
开发者ID:project8,项目名称:swarm,代码行数:8,代码来源:conversion.go
示例6: EncodeEchoCtrl
func (e *Echo) EncodeEchoCtrl() (*NmgrReq, error) {
type SerialEchoCtl struct {
Echo int `codec:"echo"`
}
integer, err := strconv.Atoi(e.Message)
if err != nil {
return nil, util.NewNewtError(fmt.Sprintf("Invalid echo ctrl setting %s",
err.Error()))
}
echoCtl := &SerialEchoCtl{
Echo: integer,
}
nmr, err := NewNmgrReq()
if err != nil {
return nil, err
}
nmr.Op = NMGR_OP_WRITE
nmr.Flags = 0
nmr.Group = NMGR_GROUP_ID_DEFAULT
nmr.Id = NMGR_ID_CONS_ECHO_CTRL
data := make([]byte, 0)
enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
if err := enc.Encode(echoCtl); err != nil {
return nil, util.NewNewtError(fmt.Sprintf("Failed to encode message %s",
err.Error()))
}
nmr.Len = uint16(len(data))
nmr.Data = data
return nmr, nil
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:35,代码来源:echo.go
示例7: EncodeWriteRequest
func (f *FileDownload) EncodeWriteRequest() (*NmgrReq, error) {
type DownloadReq struct {
Off uint32 `codec:"off"`
Name string `codec:"name"`
}
nmr, err := NewNmgrReq()
if err != nil {
return nil, err
}
nmr.Op = NMGR_OP_READ
nmr.Flags = 0
nmr.Group = NMGR_GROUP_ID_IMAGE
nmr.Id = IMGMGR_NMGR_OP_FILE
downloadReq := &DownloadReq{
Off: f.Offset,
Name: f.Name,
}
data := make([]byte, 0)
enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
enc.Encode(downloadReq)
nmr.Len = uint16(len(data))
nmr.Data = data
return nmr, nil
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:28,代码来源:imagefiledownload.go
示例8: Encode
func (i *ImageStateWriteReq) Encode() (*NmgrReq, error) {
nmr, err := NewNmgrReq()
if err != nil {
return nil, err
}
clone, err := NewImageStateWriteReq()
if err != nil {
return nil, err
}
clone.Confirm = i.Confirm
if len(i.Hash) != 0 {
clone.Hash = i.Hash
if err != nil {
return nil, err
}
}
nmr.Op = NMGR_OP_WRITE
nmr.Flags = 0
nmr.Group = NMGR_GROUP_ID_IMAGE
nmr.Id = IMGMGR_NMGR_OP_STATE
data := make([]byte, 0)
enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
enc.Encode(clone)
nmr.Data = data
nmr.Len = uint16(len(data))
return nmr, nil
}
开发者ID:apache,项目名称:incubator-mynewt-newt,代码行数:33,代码来源:imagestate.go
示例9: writeMessage
func (client *Client) writeMessage(typ MsgType, session [16]byte, seq uint64, meta MetaMap, args []interface{}) (err error) {
// turn seq into 16 bytes
var seqBytes [16]byte
binary.BigEndian.PutUint64(seqBytes[8:], seq)
// message body as msgpack list
msg := []interface{}{&session, &seqBytes, meta}
msg = append(msg, args...)
var msgBytes []byte
enc := codec.NewEncoderBytes(&msgBytes, &mh)
if err = enc.Encode(msg); err != nil {
return
}
// fill in the rest of the header
h := &MsgHeader{0, typ, uint32(len(msgBytes))}
if err = binary.Write(client.wbuf, binary.BigEndian, h); err != nil {
return
}
if _, err = client.wbuf.Write(msgBytes); err != nil {
return
}
return client.wbuf.Flush()
}
开发者ID:nerdpanda,项目名称:rexgo,代码行数:26,代码来源:rexpro0.go
示例10: scriptBody
func scriptBody(sessionId []byte, graphName, script string, bindings map[string]interface{}) (out []byte, err error) {
var (
mh = new(codec.MsgpackHandle)
enc = codec.NewEncoderBytes(&out, mh)
reqId = uuid.NewV4()
isSessionless = bytes.Equal(sessionId, sessionlessUuid[:])
)
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
meta := map[string]interface{}{
"inSession": !isSessionless,
"isolate": isSessionless,
"graphObjName": "g",
}
if isSessionless {
meta["graphName"] = graphName
}
err = enc.Encode([]interface{}{
sessionId,
reqId[:],
meta,
"groovy",
script,
bindings,
})
return
}
开发者ID:philipsoutham,项目名称:gorexpro,代码行数:26,代码来源:rexpro.go
示例11: unmarshalBinary
func (p *KeybasePacket) unmarshalBinary(data []byte) error {
ch := codecHandle()
if err := MsgpackDecodeAll(data, ch, p); err != nil {
return err
}
if err := p.unpackBody(ch); err != nil {
return err
}
// Test for nonstandard msgpack data (which could be maliciously crafted)
// by re-encoding and making sure we get the same thing.
// https://github.com/keybase/client/issues/423
//
// Ideally this should be done at a lower level, like MsgpackDecodeAll, but
// our msgpack library doesn't sort maps the way we expect. See
// https://github.com/ugorji/go/issues/103
var reencoded []byte
if err := codec.NewEncoderBytes(&reencoded, ch).Encode(p); err != nil {
return err
}
if reencoded, err := p.Encode(); err != nil {
return err
} else if !bytes.Equal(reencoded, data) {
return FishyMsgpackError{data, reencoded}
}
return p.checkHash()
}
开发者ID:mark-adams,项目名称:client,代码行数:30,代码来源:kbpackets.go
示例12: New
// Creates a new token of an user "id" which expires on "expires"
func New(id string, expires time.Time) (string, error) {
// Expiration time in unix
unix := expires.Unix()
// Create a new instance of a Token
token := Token{
id,
unix,
signToken(id, unix),
}
// Initialize an empty []byte that will receive msgpack data
var message []byte
// Create a new msgpack encoder
encoder := codec.NewEncoderBytes(&message, msgpack)
// Encode it
err := encoder.Encode(token)
if err != nil {
return "", err
}
// Base64 it in a form that is friendly to HTTP headers and return it
return base64.URLEncoding.EncodeToString(message), nil
}
开发者ID:pzduniak,项目名称:tokener,代码行数:26,代码来源:token.go
示例13: unpackBody
func (p *KeybasePacket) unpackBody(ch *codec.MsgpackHandle) error {
var body interface{}
switch p.Tag {
case TagP3skb:
// XXX this function should get a G passed into it, but to do that requires
// a lot of changes upstream.
body = NewSKB(G)
case TagSignature:
body = &NaclSigInfo{}
case TagEncryption:
body = &NaclEncryptionInfo{}
default:
return fmt.Errorf("Unknown packet tag: %d", p.Tag)
}
var encoded []byte
if err := codec.NewEncoderBytes(&encoded, ch).Encode(p.Body); err != nil {
return err
}
if err := MsgpackDecodeAll(encoded, ch, body); err != nil {
return err
}
p.Body = body
return nil
}
开发者ID:mark-adams,项目名称:client,代码行数:26,代码来源:kbpackets.go
示例14: TestHTTPDecoder
func TestHTTPDecoder(t *testing.T) {
var out []byte
if err := codec.NewEncoderBytes(&out, h).Encode(req); err != nil {
t.Fatalf("unable to pack test data: %v", err)
}
r, err := UnpackProxyRequest(out)
if err != nil {
t.Fatalf("unable to unpack request %v", err)
}
defer r.Body.Close()
if b, _ := ioutil.ReadAll(r.Body); !bytes.Equal(b, body) {
t.Fatalf("bad bytes: %s %s", b, body)
}
if r.Method != method {
t.Fatalf("bad method: %s %s", r.Method, method)
}
if r.Header.Get("X-Cocaine-Service") != "Test" {
t.Fatalf("bad header", r.Header.Get("X-Cocaine-Service"))
}
}
开发者ID:ascheglov,项目名称:cocaine-framework-go,代码行数:25,代码来源:http_test.go
示例15: packRequest
func packRequest(req *http.Request) ([]byte, error) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
// method uri 1.1 headers body
headers := make([][2]string, 0, len(req.Header))
for header, values := range req.Header {
for _, val := range values {
headers = append(headers, [2]string{header, val})
}
}
var task []byte
codec.NewEncoderBytes(&task, hAsocket).Encode([]interface{}{
req.Method,
req.URL.RequestURI(),
fmt.Sprintf("%d.%d", req.ProtoMajor, req.ProtoMinor),
headers,
body,
})
return task, nil
}
开发者ID:ascheglov,项目名称:cocaine-framework-go,代码行数:25,代码来源:proxy.go
示例16: WriteMessage
func WriteMessage(conn net.Conn, timeout time.Duration, msg *Message) error {
var handle = &codec.MsgpackHandle{}
var buf []byte
var encoder = codec.NewEncoderBytes(&buf, handle)
err := encoder.Encode(msg)
if err != nil {
return err
}
length := len(buf)
if length > DefaultMessageLengthCap {
return errors.New("message is too long")
}
var lengthBuf [2]byte
binary.BigEndian.PutUint16(lengthBuf[:], uint16(length))
conn.SetWriteDeadline(time.Now().Add(timeout))
_, err = conn.Write(lengthBuf[:])
if err != nil {
return err
}
_, err = io.Copy(conn, bytes.NewBuffer(buf))
return err
}
开发者ID:postman0,项目名称:xep,代码行数:26,代码来源:executor.go
示例17: main
func main() {
jh := new(codec.JsonHandle)
u := &user.User{
Name: "Inigo Montoya",
Email: "[email protected]",
}
var out []byte
err := codec.NewEncoderBytes(&out, jh).Encode(&u)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(out))
var u2 user.User
err = codec.NewDecoderBytes(out, jh).Decode(&u2)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(u2)
}
开发者ID:ymonk,项目名称:go-in-practice,代码行数:25,代码来源:json.go
示例18: encoderTest
func encoderTest(t *testing.T, obj interface{}, expecteds ...[]byte) {
var actual []byte
encoder := codec.NewEncoderBytes(&actual, cbor)
err := encoder.Encode(obj)
if assert.NoError(t, err) {
assert.Contains(t, expecteds, actual)
}
}
开发者ID:diffeo,项目名称:go-coordinate,代码行数:8,代码来源:cborrpc_test.go
示例19: BenchmarkHTTPDecoder
func BenchmarkHTTPDecoder(b *testing.B) {
var out []byte
codec.NewEncoderBytes(&out, h).Encode(req)
for n := 0; n < b.N; n++ {
UnpackProxyRequest(out)
}
}
开发者ID:ascheglov,项目名称:cocaine-framework-go,代码行数:8,代码来源:http_test.go
示例20: encodeEvent
func encodeEvent(event *[]interface{}) (*[]byte, error) {
var result []byte
var err = codec.NewEncoderBytes(&result, &msgpackHandle).Encode(event)
if err != nil {
Log.Printf("Failed to encode event %v\n", event)
}
return &result, err
}
开发者ID:zvin,项目名称:vandal,代码行数:8,代码来源:user.go
注:本文中的github.com/ugorji/go/codec.NewEncoderBytes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论