stevebot
stevebot

Reputation: 24005

How to deserialize in PHP an object serialized in Java

Is there any way to deserialize in PHP an object serialized in 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?

What does the Java Serialized data look like?

Response:

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

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

:)

Upvotes: 4

Views: 4672

Answers (6)

Jacek Prucia
Jacek Prucia

Reputation: 1076

Some time ago i did something simillar. However i didn't make PHP read "Java serialize" format. I did the oposite, that is, made Java serialize itself to a "PHP serialize" format. This is actually quite easy. Have look at PHPSerializedResponseWriter class that is a part of Solr package:

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

Upvotes: 2

Thomas Langston
Thomas Langston

Reputation: 3735

Is there any way to deserialize in PHP an object serialized in Java?

Yes. The question is, should you? Exporting the Java object as XML or JSON probably makes more sense.

The following SO question might also help.

Dynamically create PHP object based on string

Upvotes: -1

Hut8
Hut8

Reputation: 6342

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. Protocol Buffers or any other intentionally cross-language serialization technique would work fine plus Protocol Buffers can efficiently encode binary data.

Upvotes: 4

dnagirl
dnagirl

Reputation: 20446

Is it possible to use one of the more common cross platform data formats like JSON to communicate between your Java app and PHP? PHP has plenty of parsers for those formats. Check out json_decode for an example.

Upvotes: -1

Mark Peters
Mark Peters

Reputation: 81104

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

Upvotes: 7

justkt
justkt

Reputation: 14766

From comments in the online PHP manual, there is a Java class that serializes to the PHP serialization format that you can look into. Then you can unserialize the data using the standard PHP functionality.

Upvotes: 1

Related Questions