Skip to content
caucho hessian2 implementation in Go for [apache/dubbo-go](https://github.com/apache/dubbo-go) which is compatible with [dubbo-hessian-lite](https://github.com/apache/dubbo-hessian-lite)
Go Java
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github add issue&pr template May 26, 2019
java_exception fix package name Jul 31, 2019
test_dubbo delete file Jul 18, 2019
test_hessian Merge pull request #102 from aliiohs/feature/FixDateNilBug Jul 24, 2019
.gitignore add go module && travis ci support Mar 29, 2019
.golangci.yml add .golangci.yml Apr 29, 2019
.travis.yml update travis jdk Jul 29, 2019
LICENSE Add: license Feb 23, 2019
Makefile mod: add golangci-lint to Makefile Apr 29, 2019
README.md fix package name Jul 31, 2019
binary.go Mod: move gost package to another import block Jul 27, 2019
binary_test.go add binary, date, null, object, string test Jun 14, 2019
boolean.go add license header May 21, 2019
boolean_test.go update code Jun 14, 2019
codec.go fix Jul 24, 2019
codec_test.go add license header May 21, 2019
const.go
contributing.md Add: contribution notes May 19, 2019
date.go fix ZeroTime bug Jul 20, 2019
date_test.go fix Jul 23, 2019
decode.go Add: supporting multidimensional array and field of interface{} type Jun 30, 2019
decode_test.go fix the exception name Jun 25, 2019
doc.go add license header May 21, 2019
double.go update code Jun 14, 2019
double_test.go update code Jun 14, 2019
encode.go add bool nil, fix #113 Aug 4, 2019
encode_test.go fix bug: if pipe is full and command doesn't run, code will hang Jun 14, 2019
go.mod fix package name Jul 31, 2019
go.sum fix package name Jul 31, 2019
hessian.go Add:support attachments Jul 11, 2019
hessian_test.go Add:add group Jul 2, 2019
int.go Mod: add pkg/errors alias May 23, 2019
int_test.go update code Jun 14, 2019
java_exception.go Mod: change third-part package import block Aug 7, 2019
java_exception_test.go add exception Jul 21, 2019
list.go Fix:fix bug Jul 4, 2019
list_test.go Mod:change Test to TypedListTest Jul 1, 2019
long.go update code Jun 14, 2019
long_test.go update code Jun 14, 2019
map.go add map encode test Jun 14, 2019
map_test.go add map encode test Jun 14, 2019
null.go add license header May 21, 2019
null_test.go add binary, date, null, object, string test Jun 14, 2019
object.go check assertion Aug 6, 2019
object_test.go add bool nil, fix #113 Aug 4, 2019
pojo.go improve error tips Jul 4, 2019
ref.go
ref_test.go
request.go Mod:decode attachments in unpackResponseBody Jul 16, 2019
request_test.go Add:add group Jul 2, 2019
response.go Mod: change third-part package import block Aug 7, 2019
response_test.go Add:support return interface Jul 2, 2019
serialize.go change package version Jul 10, 2019
serialize_test.go change package version Jul 10, 2019
string.go Rem: useless package Jul 27, 2019
string_test.go add binary, date, null, object, string test Jun 14, 2019

README.md

gohessian

Build Status GoCover GoDoc


It's a golang hessian library used by Apache/dubbo-go.

Feature List

Basic Usage Examples

Encode To Bytes

type Circular struct {
	Num      int
	Previous *Circular
	Next     *Circular
}

func (Circular) JavaClassName() string {
	return "com.company.Circular"
}

c := &Circular{}
c.Num = 12345
c.Previous = c
c.Next = c

e := NewEncoder()
err := e.Encode(c)
if err != nil {
    panic(err)
}

bytes := e.Buffer()

Decode From Bytes

decodedObject, err := NewDecoder(bytes).Decode()
if err != nil {
    panic(err)
}
circular, ok := obj.(*Circular)
// ...

Customize Usage Examples

Encoding filed name

Hessian encoder default converts filed names of struct to lower camelcase, but you can customize it using hessian tag.

Example:

type MyUser struct {
	UserFullName      string   `hessian:"user_full_name"`
	FamilyPhoneNumber string   // default convert to => familyPhoneNumber
}

func (MyUser) JavaClassName() string {
	return "com.company.myuser"
}

user := &MyUser{
    UserFullName:      "username",
    FamilyPhoneNumber: "010-12345678",
}

e := hessian.NewEncoder()
err := e.Encode(user)
if err != nil {
    panic(err)
}

The encoded bytes of the struct MyUser is as following:

 00000000  43 12 63 6f 6d 2e 63 6f  6d 70 61 6e 79 2e 6d 79  |C.com.company.my|
 00000010  75 73 65 72 92 0e 75 73  65 72 5f 66 75 6c 6c 5f  |user..user_full_|
 00000020  6e 61 6d 65 11 66 61 6d  69 6c 79 50 68 6f 6e 65  |name.familyPhone|
 00000030  4e 75 6d 62 65 72 60 08  75 73 65 72 6e 61 6d 65  |Number`.username|
 00000040  0c 30 31 30 2d 31 32 33  34 35 36 37 38           |.010-12345678|

Decoding filed name

Hessian decoder finds the correct target field though comparing all filed names of struct one by one until matching.

The following example shows the order of the matching rules:

type MyUser struct {
	MobilePhone      string   `hessian:"mobile-phone"`
}

// You must define the tag of struct for lookup filed form encoded binary bytes, in this case:
// 00000000  43 12 63 6f 6d 2e 63 6f  6d 70 61 6e 79 2e 6d 79  |C.com.company.my|
// 00000010  75 73 65 72 91 0c 6d 6f  62 69 6c 65 2d 70 68 6f  |user..mobile-pho|
// 00000020  6e 65 60 0b 31 37 36 31  32 33 34 31 32 33 34     |ne`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
// ^ will matched


type MyUser struct {
	MobilePhone      string
}

// The following encoded binary bytes will be hit automatically:
//
// 00000000  43 12 63 6f 6d 2e 63 6f  6d 70 61 6e 79 2e 6d 79  |C.com.company.my|
// 00000010  75 73 65 72 91 0b 6d 6f  62 69 6c 65 50 68 6f 6e  |user..mobilePhon|
// 00000020  65 60 0b 31 37 36 31 32  33 34 31 32 33 34        |e`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
//                             ^ will matched
//
// 00000000  43 12 63 6f 6d 2e 63 6f  6d 70 61 6e 79 2e 6d 79  |C.com.company.my|
// 00000010  75 73 65 72 91 0b 4d 6f  62 69 6c 65 50 68 6f 6e  |user..MobilePhon|
// 00000020  65 60 0b 31 37 36 31 32  33 34 31 32 33 34        |e`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
//                                                           ^ will matched
//
// 00000000  43 12 63 6f 6d 2e 63 6f  6d 70 61 6e 79 2e 6d 79  |C.com.company.my|
// 00000010  75 73 65 72 91 0b 6d 6f  62 69 6c 65 70 68 6f 6e  |user..mobilephon|
// 00000020  65 60 0b 31 37 36 31 32  33 34 31 32 33 34        |e`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
//                                                                                    ^ will matched
hessian.SetTagIdentifier

You can use hessian.SetTagIdentifier to customize tag-identifier of hessian, which takes effect to both encoder and decoder.

Example:

hessian.SetTagIdentifier("json")

type MyUser struct {
	UserFullName      string   `json:"user_full_name"`
	FamilyPhoneNumber string   // default convert to => familyPhoneNumber
}

func (MyUser) JavaClassName() string {
	return "com.company.myuser"
}

user := &MyUser{
    UserFullName:      "username",
    FamilyPhoneNumber: "010-12345678",
}

e := hessian.NewEncoder()
err := e.Encode(user)
if err != nil {
    panic(err)
}

The encoded bytes of the struct MyUser is as following:

 00000000  43 12 63 6f 6d 2e 63 6f  6d 70 61 6e 79 2e 6d 79  |C.com.company.my|
 00000010  75 73 65 72 92 0e 75 73  65 72 5f 66 75 6c 6c 5f  |user..user_full_|
 00000020  6e 61 6d 65 11 66 61 6d  69 6c 79 50 68 6f 6e 65  |name.familyPhone|
 00000030  4e 75 6d 62 65 72 60 08  75 73 65 72 6e 61 6d 65  |Number`.username|
 00000040  0c 30 31 30 2d 31 32 33  34 35 36 37 38           |.010-12345678|
You can’t perform that action at this time.