gohessian icon indicating copy to clipboard operation
gohessian copied to clipboard

Cannot unmarshal bytes to object

Open tinycedar opened this issue 9 years ago • 4 comments

I modify the hessian_test.go as follows:

type Person struct {
    firstName string
    lastName  string
}

func TestSerializer(t *testing.T) {
    p := Person{"John", "Doe"}
    gh := NewGoHessian(nil, nil)
    bt, _ := gh.ToBytes(p)
    p_new, _ := gh.ToObject(bt)
    fmt.Println("bt", string(bt))
    fmt.Println("p= ", p)
    fmt.Println("p_new= ", p_new)
    // reflect.DeepEqual(p, p_new)
}

But the ouput of p_new is nil, could you please help work on it ?

PS D:\go\src\github.com\viant\gohessian> go test -v .\hessian_test.go                                                                         
=== RUN   TestSerializer                                                                                                                      
struct {John Doe}                                                                                                                             
clsName Person                                                                                                                                
bt C═Person�    firstNamlastName`╝John╚Doe                                                                                                    
p=  {John Doe}                                                                                                                                
p_new=  <nil>                                                                                                                                 
--- PASS: TestSerializer (0.00s)                                                                                                              
PASS                                                                                                                                          
ok      command-line-arguments  0.045s

tinycedar avatar Oct 25 '16 13:10 tinycedar

Daniel,

Some back ground information, go hessian was design to interoperate between java and go. There are some java and go language difference. Like field attribute upper and lower case. In go it is lower case, it is not accessible outside of package

This will work

type Person struct {

*F*irstName string

*L*astName  string

}

func TestSerializer(t *testing.T) {

p := Person{"John", "Doe"}

*typMap := map[string]reflect.Type{"Person": reflect.TypeOf(p)}*

gh := NewGoHessian(typMap, nil)

bt, _ := gh.ToBytes(p)

p_new, _ := gh.ToObject(bt)

fmt.Println("bt", string(bt))

fmt.Println("p= ", p)

fmt.Println("p_new= ", p_new)

// reflect.DeepEqual(p, p_new)

}

On Tue, Oct 25, 2016 at 6:30 AM, Daniel Lin [email protected] wrote:

I modify the hessian_test.go as follows:

type Person struct { firstName string lastName string }

func TestSerializer(t *testing.T) { p := Person{"John", "Doe"} gh := NewGoHessian(nil, nil) bt, _ := gh.ToBytes(p) p_new, _ := gh.ToObject(bt) fmt.Println("bt", string(bt)) fmt.Println("p= ", p) fmt.Println("p_new= ", p_new) // reflect.DeepEqual(p, p_new) }

But the ouput of p_new is nil, could you please help work on it ?

PS D:\go\src\github.com\viant\gohessian> go test -v .\hessian_test.go === RUN TestSerializer struct {John Doe} clsName Person bt C═Person� firstNamlastName`╝John╚Doe p= {John Doe} p_new= --- PASS: TestSerializer (0.00s) PASS ok command-line-arguments 0.045s

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/viant/gohessian/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGDytna2AgwbMBdWbij7rb1d2ZU9fh1ks5q3gRfgaJpZM4Kf_Cw .

mickeyhs avatar Oct 27 '16 17:10 mickeyhs

just to add one more point for the reason of typMap: due to java and go package name space difference. Upper case for field name is must for go reflection, other wise you will get can not write for reflection.

On Thu, Oct 27, 2016 at 10:27 AM, Mickey Hsieh [email protected] wrote:

Daniel,

Some back ground information, go hessian was design to interoperate between java and go. There are some java and go language difference. Like field attribute upper and lower case. In go it is lower case, it is not accessible outside of package

This will work

type Person struct {

*F*irstName string

*L*astName  string

}

func TestSerializer(t *testing.T) {

p := Person{"John", "Doe"}

*typMap := map[string]reflect.Type{"Person": reflect.TypeOf(p)}*

gh := NewGoHessian(typMap, nil)

bt, _ := gh.ToBytes(p)

p_new, _ := gh.ToObject(bt)

fmt.Println("bt", string(bt))

fmt.Println("p= ", p)

fmt.Println("p_new= ", p_new)

// reflect.DeepEqual(p, p_new)

}

On Tue, Oct 25, 2016 at 6:30 AM, Daniel Lin [email protected] wrote:

I modify the hessian_test.go as follows:

type Person struct { firstName string lastName string }

func TestSerializer(t *testing.T) { p := Person{"John", "Doe"} gh := NewGoHessian(nil, nil) bt, _ := gh.ToBytes(p) p_new, _ := gh.ToObject(bt) fmt.Println("bt", string(bt)) fmt.Println("p= ", p) fmt.Println("p_new= ", p_new) // reflect.DeepEqual(p, p_new) }

But the ouput of p_new is nil, could you please help work on it ?

PS D:\go\src\github.com\viant\gohessian> go test -v .\hessian_test.go === RUN TestSerializer struct {John Doe} clsName Person bt C═Person� firstNamlastName`╝John╚Doe p= {John Doe} p_new= --- PASS: TestSerializer (0.00s) PASS ok command-line-arguments 0.045s

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/viant/gohessian/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGDytna2AgwbMBdWbij7rb1d2ZU9fh1ks5q3gRfgaJpZM4Kf_Cw .

mickeyhs avatar Oct 27 '16 17:10 mickeyhs

@mickeyhs It doesn't work according to your advice

tinycedar avatar Oct 28 '16 01:10 tinycedar

@tinycedar First: pay attention to FirstName/ LastName
type Person struct { FirstName string LastName string }

Second: new NewGoHessian with a typMap typMap:=map[string]reflect.Type{ "Person":reflect.TypeOf(Person{}), } gh := NewGoHessian(typMap, nil)

Other tips: And there's another way to register your classname when encode/decode

func (e *encoder) RegisterNameType(key string, javaClsName string) { e.nameMap[key] = javaClsName }

func (d *decoder) RegisterType(key string, value reflect.Type) { d.typMap[key] = value }

zhchj126 avatar Oct 29 '16 01:10 zhchj126