Java PHP Serializer

PHP Serializer for Java

License

License

MIT
GroupId

GroupId

com.marcospassos
ArtifactId

ArtifactId

phpserializer
Last Version

Last Version

0.8.0
Release Date

Release Date

Type

Type

jar
Description

Description

Java PHP Serializer
PHP Serializer for Java
Project URL

Project URL

https://github.com/marcospassos/java-php-serializer
Project Organization

Project Organization

Java PHP Serializer
Source Code Management

Source Code Management

https://github.com/marcospassos/java-php-serializer.git

Download phpserializer

How to add to project

<!-- https://jarcasting.com/artifacts/com.marcospassos/phpserializer/ -->
<dependency>
    <groupId>com.marcospassos</groupId>
    <artifactId>phpserializer</artifactId>
    <version>0.8.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.marcospassos/phpserializer/
implementation 'com.marcospassos:phpserializer:0.8.0'
// https://jarcasting.com/artifacts/com.marcospassos/phpserializer/
implementation ("com.marcospassos:phpserializer:0.8.0")
'com.marcospassos:phpserializer:jar:0.8.0'
<dependency org="com.marcospassos" name="phpserializer" rev="0.8.0">
  <artifact name="phpserializer" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.marcospassos', module='phpserializer', version='0.8.0')
)
libraryDependencies += "com.marcospassos" % "phpserializer" % "0.8.0"
[com.marcospassos/phpserializer "0.8.0"]

Dependencies

test (2)

Group / Artifact Type Version
org.mockito : mockito-core jar 2.8.9
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Java PHP Serializer

Build Status Coverage Status Java 7+ License

Latest release: Maven Central

A Java library for serializing objects as PHP serialization format.

The library fully implements the PHP serialization format specification, which includes:

  • Scalar values
  • Objects
  • Serializable (custom serializable objects)
  • Object and variable references

The API documentation is available on GitHub Pages more convenient viewing in browser.

A word of notice

This library does not provide any mechanism for creating a communication channel between Java and PHP. For such purpose, consider using Soluble Java.

Use case

One of the easier way to exchange data between Java and PHP consists in serializing the value to a data-interchanging format, such as JSON or XML, send it through a communication channel and finally deserialize it back to the original form on the PHP side. The problem with this approach is that the cost of deserializing complex objects in PHP is very high.

Most of the serialization libraries in PHP use reflection for re-hydrating objects, and it becomes an issue when you have to deserialize large structures with hundreds of objects. Fortunately, PHP's native serialization is fast and the unserialize() function can handle such cases in a few milliseconds.

This library implements the full format specification through a friendly API that encapsulates the complexity of the serialization process.

Installation

Maven

The PHP Serializer is available in the Maven Central repository. Any Maven based project can use it directly by adding the appropriate entries to the dependencies section of its pom.xml file:

<dependencies>
  <dependency>
    <groupId>com.marcospassos</groupId>
    <artifactId>phpserializer</artifactId>
    <version>0.8.0</version>
  </dependency>
</dependencies>

Binaries

Packaged JARs can be downloaded directly from the releases page and extracted using tar or unzip.

Usage

How to serialize data

The first step to serialize a Java object into a PHP serialization format string is to create a Serializer instance according to your application domain model. The library ships a builder that help us with this task:

Serializer serializer = new SerializerBuilder()

  // Adds a custom exclusion strategy to determine which field
  // should be serialized or not (default: no exclusion)
  .addExclusionStrategy(new MyCustomExclusionStrategy())
  
  // Sets the naming strategy to convert the name of classes
  // and fields from Java to PHP (default: PsrNamingStrategy)
  .setNamingStrategy(new MyCustomNamingStrategy())
  
  // Registers all builtin adapters, using UTF-8 for encoding strings 
  // (default: all built-in adapters, UTF-8 charset)
  .registerBuiltinAdapters()
  
  // Sets ISO-8859-1 as the default charset
  //
  // Notice that setCharset() register a new adapter configured with the 
  // specified charset. Calling setCharset() prior to registerBuiltinAdapters()
  // will have no effect as the previous configuration will get overriden
  // by the default adapter which encodes strings in UTF-8. 
  .setCharset(Charset.forName("ISO-8859-1"))
  
  // Register a custom type adapter
  .registerAdapter(CustomObject.class, new CustomObjectAdapter())
  
  // Creates the serialized based on the given configuration
  .build();

Now you have a Serializer instance configured according to your application domain model. To serialize a value, just invoke serialize(Object object) on the serializer instance:

List list = new ArrayList();
list.add("A string");
list.add(12345);
list.add(true);

// Outputs: a:3:{i:0;s:8:"A string";i:1;i:12345;i:2;b:1;}
System.out.println(serializer.serialize(list));

Naming strategies

A naming strategy allows you to translate the name of classes and fields according to the target domain model. The library ships with a built-in adapter that converts Java packages to PHP namespaces in accordance with PSR-1 rules. If the PSR strategy does not fit your needs you can easily implement a custom naming strategy. Takes as reference the following strategy that appends an underscore to all private fields:

public class UnderscoreNamingStrategy implements NamingStrategy
{
    public String getClassName(Class type)
    {
        return type.getName();
    }

    public String getFieldName(Field field)
    {
        if (Modifier.isPrivate(field.getModifiers())) {
            return "_" + field.getName();
        }

        return field.getName();
    }
}

Type Adapters

A type adapter provides the serializer the logic for how to encode a specific type. The following example shows how to create a custom type adapter to serialize Enums as string using the name of the enum constant:

public class EnumTypeAdapter implements TypeAdapter<Enum>
{
    public void write(Enum value, Writer writer, Context context)
    {
        writer.writeString(value.name());
    }
}

Notice that circular references are handled as per case basis, once it is not always the desirable. For instance, you may not want to serialize lists as objects references, as array is probably the most appropriate corresponding type in PHP. The following example shows how to handle such cases:

public class MyCustomAdapter implements TypeAdapter<CustomObject>
{
    @Override
    public void write(CustomObject object, Writer writer, Context context)
    {
        int reference = context.getReference(object);

        if (reference > 0) {
            writer.writeObjectReference(reference);

            return;
        }

        context.setReference(reference, object);

        // Custom serialization logic
    }
}

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Contributions to the package are always welcome!

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

All contents of this package are licensed under the MIT license.

Copyright (c) 2018 Marcos Passos <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Versions

Version
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.0
0.2.0
0.1.0