简体   繁体   English

在Java中反序列化序列化的php对象

[英]Unserialize in Java a serialized php object

Does anyone know if it is possible, actually if it has been done, to serialize an object in php and unserialize it in Java (java-php communication). 有谁知道是否有可能(实际上是否已完成)在php中序列化对象并在Java中反序列化该对象(java-php通信)。 Maybe an adapter will be needed. 也许需要适配器。

What do you think? 你怎么看?

Thanks 谢谢

There is serialized-php-parser , which is a Java implementation that can parse php-serialized objects. serialized-php-parser ,这是一个可以解析php-serialized对象的Java实现。 In general, if you have the choice, I wouldn't recommend php-serialized as an exchange format, because it isn't ascii-safe (It contains null-bytes). 通常,如果可以选择,我不建议您将php序列化为交换格式,因为它不是ascii安全的(它包含空字节)。 Go with a format like xml or json instead. 改用xml或json之类的格式。 If you need a bit of type-information, xmlrpc is a good choice. 如果您需要一些类型信息,那么xmlrpc是一个不错的选择。 It has good implementations for both php and Java. 它对php和Java都有很好的实现。

PHP and Java both use their own (obviously different) serialization schemes. PHP和Java都使用自己(显然不同)的序列化方案。 You could however use an interchange format both could read and write. 但是,您可以使用可以读取和写入的交换格式。

The two most obvious examples are XML and JSON. 两个最明显的示例是XML和JSON。

There are others however such as Google Protocol Buffers. 但是,还有其他一些协议,例如Google Protocol Buffers。

Another Java project to work with the PHP serialization format is Pherialize . 另一个使用PHP序列化格式的Java项目是Pherialize

Let's say you are serializing an array like this: 假设您要像这样序列化一个数组:

array(3) {
  [0]=>
  string(8) "A string"
  [1]=>
  int(12345)
  [2]=>
  bool(true)
}

Then you can unserialize it in Java with Pherialize like this: 然后,您可以使用Pherialize在Java中反序列化它,如下所示:

MixedArray list = Pherialize.unserialize(data).toArray();
System.out.println("Item 1: " + list.getString(0));
System.out.println("Item 2: " + list.getInteger(1));
System.out.println("Item 3: " + list.getBoolean(2));

Theoretically, it's certainly possible. 从理论上讲,这肯定是可能的。 It's just bytes after all, and they can be parsed. 毕竟只是字节,可以解析它们。 Of course, the deserialized object would contain only data, not any of the PHP methods. 当然,反序列化的对象将只包含数据,而不包含任何PHP方法。 If you want that, you'd have to rewrite the behaviour as Java classes that correspond directly with the PHP classes. 如果需要,您必须将行为重写为直接与PHP类相对应的Java类。

In practice, the main problem seems to be that the PHP serialization format does not seem to be formally specified - at least there is no link to a specification in the manual . 实际上,主要问题似乎是PHP序列化格式似乎没有正式指定-至少在手册中没有指向规范的链接。

So you might have to dig through the code to understand the format. 因此,您可能必须仔细阅读代码以了解格式。

All in all, it sounds like it would be much easier and more stable to use something like XML serialization - I'm sure both languages have libraries that faciliate this. 总而言之,听起来像使用XML序列化之类的东西会容易得多,也更加稳定-我敢肯定,两种语言都有支持此目的的库。

The JSON format would be a good place to start. JSON格式将是一个不错的起点。 There are implementations for Java , PHP and many other languages. JavaPHP和许多其他语言的实现。

While initially based on the javascript object literal notation, JSON proved convenient for lightweight data transfer between all types of systems. 最初基于javascript对象文字表示法时,JSON证明可方便地在所有类型的系统之间进行轻量级数据传输。

add into pom.xml 添加到pom.xml

<dependency>
    <groupId>de.ailis.pherialize</groupId>
    <artifactId>pherialize</artifactId>
    <version>1.2.1</version>
</dependency>

then in code use 然后在代码中使用

MixedArray list = Pherialize.unserialize(data).toArray(); // data is string `enter code here`

您可以为此使用PHP的var_export()函数,该函数返回要序列化的对象的可分析字符串表示形式。

I remember a snippet for Drupal (PHP CMS) where this functionality was needed. 我记得Drupal(PHP CMS)的一个片段,其中需要此功能。 Just found it, so take a look at Serialized drupal node objects to java (should work with any PHP serialized object). 刚刚找到它,因此来看看Java的序列化drupal节点对象 (应该与任何PHP序列化对象一起使用)。

Maybe you can use that. 也许您可以使用它。 I don't know whether there are issues with newer versions of PHP. 我不知道新版本的PHP是否存在问题。

更好的选择是将php序列化的字符串解析为JSONArray,此存储库( https://github.com/superalsrk/PhpSerialization )可能会帮助您

Like previous answers have mentioned, I would avoid PHP object serialization if possible. 就像前面提到的答案一样,如果可能的话,我将避免PHP对象序列化。 Use JSON (which is actually faster than serialize() in PHP), thrift or some other format that is more universal. 使用JSON(实际上比PHP中的serialize()更快),节俭或其他更通用的格式。

If you have no choice I have been working on a Jackson Module to enable reading and writing serialized PHP from Java. 如果您别无选择,那么我一直在研究Jackson模块来启用从Java读取和写入序列化PHP的功能。 Jackson is a great JSON parser and since PHP serialization format is pretty similar it seemed like a good fit. Jackson是一个出色的JSON解析器,并且由于PHP序列化格式非常相似,因此似乎很合适。 It's not quite complete yet (writing is still a work in progress). 尚未完成(编写仍在进行中)。

Note that there's a Java implementation of PHP . 请注意,有一个Java实现的PHP So you may be able to serialise the object and pass it to your Java-PHP instance, deserialise and then call into your Java infrastructure. 因此,您可以序列化对象并将其传递给Java-PHP实例,反序列化然后调用Java基础结构。

It all sounds a bit of an unholy mess, but perhaps worth looking at! 听起来有些混乱,但也许值得一看!

尝试使用xstream (将Java对象转换为可读的XML)进行序列化,然后编写自己的PHP代码进行反序列化。

使用Web服务(REST,RPC,SOAP)或任何其他存储纯文本的解决方案,允许您从Java读取/重建数据。

Serializing an object in PHP will dump the object properties. 在PHP中序列化对象将转储对象属性。 The resulting string isn't terribly complicated. 生成的字符串并不十分复杂。

echo serialize(
    array(1, null, "mystring", array("key"=>"value"))
);

Results in: 结果是:

a:4:{i:0;i:1;i:1;N;i:2;s:8:"mystring";i:3;a:1:{s:3:"key";s:5:"value";}}

The string identifies datatypes, array lengths, array indexes and values, string lengths... Wouldn't take too much effort to reverse-engineer it and come up with your own parser, I think. 字符串标识数据类型,数组长度,数组索引和值,字符串长度...我想不会花费太多的精力对其进行反向工程并提出自己的解析器。

You may be also interested in using PHP/Java bridge ( http://php-java-bridge.sourceforge.net/ ). 您可能还对使用PHP / Java桥( http://php-java-bridge.sourceforge.net/ )感兴趣。 It has own protocol. 它有自己的协议。 In their site said that it's fast implementation of bridge. 在他们的网站上说这是快速实施的桥梁。

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

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