Skip to content

Instantly share code, notes, and snippets.

@smallnest
Created September 24, 2015 07:59
Show Gist options
  • Save smallnest/847ecfc428944a92557f to your computer and use it in GitHub Desktop.
Save smallnest/847ecfc428944a92557f to your computer and use it in GitHub Desktop.
package main;
message ColorGroup2 {
required int32 id = 1;
required string name = 2;
repeated string colors = 3;
}
// Code generated by protoc-gen-go.
// source: colorgroup.proto
// DO NOT EDIT!
/*
Package main is a generated protocol buffer package.
It is generated from these files:
colorgroup.proto
It has these top-level messages:
ColorGroup2
*/
package main
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
type ColorGroup2 struct {
Id *int32 `protobuf:"varint,1,req,name=id" json:"id,omitempty"`
Name *string `protobuf:"bytes,2,req,name=name" json:"name,omitempty"`
Colors []string `protobuf:"bytes,3,rep,name=colors" json:"colors,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *ColorGroup2) Reset() { *m = ColorGroup2{} }
func (m *ColorGroup2) String() string { return proto.CompactTextString(m) }
func (*ColorGroup2) ProtoMessage() {}
func (m *ColorGroup2) GetId() int32 {
if m != nil && m.Id != nil {
return *m.Id
}
return 0
}
func (m *ColorGroup2) GetName() string {
if m != nil && m.Name != nil {
return *m.Name
}
return ""
}
func (m *ColorGroup2) GetColors() []string {
if m != nil {
return m.Colors
}
return nil
}
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/pquerna/ffjson/ffjson"
"github.com/golang/protobuf/proto"
)
type ColorGroup struct {
ID int `json:"id" xml:"id,attr""`
Name string `json:"name" xml:"name"`
Colors []string `json:"colors" xml:"colors"`
}
var group = ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
var group2 = ColorGroup2{
Id: proto.Int32(17),
Name: proto.String("Reds"),
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
func MarshalByJson(g *ColorGroup) string {
bytes, error := json.Marshal(g)
if error == nil {
return string(bytes)
} else {
return ""
}
}
func UnmarshalByJson(s string) *ColorGroup {
g := &ColorGroup{}
error := json.Unmarshal([]byte(s), g)
if error == nil {
return g
} else {
return nil
}
}
func MarshalByFfjson(g *ColorGroup) string {
bytes, error := ffjson.Marshal(g)
if error == nil {
return string(bytes)
} else {
return ""
}
}
func UnmarshalByFfjson(s string) *ColorGroup {
g := &ColorGroup{}
error := ffjson.Unmarshal([]byte(s), g)
if error == nil {
return g
} else {
return nil
}
}
func MarshalByXml(g *ColorGroup) string {
bytes, error := xml.Marshal(g)
if error == nil {
return string(bytes)
} else {
return ""
}
}
func UnmarshalByXml(s string) *ColorGroup {
g := &ColorGroup{}
error := xml.Unmarshal([]byte(s), g)
if error == nil {
return g
} else {
return nil
}
}
func MarshalByProtoBuf(g *ColorGroup2) []byte {
bytes, _ := proto.Marshal(g)
return bytes
}
func UnmarshalByProtoBuf(bytes []byte) *ColorGroup2 {
g := &ColorGroup2{}
error := proto.Unmarshal(bytes, g)
if error == nil {
return g
} else {
return nil
}
}
func main() {
s := MarshalByJson(&group)
fmt.Println(s)
fmt.Println(UnmarshalByJson(s))
s = MarshalByFfjson(&group)
fmt.Println(s)
fmt.Println(UnmarshalByFfjson(s))
s = MarshalByXml(&group)
fmt.Println(s)
fmt.Println(UnmarshalByXml(s))
fmt.Println(UnmarshalByProtoBuf(MarshalByProtoBuf(&group2)))
}
package main
import "testing"
func BenchmarkMarshalByJson(b *testing.B) {
for i := 0; i < b.N; i++ {
MarshalByJson(&group)
}
}
func BenchmarkUnmarshalByJson(b *testing.B) {
s := MarshalByJson(&group)
b.ResetTimer()
for i := 0; i < b.N; i++ {
UnmarshalByJson(s)
}
}
func BenchmarkMarshalByFfjson(b *testing.B) {
for i := 0; i < b.N; i++ {
MarshalByFfjson(&group)
}
}
func BenchmarkUnmarshalByFfjson(b *testing.B) {
s := MarshalByFfjson(&group)
b.ResetTimer()
for i := 0; i < b.N; i++ {
UnmarshalByFfjson(s)
}
}
func BenchmarkMarshalByXml(b *testing.B) {
for i := 0; i < b.N; i++ {
MarshalByXml(&group)
}
}
func BenchmarkUnmarshalByXml(b *testing.B) {
s := MarshalByJson(&group)
b.ResetTimer()
for i := 0; i < b.N; i++ {
UnmarshalByXml(s)
}
}
func BenchmarkMarshalByProtoBuf(b *testing.B) {
for i := 0; i < b.N; i++ {
MarshalByProtoBuf(&group2)
}
}
func BenchmarkUnmarshalByProtoBuf(b *testing.B) {
s := MarshalByProtoBuf(&group2)
b.ResetTimer()
for i := 0; i < b.N; i++ {
UnmarshalByProtoBuf(s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment