How to deserialize a java map

huangapple go评论75阅读模式
英文:

How to deserialize a java map

问题

我正在尝试在Go语言中将字节反序列化为对象,这些字节是以以下方式在Java中序列化为字节的:

// myMap是Java中TreeMap<String, Object>的实例

ByteArrayOutputStream a = new ByteArrayOutputStream();
GZIPOutputStream b = new GZIPOutputStream(a);
ObjectOutputStream c = new ObjectOutputStream(b);
c.writeObject(myMap);
c.close();
byte[] bytes = a.toByteArray()

以下是我尝试的步骤:

步骤1 - 使用以下代码解压缩字节(存储在变量result中):

// att是接收到的字节数组

buf := bytes.NewBuffer(att)
reader, _ := gzip.NewReader(buf)
defer reader.Close()
result, _ := ioutil.ReadAll(reader)

步骤2 - 从解压缩的字节中读取对象 - 但是失败了:

var decodedMap map[string]interface{}
d := gob.NewDecoder(bytes.NewBuffer(result))
err = d.Decode(&decodedMap)
if err != nil {
panic(err)
}

错误信息为"gob: encoded unsigned integer out of range"

但是,当我将(字节数组)result转换为字符串时,在Go语言中我可以看到编码的TreeMap详细信息和内容:

map: �� sr java.util.TreeMap��>-%j� Lt NAMEt JOHNt AGEt 32t LOCODEsr java.lang.Long;���̏#� J valuexr java.lang.Number������ xp y

有人可以帮我解决这个问题吗?

英文:

I am trying to de-serialize bytes into an object in Go, which was serialized into bytes in Java, in the following way:

//myMap is an instance of Java TreeMap&lt;String, Object&gt;

ByteArrayOutputStream a = new ByteArrayOutputStream();
GZIPOutputStream b = new GZIPOutputStream(a);
ObjectOutputStream c = new ObjectOutputStream(b);
c.writeObject(myMap);
c.close();
byte[] bytes = a.toByteArray()

Below are the attempts I made

step1 - uncompressed the bytes (in the variable result) using

   //att is the byte array received 

   buf := bytes.NewBuffer(att)
   reader, _ := gzip.NewReader(buf)
   defer reader.Close()
   result , _ := ioutil.ReadAll(reader)

step2 - read object out of uncompressed bytes - but failed

    var decodedMap map[string]interface{}
	d := gob.NewDecoder(bytes.NewBuffer(*result*))
	err = d.Decode(&amp;decodedMap)
	if err != nil {
		panic(err)
	}	
 
    error = gob: encoded unsigned integer out of range

But when I convert the (byte array) result to string in Go, I see the encoded treemap details and the contents

   map:  �� sr java.util.TreeMap��&gt;-%j� Lt NAMEt JOHNt AGEt 32t LOCODEsr java.lang.Long;���̏#� J valuexr java.lang.Number������  xp  y

Can someone help me out here?

答案1

得分: 1

你可以(不容易地)在Go中反序列化这些映射,因为序列化数据包含了Java特定的数据,这些数据用于实例化和初始化原始的Java类(在这种情况下是java.util.TreeMap),而这些数据对于Go应用程序来说是未知的。Java对象序列化和由encoding/gob实现的编码之间没有任何关联;前者是特定于Java的,而后者是特定于Go的。

相反,尝试以一种与语言无关的方式对Java对象进行序列化,例如JSON,然后在Go(或任何其他语言)中进行解码。

英文:

You can't (easily) deserialize those maps in Go, because the serialized data contains Java-specific data, data required to instantiate and initialize the original Java class (java.util.TreeMap in this case), which is obviously unknown to a Go app. Java object serialization and the encoding implemented by encoding/gob have nothing to do with each other; the former is specific to Java and the latter is specific to Go.

Instead try to serialize the Java object in a language-neutral way, e.g. to JSON, which you can decode in Go (or in any other language).

huangapple
  • 本文由 发表于 2021年7月8日 18:49:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/68300187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定