Hypnus
Hypnus

Reputation: 191

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

What do you think?

Thanks

Upvotes: 17

Views: 28196

Answers (15)

Rusif Budagov
Rusif Budagov

Reputation: 61

add into 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`

Upvotes: 4

StackBox
StackBox

Reputation: 326

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

Upvotes: 1

Thejesh GN
Thejesh GN

Reputation: 1128

Try xstream (converts Java objects into readable XML) to serialize and then write your own PHP code to deserialize.

Upvotes: 0

jho
jho

Reputation: 111

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).

Upvotes: 1

kayahr
kayahr

Reputation: 22050

Another Java project to work with the PHP serialization format is 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:

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));

Upvotes: 13

Michael Borgwardt
Michael Borgwardt

Reputation: 346377

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.

Upvotes: 6

troelskn
troelskn

Reputation: 117567

There is serialized-php-parser, which is a Java implementation that can parse php-serialized objects. 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). Go with a format like xml or json instead. If you need a bit of type-information, xmlrpc is a good choice. It has good implementations for both php and Java.

Upvotes: 18

ruslan
ruslan

Reputation: 805

You may be also interested in using PHP/Java bridge (http://php-java-bridge.sourceforge.net/). It has own protocol. In their site said that it's fast implementation of bridge.

Upvotes: 0

zimbu668
zimbu668

Reputation: 1335

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.

Upvotes: 5

user7675
user7675

Reputation:

Serializing an object in PHP will dump the object properties. 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.

Upvotes: 1

Marcel Jackwerth
Marcel Jackwerth

Reputation: 54762

I remember a snippet for Drupal (PHP CMS) where this functionality was needed. Just found it, so take a look at Serialized drupal node objects to java (should work with any PHP serialized object).

Maybe you can use that. I don't know whether there are issues with newer versions of PHP.

Upvotes: 2

John Doe
John Doe

Reputation:

Use Web Services (REST, RPC, SOAP) or any other solution storing plain text that will allow you to read/rebuild the data from Java.

Upvotes: 0

cletus
cletus

Reputation: 625227

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.

Upvotes: 15

karim79
karim79

Reputation: 342665

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.

Upvotes: 3

Brian Agnew
Brian Agnew

Reputation: 272337

Note that there's a Java implementation of 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.

It all sounds a bit of an unholy mess, but perhaps worth looking at!

Upvotes: 0

Related Questions