apache / dubbo-go-hessian2

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)
Apache License 2.0
209 stars 113 forks source link

go serialization & java unserialization issue #149

Closed SuperWonggeorge closed 4 years ago

SuperWonggeorge commented 4 years ago

What happened:

1.serialization failed when I try to unserialize go class with inheritance 2.lose attribute when I try to unserialize java class with implement

What you expected to happen:

Serialize between java and go for all type

How to reproduce it (as minimally and precisely as possible): Q1. `package main

import ( "fmt" "github.com/apache/dubbo-go-hessian2" "os" )

type BasketballPlayer struct { Skill string }

type Demo struct { BasketballPlayer //不支持 Team string Name string Number int Description []string Teammate map[string]string }

func (d Demo) JavaClassName() string { return "app.pojo.Demo" }

func main() { file, err := os.OpenFile("F:\serialization\serializedResult", os.O_WRONLY|os.O_CREATE, 0644) if err != nil { panic(err) } defer file.Close()

t := Demo{
    Team:        "Lakers",
    Name:        "LebornJames",
    Number:      23,
    Description: []string{"61 points", "3 champions"},
    Teammate:    map[string]string{"3": "wade", "2": "irving"},
}
e := hessian.NewEncoder()
err = e.Encode(t)
if err != nil {
    panic(err)
}

_, err = file.Write(e.Buffer())
if err != nil {
    panic(err)
}
fmt.Println("serialize done!")

} ` I was told "panic: failed to encode field: main.BasketballPlayer, {Skill:}: struct type not Support! main.BasketballPlayer[{}] is not a instance of POJO!" when I try to dubug this

Q2. java code: Association.java `package app.pojo;

import java.io.Serializable;

public interface Association { String associationName = "NBA"; } `

BasketballPlayer.java `package app.pojo;

import java.io.Serializable;

public class BasketballPlayer implements Serializable { public static final long serialVersionUID = 1L; private String skill = "jump shot";

} `

Demo.java `package app.pojo;

import java.io.Serializable; import java.util.List; import java.util.Map;

import lombok.Data;

@Data public class Demo extends BasketballPlayer implements Serializable,Association { private static final long serialVersionUID = 1L; public String skill; private String team; private String name; private int number; private List description; private Map<String, String> teammate;

@Override
public String toString() {
    return "Demo{" +
            "skill=" + skill +
            "' team='" + team +
            "' name='" + name +
            "', number=" + number +
            "', description='" + description +
            "', teammate='" + teammate +
            "', association='" + associationName +"'}";
}

} **App.java** package app;

import java.io.ByteArrayOutputStream;

import com.caucho.hessian.io.Hessian2Output; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

import app.pojo.Demo;

public class App { public static void main(String[] args) { Demo demo = new Demo(); demo.setTeam("Lakers"); demo.setName("kobeBryant"); demo.setNumber(24); List list = new ArrayList(); list.add("81 points"); list.add("5 champions"); demo.setDescription(list); Map map = new HashMap(); map.put("34","O'Neal"); map.put("16", "Gasol"); demo.setTeammate(map); try { byte[] data = App.serialize(demo); FileOutputStream os =new FileOutputStream(new File("F:\serialization\serializedResult")); System.out.println(data.length); os.write(data); os.close(); System.out.println("done"); } catch (Exception e) { e.printStackTrace(); } }

public static byte[] serialize(Object obj){
    ByteArrayOutputStream byteArrayOutputStream = null;
    Hessian2Output hessianOutput = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        // Hessian的序列化输出
        hessianOutput = new Hessian2Output(byteArrayOutputStream);
        hessianOutput.writeObject(obj);
        hessianOutput.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArrayOutputStream.toByteArray();
}

}

`

go code

`package main

import ( "fmt" "github.com/apache/dubbo-go-hessian2" "io/ioutil" "os" )

type Demo struct { Skill string Team string Name string Number int Description []string Teammate map[string] string AssociationName string }

func (d Demo) JavaClassName() string { return "app.pojo.Demo" }

func main() { file, err := os.Open("F:\serialization\serializedResult") if err != nil { panic(err) } defer file.Close()

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

bytes, _ := ioutil.ReadAll(file)
decodedObject, err := hessian.NewDecoder(bytes).Decode()
if err != nil {
    panic(err)
}

d, ok := decodedObject.(*Demo)
if !ok {
    panic("fail")
}
fmt.Printf("%v\n", *d)

}`

result:

API server listening at: 127.0.0.1:50036 {jump shot Lakers kobeBryant 24 [81 points 5 champions] map[16:Gasol 34:O'Neal] }

it lose association='NBA' (didn't happen when I try to unserialize with java)

Anything else we need to know?:

Any solution to fix that problem

micln commented 4 years ago

problem 1: serialization failed when I try to unserialize go class with inheritance

it will be fixed in the next days at #150

micln commented 4 years ago

problem2 : lose attribute when I try to unserialize java class with implement

it's difficult to suport directly: hessian don't encode constant which defined in interface. So we don't relealized the implement behavior

For the reason why it works in java, maybe the value of association='NBA' is set in class-inited

@aliiohs can you give me some suggest

SuperWonggeorge commented 4 years ago

problem 1: serialization failed when I try to unserialize go class with inheritance

it will be fixed in the next days at #150

Good to hear that , do u have any progress now? thx.

SuperWonggeorge commented 4 years ago

problem2 : lose attribute when I try to unserialize java class with implement

it's difficult to suport directly: hessian don't encode constant which defined in interface. So we don't relealized the implement behavior

For the reason why it works in java, maybe the value of association='NBA' is set in class-inited

@aliiohs can you give me some suggest

So do u have any further solution?cuz this feature suppose to be necessary for us ,thank you