简体   繁体   English

如何在PHP中反序列化用Java序列化的对象

[英]How to deserialize in PHP an object serialized in Java

Is there any way to deserialize in PHP an object serialized in Java? 有没有办法在PHP中反序列化一个用Java序列化的对象? IE If I have a Java class that implements Serialization and I use an ObjectOutputStream to write the object, and convert the result to a string, is there a way in PHP to take that string and create a similar object representation from it? IE如果我有一个实现序列化的Java类,我使用ObjectOutputStream来编写对象,并将结果转换为字符串,那么PHP中是否有一种方法可以获取该字符串并从中创建类似的对象表示?

What does the Java Serialized data look like? Java Serialized数据是什么样的?

Response: 响应:

���sr�com.site.entity.SessionV3Data���������xpsr�java.util.HashMap���`��F�

loadFactorI thresholdxp?@ w t sessionIdt 0NmViMzUxYWItZDRmZC00MWY4LWFlMmUtZjg2YmZjZGUxNjg5xx loadFactorI thresholdxp?@ w t sessionIdt 0NmViMzUxYWItZDRmZC00MWY4LWFlMmUtZjg2YmZjZGUxNjg5xx

:) :)

I would heavily recommend you don't do this. 我强烈建议你不要这样做。 Java serialization is meant for a Java instance to both save and load the data (for either transmission to another Java application or persistence between invocations of the same application). Java序列化意味着Java实例既可以保存也可以加载数据(用于传输到另一个Java应用程序或者在同一个应用程序的调用之间进行持久化)。 It was not at all meant to be a cross-platform protocol. 它根本不是一个跨平台的协议。

I would advise you to make an API adapter layer between the two. 我建议你在两者之间建立一个API适配器层。 Output the contents of your Java object to a format you can work with in PHP, be it XML, YAML, or even a binary format (where you could use DataOutputStream). 将Java对象的内容输出为可以在PHP中使用的格式,可以是XML,YAML,甚至是二进制格式(可以使用DataOutputStream)。

What is the easiest way to eat soup with chopsticks when the soup was put in a bowl with a ladle? 将汤放入带有钢包的碗中时,用筷子吃汤的最简单方法是什么? Put the soup in a cup and discard your chopsticks, because chopsticks are a poor choice for aiding in the consumption of soup. 将汤放入杯中并丢弃筷子,因为筷子是帮助消费汤的不良选择。 A cup (ubiquitous) eliminates external dependencies except for "mouth" and "opposable thumbs", both of which come with the standard library of humans. 杯子(无处不在)消除了外部依赖性,除了“嘴”和“可对立的拇指”,两者都带有标准的人类库。

A more elegant solution would be to encode that Java object with a JSON Serializer or XML serializer. 更优雅的解决方案是使用JSON Serializer或XML序列化程序对该Java对象进行编码。 Protocol Buffers or any other intentionally cross-language serialization technique would work fine plus Protocol Buffers can efficiently encode binary data. 协议缓冲区或任何其他有意的跨语言序列化技术可以正常工作,而协议缓冲区可以有效地编码二进制数据。

Some time ago i did something simillar. 前段时间我做了类似的事情。 However i didn't make PHP read "Java serialize" format. 但是我没有让PHP读取“Java serialize”格式。 I did the oposite, that is, made Java serialize itself to a "PHP serialize" format. 我做了oposite,也就是说,使Java自身序列化为“PHP序列化”格式。 This is actually quite easy. 这其实很容易。 Have look at PHPSerializedResponseWriter class that is a part of Solr package: 看看作为Solr包的一部分的PHPSerializedResponseWriter类:

https://github.com/terrancesnyder/solr-analytics/blob/master/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java https://github.com/terrancesnyder/solr-analytics/blob/master/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java

...then all you have to do is just read the string and call: ...然后你所要做的只是读取字符串并调用:

$result = unserialize($string);

From comments in the online PHP manual, there is a Java class that serializes to the PHP serialization format that you can look into. 根据在线PHP手册中的注释,有一个Java类可以序列化为您可以查看的PHP序列化格式。 Then you can unserialize the data using the standard PHP functionality. 然后,您可以使用标准PHP功能对数据进行反序列化。

Is there any way to deserialize in PHP an object serialized in Java? 有没有办法在PHP中反序列化一个用Java序列化的对象?

Yes. 是。 The question is, should you? 问题是,你呢? Exporting the Java object as XML or JSON probably makes more sense. 将Java对象导出为XML或JSON可能更有意义。

The following SO question might also help. 以下SO问题也可能有所帮助。

Dynamically create PHP object based on string 基于字符串动态创建PHP对象

Is it possible to use one of the more common cross platform data formats like JSON to communicate between your Java app and PHP? 是否可以使用一种更常见的跨平台数据格式(如JSON)在Java应用程序和PHP之间进行通信? PHP has plenty of parsers for those formats. PHP有很多针对这些格式的解析器。 Check out json_decode for an example. 查看json_decode的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM