Skip to content

Instantly share code, notes, and snippets.

@algrebe
Created December 30, 2015 07:18
Show Gist options
  • Save algrebe/a021d9ce27cb8153d82f to your computer and use it in GitHub Desktop.
Save algrebe/a021d9ce27cb8153d82f to your computer and use it in GitHub Desktop.
a comparison of vmihailenco and ugorji msgpack encoding of float
package main
import (
"fmt"
"github.com/ugorji/go/codec"
"gopkg.in/vmihailenco/msgpack.v2"
)
type TestStruct struct {
Num float32
}
func testYourStruct() {
data := TestStruct{Num: 0.0}
fmt.Printf("starting with %+v\n", data)
b, err := msgpack.Marshal(data)
if err != nil {
fmt.Printf("failed to encode...\n")
panic(err)
}
fmt.Printf("encoded into %v\n", b)
orig := TestStruct{}
if err := msgpack.Unmarshal(b, &orig); err != nil {
fmt.Printf("failed to decode...\n")
panic(err)
}
fmt.Printf("decoded back into %+v\n", orig)
}
func testYourFloat() {
var data float32 = 0.0
fmt.Printf("starting with %v\n", data)
b, err := msgpack.Marshal(data)
if err != nil {
fmt.Printf("failed to encode ...\n")
panic(err)
}
fmt.Printf("encoded into %v\n", b)
var orig float32
if err := msgpack.Unmarshal(b, &orig); err != nil {
fmt.Printf("failed to decode ...\n")
panic(err)
}
fmt.Printf("decoded back into %v\n", orig)
}
func testUgorjiStruct() {
var mh codec.MsgpackHandle
data := TestStruct{Num: 0.0}
fmt.Printf("starting with %+v\n", data)
var b []byte
enc := codec.NewEncoderBytes(&b, &mh)
err := enc.Encode(data)
if err != nil {
fmt.Printf("failed to encode...\n")
panic(err)
}
fmt.Printf("encoded into %v\n", b)
orig := TestStruct{}
dec := codec.NewDecoderBytes(b, &mh)
if err := dec.Decode(&orig); err != nil {
fmt.Printf("failed to decode...\n")
panic(err)
}
fmt.Printf("decoded back into %+v\n", orig)
}
func testUgorjiFloat() {
var mh codec.MsgpackHandle
var data float32 = 0.0
fmt.Printf("starting with %v\n", data)
var b []byte
enc := codec.NewEncoderBytes(&b, &mh)
err := enc.Encode(data)
if err != nil {
fmt.Printf("failed to encode ...\n")
panic(err)
}
fmt.Printf("encoded into %v\n", b)
var orig float32
dec := codec.NewDecoderBytes(b, &mh)
if err := dec.Decode(&orig); err != nil {
fmt.Printf("failed to decode ...\n")
panic(err)
}
fmt.Printf("decoded back into %v\n", orig)
}
func testHybridStruct() {
var mh codec.MsgpackHandle
var b []byte
data := TestStruct{Num: 0.0}
fmt.Printf("starting with %+v\n", data)
enc := codec.NewEncoderBytes(&b, &mh)
if err := enc.Encode(data); err != nil {
fmt.Printf("failed to ugorji encode\n")
panic(err)
}
fmt.Printf("ugorji encoded into %v\n", b)
orig := TestStruct{}
if err := msgpack.Unmarshal(b, &orig); err != nil {
fmt.Printf("failed to decode ugorji encoding...%s\n", err)
panic(err)
}
fmt.Printf("decoded from ugorji into yours successfully %+v\n", orig)
}
func main() {
str := "\n---------------------------------\n"
fmt.Printf("%syour float%s", str, str)
testYourFloat()
/* OUTPUT
starting with 0
encoded into [202 0 0 0 0]
decoded back into 0
*/
fmt.Printf("%sugorji float%s", str, str)
testUgorjiFloat()
/* OUTPUT
starting with 0
encoded into [202 0 0 0 0]
decoded back into 0
*/
// The Floats look fine....
// now trying the structs
fmt.Printf("%syour struct%s", str, str)
testYourStruct()
/* OUTPUT
starting with {Num:0}
encoded into [129 163 78 117 109 203 0 0 0 0 0 0 0 0]
decoded back into {Num:0}
*/
fmt.Printf("%sugorji struct%s", str, str)
testUgorjiStruct()
/* OUTPUT
starting with {Num:0}
encoded into [129 163 78 117 109 202 0 0 0 0]
decoded back into {Num:0}
*/
// there is a difference between your struct and ugorji struct ...
// heres what happens when I use his encoding and your decoding ...
fmt.Printf("%sUgorji encoding and your decoding of struct%s", str, str)
testHybridStruct()
/* OUTPUT
starting with {Num:0}
ugorji encoded into [129 163 78 117 109 202 0 0 0 0]
failed to decode ugorji encoding...msgpack: invalid code ca decoding float64
panic: msgpack: invalid code ca decoding float64
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment