Skip to content

Bug fixes and improvements #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
There are currently no unreleased changes.

## [0.6.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.6.0) (2018-02-28)

### Changed

- Introduce adapter for `Long` type as discussed in [#2](https://github.com/marcospassos/java-php-serializer/issues/2).
- Fix missing builtin adapters reported in [#2](https://github.com/marcospassos/java-php-serializer/issues/2).
- Add support for string encoding, fixing cases where strings are badly encoded, as reported in [#3](https://github.com/marcospassos/java-php-serializer/issues/3).

## [0.5.2](https://github.com/marcospassos/java-php-serializer/releases/tag/0.5.2) (2017-07-12)

### Changed
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ to the `dependencies` section of its `pom.xml` file:
<dependency>
<groupId>com.marcospassos</groupId>
<artifactId>phpserializer</artifactId>
<version>0.5.2</version>
<version>0.6.0</version>
</dependency>
</dependencies>
```
Expand All @@ -82,8 +82,9 @@ Serializer serializer = new SerializerBuilder()
// 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 (default: all built-in adapters)
.registerBuiltinAdapters()
// Registers all builtin adapters, using UTF-8 for encoding strings
// (default: all built-in adapters, UTF-8 charset)
.registerBuiltinAdapters(Charset.forName("UTF-8"))
// Register a custom type adapter
.registerAdapter(CustomObject.class, new CustomObjectAdapter())
// Creates the serialized based on the given configuration
Expand Down Expand Up @@ -206,7 +207,7 @@ marcos@marcospassos.com instead of using the issue tracker.
All contents of this package are licensed under the [MIT license](LICENSE).

```
Copyright (c) 2017 Marcos Passos <marcos@marcospassos.com>
Copyright (c) 2018 Marcos Passos <marcos@marcospassos.com>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand All @@ -227,8 +228,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
```

[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.5.2-blue.svg
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.5.2%7Cjar
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.6.0-blue.svg
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.6.0%7Cjar
[coverall-status]: https://coveralls.io/github/marcospassos/java-php-serializer
[coverall-badge]: https://coveralls.io/repos/github/marcospassos/java-php-serializer/badge.svg
[travis-badge]: https://travis-ci.org/marcospassos/java-php-serializer.svg?branch=master
Expand All @@ -243,4 +244,4 @@ DEALINGS IN THE SOFTWARE.
[issue-tracker]: https://github.com/marcospassos/java-php-serializer/issues
[repository]: https://github.com/marcospassos/java-php-serializer
[releases-page]: https://github.com/marcospassos/java-php-serializer/releases
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.5.2
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.6.0
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.marcospassos</groupId>
<artifactId>phpserializer</artifactId>
<version>0.5.2-SNAPSHOT</version>
<version>0.6.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Java PHP Serializer</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.marcospassos.phpserializer;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -9,6 +10,8 @@
import com.marcospassos.phpserializer.adapter.BooleanAdapter;
import com.marcospassos.phpserializer.adapter.CollectionAdapter;
import com.marcospassos.phpserializer.adapter.IntegerAdapter;
import com.marcospassos.phpserializer.adapter.LongAdapter;
import com.marcospassos.phpserializer.adapter.MapAdapter;
import com.marcospassos.phpserializer.adapter.ObjectAdapter;
import com.marcospassos.phpserializer.adapter.ReferableObjectAdapter;
import com.marcospassos.phpserializer.adapter.StringAdapter;
Expand Down Expand Up @@ -149,9 +152,23 @@ public SerializerBuilder registerAdapter(Class type, TypeAdapter adapter)
/**
* Registers all builtin adapters.
*
* Strings are serialized in UTF-8 by default.
*
* @return The current builder.
*/
public SerializerBuilder registerBuiltinAdapters()
{
return registerBuiltinAdapters(Charset.forName("UTF-8"));
}

/**
* Registers all builtin adapters.
*
* @param charset The default charset used to serialize strings.
*
* @return The current builder.
*/
public SerializerBuilder registerBuiltinAdapters(Charset charset)
{
ArrayAdapter arrayAdapter = new ArrayAdapter();

Expand All @@ -164,10 +181,14 @@ public SerializerBuilder registerBuiltinAdapters()
registerAdapter(double[].class, arrayAdapter);
registerAdapter(char[].class, arrayAdapter);
registerAdapter(short[].class, arrayAdapter);

registerAdapter(Map.class, new MapAdapter());
registerAdapter(Collection.class, new CollectionAdapter());
registerAdapter(Boolean.class, new BooleanAdapter());
registerAdapter(Double.class, new IntegerAdapter());
registerAdapter(Integer.class, new IntegerAdapter());
registerAdapter(String.class, new StringAdapter());
registerAdapter(Long.class, new LongAdapter());
registerAdapter(String.class, new StringAdapter(charset));
registerAdapter(Object.class, new ReferableObjectAdapter<>(
new ObjectAdapter<>()
));
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/com/marcospassos/phpserializer/Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.marcospassos.phpserializer;

import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import com.marcospassos.phpserializer.state.FinishedState;
import com.marcospassos.phpserializer.state.WritingValueState;

Expand Down Expand Up @@ -246,11 +247,26 @@ public void writeNull()
* @param value The value.
*/
public void writeString(String value)
{
writeString(value, Charset.forName("UTF-8"));
}

/**
* Writes a string in the specified charset to the buffer.
*
* @param value The value.
* @param charset The charset to be used to serialize the value.
*/
public void writeString(String value, Charset charset)
{
setState(state.value());

byte[] bytes = value.getBytes(charset);

value = new String(bytes, charset);

buffer.append("s:");
buffer.append(value.length());
buffer.append(bytes.length);
buffer.append(":\"");
buffer.append(value);
buffer.append("\";");
Expand Down Expand Up @@ -284,6 +300,20 @@ public void writeInteger(Integer value)
buffer.append(';');
}

/**
* Writes an long value to the buffer.
*
* @param value The value.
*/
public void writeInteger(Long value)
{
setState(state.value());

buffer.append("i:");
buffer.append(value);
buffer.append(';');
}

/**
* Writes a float value to the buffer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for handling arrays.
* Adapter for {@code Array} type.
*
* @param <T> The type of values in the list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for boolean values.
* Adapter for {@code Boolean} type.
*
* @author Marcos Passos
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for handling collections.
* Adapter for {@code Collection} type.
*
* @param <T> The type of values in the collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for double values.
* Adapter for {@code Double} type.
*
* @author Marcos Passos
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for boolean values.
* Adapter for {@code Integer} values.
*
* @author Marcos Passos
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.marcospassos.phpserializer.adapter;

import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.TypeAdapter;
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for {@code Long} values.
*
* @author Marcos Passos
* @since 1.0
*/
public class LongAdapter implements TypeAdapter<Long>
{
@Override
public void write(Long value, Writer writer, Context context)
{
writer.writeInteger(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for handling maps.
* Adapter for {@code Map} type.
*
* @param <K> the type of keys maintained by the map.
* @param <V> the type of mapped values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.marcospassos.phpserializer.util.ReflectionUtils;

/**
* Adapter for string values.
* Base adapter for {@code Object} type.
*
* @author Marcos Passos
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
package com.marcospassos.phpserializer.adapter;

import java.nio.charset.Charset;
import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.TypeAdapter;
import com.marcospassos.phpserializer.Writer;

/**
* Adapter for handling strings.
* Adapter for string type.
*
* @author Marcos Passos
* @since 1.0
*/
public class StringAdapter implements TypeAdapter<String>
{
/**
* The charset.
*/
private Charset charset;

/**
* Creates a adapter for strings encoded in UTF-8.
*/
public StringAdapter() {
this(Charset.forName("UTF-8"));
}

/**
* Creates a adapter for strings encoded with the specified charset.
*
* @param charset
*/
public StringAdapter(Charset charset) {
this.charset = charset;
}

@Override
public void write(String value, Writer writer, Context context)
{
writer.writeString(value);
writer.writeString(value, this.charset);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.marcospassos.phpserializer;

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Map;
import com.marcospassos.phpserializer.exclusion.DisjunctionExclusionStrategy;
import com.marcospassos.phpserializer.exclusion.NoExclusionStrategy;
Expand All @@ -14,6 +15,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -177,4 +179,63 @@ public void builderRegistersBuiltinAdaptersIfNoAdapterIsRegistered()

assertFalse(adapters.isEmpty());
}

@Test
public void builderRegisterStringAdapterUsingUtf8CharsetByDefault() throws Exception
{
SerializerFactory factory = mock(SerializerFactory.class);

new SerializerBuilder(factory).build();

ArgumentCaptor<AdapterRegistry> adapterRegistryArgument =
ArgumentCaptor.forClass(AdapterRegistry.class);

verify(factory).create(
any(NamingStrategy.class),
any(FieldExclusionStrategy.class),
adapterRegistryArgument.capture()
);

AdapterRegistry registry = adapterRegistryArgument.getValue();
Writer writer = mock(Writer.class);
Context context = mock(Context.class);

TypeAdapter<String> adapter = registry.getAdapter(String.class);

adapter.write("foo", writer, context);

verify(writer).writeString("foo", Charset.forName("UTF-8"));
}

@Test
public void builderRegisterStringAdapterUsingSpecifiedCharset() throws Exception
{
SerializerFactory factory = mock(SerializerFactory.class);

Charset charset = Charset.forName("ISO-8859-1");

SerializerBuilder builder = new SerializerBuilder(factory);
builder.registerBuiltinAdapters(charset);

builder.build();

ArgumentCaptor<AdapterRegistry> adapterRegistryArgument =
ArgumentCaptor.forClass(AdapterRegistry.class);

verify(factory).create(
any(NamingStrategy.class),
any(FieldExclusionStrategy.class),
adapterRegistryArgument.capture()
);

AdapterRegistry registry = adapterRegistryArgument.getValue();
Writer writer = mock(Writer.class);
Context context = mock(Context.class);

TypeAdapter<String> adapter = registry.getAdapter(String.class);

adapter.write("foo", writer, context);

verify(writer).writeString("foo", charset);
}
}
Loading