Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎样友好的兼容 java 中的集合类 #343

Open
alpha-baby opened this issue Dec 19, 2022 · 4 comments
Open

怎样友好的兼容 java 中的集合类 #343

alpha-baby opened this issue Dec 19, 2022 · 4 comments

Comments

@alpha-baby
Copy link

What would you like to be added:

Why is this needed:

在 java 中把这个结构序列化出来:

        Object[] as = new Object[1];
        List l = new LinkedList();
        l.add("123");
        l.add("456");
        as[0] = l;
java:
00000000  71 07 5b 6f 62 6a 65 63  74 72 14 6a 61 76 61 2e  |q.[objectr.java.|
00000010  75 74 69 6c 2e 4c 69 6e  6b 65 64 4c 69 73 74 03  |util.LinkedList.|
00000020  31 32 33 03 34 35 36                              |123.456|

golang 这个接口序列化出来是:

encoder.Encode([]interface{}{[]string{"123", "123"}})
golang:
00000000  58 91 56 07 5b 73 74 72  69 6e 67 92 03 31 32 33  |X.V.[string..123|
00000010  03 31 32 33                                       |.123|

java 把 Object[] 序列化,为了TypedList, 但是 golang 把 interface{} 切片序列化为了 UntypedList 这个又是为什么呢?

@tiltwind
Copy link
Contributor

java 把 Object[] 序列化,为了TypedList, 但是 golang 把 interface{} 切片序列化为了 UntypedList 这个又是为什么呢?

@alpha-baby 你的意思是 java中 如果 Object[] 中只有一个元素,就会序列化为 TypedList, 如果有2个不同的元素就会序列化为 UntypedList 吗? 如果真是这样,就变得不可预期,是不是一种不可取的方式?

@alpha-baby
Copy link
Author

java 把 Object[] 序列化,为了TypedList, 但是 golang 把 interface{} 切片序列化为了 UntypedList 这个又是为什么呢?

@alpha-baby 你的意思是 java中 如果 Object[] 中只有一个元素,就会序列化为 TypedList, 如果有2个不同的元素就会序列化为 UntypedList 吗? 如果真是这样,就变得不可预期,是不是一种不可取的方式?

不管有几个元素都会序列化成 TypedList, 这里和元素个数没得关系,

@tiltwind
Copy link
Contributor

@alpha-baby 跨语言访问最好还是不要使用特定语言中才有的类定义,如果要兼容,需要在go中特殊定义,参考 https://github.com/apache/dubbo-go-hessian2/blob/master/java_util/README_CN.md

@alpha-baby
Copy link
Author

https://github.com/apache/dubbo-go-hessian2/blob/master/java_util/README_CN.md

确实是这样,我这里的情况是,对方定义的那个 java 接口已经很多年了,方法入参中已经定义成了 List

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants