Unserialize in Java a serialized php object

前端 未结 15 996
闹比i
闹比i 2020-11-29 11:31

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). Maybe an adapter will be

相关标签:
15条回答
  • 2020-11-29 11:39

    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. If you want that, you'd have to rewrite the behaviour as Java classes that correspond directly with the PHP classes.

    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.

    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.

    0 讨论(0)
  • 2020-11-29 11:40

    The JSON format would be a good place to start. There are implementations for Java, PHP and many other languages.

    While initially based on the javascript object literal notation, JSON proved convenient for lightweight data transfer between all types of systems.

    0 讨论(0)
  • 2020-11-29 11:43

    You can somehow make use of PHP's var_export() function for this, which returns a parseable string representation of the object you want to serialize.

    0 讨论(0)
  • 2020-11-29 11:43

    A better choice is to parse php serialized string to JSONArray, this repo (https://github.com/superalsrk/PhpSerialization) may help you

    0 讨论(0)
  • 2020-11-29 11:45

    PHP and Java both use their own (obviously different) serialization schemes. You could however use an interchange format both could read and write.

    The two most obvious examples are XML and JSON.

    There are others however such as Google Protocol Buffers.

    0 讨论(0)
  • 2020-11-29 11:49

    Like previous answers have mentioned, I would avoid PHP object serialization if possible. Use JSON (which is actually faster than serialize() in PHP), thrift or some other format that is more universal.

    If you have no choice I have been working on a Jackson Module to enable reading and writing serialized PHP from Java. Jackson is a great JSON parser and since PHP serialization format is pretty similar it seemed like a good fit. It's not quite complete yet (writing is still a work in progress).

    0 讨论(0)
提交回复
热议问题