[ACCEPTED]-Unserialize in Java a serialized php object-serialization

Accepted answer
Score: 18

There is serialized-php-parser, which is a Java implementation 9 that can parse php-serialized objects. In 8 general, if you have the choice, I wouldn't 7 recommend php-serialized as an exchange 6 format, because it isn't ascii-safe (It 5 contains null-bytes). Go with a format like 4 xml or json instead. If you need a bit of 3 type-information, xmlrpc is a good choice. It 2 has good implementations for both php and 1 Java.

Score: 15

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

The two most obvious examples 2 are XML and JSON.

There are others however 1 such as Google Protocol Buffers.

Score: 11

Another Java project to work with the PHP 4 serialization format is Pherialize.

Let's say you 3 are serializing an array like this:

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

Then 2 you can unserialize it in Java with Pherialize 1 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));
Score: 6

Theoretically, it's certainly possible. It's 15 just bytes after all, and they can be parsed. Of 14 course, the deserialized object would contain 13 only data, not any of the PHP methods. If 12 you want that, you'd have to rewrite the 11 behaviour as Java classes that correspond 10 directly with the PHP classes.

In practice, the 9 main problem seems to be that the PHP serialization 8 format does not seem to be formally specified 7 - at least there is no link to a specification 6 in the manual.

So you might have to dig through the 5 code to understand the format.

All in all, it 4 sounds like it would be much easier and 3 more stable to use something like XML serialization 2 - I'm sure both languages have libraries 1 that faciliate this.

Score: 5

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

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

Score: 4

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`

0

Score: 3

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

Score: 2

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

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

Score: 1

Serializing an object in PHP will dump the 6 object properties. The resulting string 5 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 4 string identifies datatypes, array lengths, array 3 indexes and values, string lengths... Wouldn't 2 take too much effort to reverse-engineer 1 it and come up with your own parser, I think.

Score: 1

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

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

Score: 1

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

Score: 0

Note that there's a Java implementation of PHP. So you may be able to 5 serialise the object and pass it to your 4 Java-PHP instance, deserialise and then 3 call into your Java infrastructure.

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

Score: 0

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

Score: 0

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

Score: 0

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

More Related questions