Skip to content

Commit 904818b

Browse files
authored
Bug fixes and improvements (#4)
1 parent 6767616 commit 904818b

19 files changed

+283
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## Unreleased
88
There are currently no unreleased changes.
99

10+
## [0.6.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.6.0) (2018-02-28)
11+
12+
### Changed
13+
14+
- Introduce adapter for `Long` type as discussed in [#2](https://github.com/marcospassos/java-php-serializer/issues/2).
15+
- Fix missing builtin adapters reported in [#2](https://github.com/marcospassos/java-php-serializer/issues/2).
16+
- 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).
17+
1018
## [0.5.2](https://github.com/marcospassos/java-php-serializer/releases/tag/0.5.2) (2017-07-12)
1119

1220
### Changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ to the `dependencies` section of its `pom.xml` file:
5656
<dependency>
5757
<groupId>com.marcospassos</groupId>
5858
<artifactId>phpserializer</artifactId>
59-
<version>0.5.2</version>
59+
<version>0.6.0</version>
6060
</dependency>
6161
</dependencies>
6262
```
@@ -82,8 +82,9 @@ Serializer serializer = new SerializerBuilder()
8282
// Sets the naming strategy to convert the name of classes
8383
// and fields from Java to PHP (default: PsrNamingStrategy)
8484
.setNamingStrategy(new MyCustomNamingStrategy())
85-
// Registers all builtin adapters (default: all built-in adapters)
86-
.registerBuiltinAdapters()
85+
// Registers all builtin adapters, using UTF-8 for encoding strings
86+
// (default: all built-in adapters, UTF-8 charset)
87+
.registerBuiltinAdapters(Charset.forName("UTF-8"))
8788
// Register a custom type adapter
8889
.registerAdapter(CustomObject.class, new CustomObjectAdapter())
8990
// Creates the serialized based on the given configuration
@@ -206,7 +207,7 @@ marcos@marcospassos.com instead of using the issue tracker.
206207
All contents of this package are licensed under the [MIT license](LICENSE).
207208

208209
```
209-
Copyright (c) 2017 Marcos Passos <marcos@marcospassos.com>
210+
Copyright (c) 2018 Marcos Passos <marcos@marcospassos.com>
210211
211212
Permission is hereby granted, free of charge, to any person obtaining a
212213
copy of this software and associated documentation files (the "Software"),
@@ -227,8 +228,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
227228
DEALINGS IN THE SOFTWARE.
228229
```
229230

230-
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.5.2-blue.svg
231-
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.5.2%7Cjar
231+
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.6.0-blue.svg
232+
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.6.0%7Cjar
232233
[coverall-status]: https://coveralls.io/github/marcospassos/java-php-serializer
233234
[coverall-badge]: https://coveralls.io/repos/github/marcospassos/java-php-serializer/badge.svg
234235
[travis-badge]: https://travis-ci.org/marcospassos/java-php-serializer.svg?branch=master
@@ -243,4 +244,4 @@ DEALINGS IN THE SOFTWARE.
243244
[issue-tracker]: https://github.com/marcospassos/java-php-serializer/issues
244245
[repository]: https://github.com/marcospassos/java-php-serializer
245246
[releases-page]: https://github.com/marcospassos/java-php-serializer/releases
246-
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.5.2
247+
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.6.0

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.marcospassos</groupId>
88
<artifactId>phpserializer</artifactId>
9-
<version>0.5.2-SNAPSHOT</version>
9+
<version>0.6.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java PHP Serializer</name>

src/main/java/com/marcospassos/phpserializer/SerializerBuilder.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.marcospassos.phpserializer;
22

3+
import java.nio.charset.Charset;
34
import java.util.ArrayList;
45
import java.util.Collection;
56
import java.util.HashMap;
@@ -9,6 +10,8 @@
910
import com.marcospassos.phpserializer.adapter.BooleanAdapter;
1011
import com.marcospassos.phpserializer.adapter.CollectionAdapter;
1112
import com.marcospassos.phpserializer.adapter.IntegerAdapter;
13+
import com.marcospassos.phpserializer.adapter.LongAdapter;
14+
import com.marcospassos.phpserializer.adapter.MapAdapter;
1215
import com.marcospassos.phpserializer.adapter.ObjectAdapter;
1316
import com.marcospassos.phpserializer.adapter.ReferableObjectAdapter;
1417
import com.marcospassos.phpserializer.adapter.StringAdapter;
@@ -149,9 +152,23 @@ public SerializerBuilder registerAdapter(Class type, TypeAdapter adapter)
149152
/**
150153
* Registers all builtin adapters.
151154
*
155+
* Strings are serialized in UTF-8 by default.
156+
*
152157
* @return The current builder.
153158
*/
154159
public SerializerBuilder registerBuiltinAdapters()
160+
{
161+
return registerBuiltinAdapters(Charset.forName("UTF-8"));
162+
}
163+
164+
/**
165+
* Registers all builtin adapters.
166+
*
167+
* @param charset The default charset used to serialize strings.
168+
*
169+
* @return The current builder.
170+
*/
171+
public SerializerBuilder registerBuiltinAdapters(Charset charset)
155172
{
156173
ArrayAdapter arrayAdapter = new ArrayAdapter();
157174

@@ -164,10 +181,14 @@ public SerializerBuilder registerBuiltinAdapters()
164181
registerAdapter(double[].class, arrayAdapter);
165182
registerAdapter(char[].class, arrayAdapter);
166183
registerAdapter(short[].class, arrayAdapter);
184+
185+
registerAdapter(Map.class, new MapAdapter());
167186
registerAdapter(Collection.class, new CollectionAdapter());
168187
registerAdapter(Boolean.class, new BooleanAdapter());
188+
registerAdapter(Double.class, new IntegerAdapter());
169189
registerAdapter(Integer.class, new IntegerAdapter());
170-
registerAdapter(String.class, new StringAdapter());
190+
registerAdapter(Long.class, new LongAdapter());
191+
registerAdapter(String.class, new StringAdapter(charset));
171192
registerAdapter(Object.class, new ReferableObjectAdapter<>(
172193
new ObjectAdapter<>()
173194
));

src/main/java/com/marcospassos/phpserializer/Writer.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.marcospassos.phpserializer;
33

44
import java.lang.reflect.Modifier;
5+
import java.nio.charset.Charset;
56
import com.marcospassos.phpserializer.state.FinishedState;
67
import com.marcospassos.phpserializer.state.WritingValueState;
78

@@ -246,11 +247,26 @@ public void writeNull()
246247
* @param value The value.
247248
*/
248249
public void writeString(String value)
250+
{
251+
writeString(value, Charset.forName("UTF-8"));
252+
}
253+
254+
/**
255+
* Writes a string in the specified charset to the buffer.
256+
*
257+
* @param value The value.
258+
* @param charset The charset to be used to serialize the value.
259+
*/
260+
public void writeString(String value, Charset charset)
249261
{
250262
setState(state.value());
251263

264+
byte[] bytes = value.getBytes(charset);
265+
266+
value = new String(bytes, charset);
267+
252268
buffer.append("s:");
253-
buffer.append(value.length());
269+
buffer.append(bytes.length);
254270
buffer.append(":\"");
255271
buffer.append(value);
256272
buffer.append("\";");
@@ -284,6 +300,20 @@ public void writeInteger(Integer value)
284300
buffer.append(';');
285301
}
286302

303+
/**
304+
* Writes an long value to the buffer.
305+
*
306+
* @param value The value.
307+
*/
308+
public void writeInteger(Long value)
309+
{
310+
setState(state.value());
311+
312+
buffer.append("i:");
313+
buffer.append(value);
314+
buffer.append(';');
315+
}
316+
287317
/**
288318
* Writes a float value to the buffer.
289319
*

src/main/java/com/marcospassos/phpserializer/adapter/ArrayAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.marcospassos.phpserializer.Writer;
77

88
/**
9-
* Adapter for handling arrays.
9+
* Adapter for {@code Array} type.
1010
*
1111
* @param <T> The type of values in the list.
1212
*

src/main/java/com/marcospassos/phpserializer/adapter/BooleanAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.marcospassos.phpserializer.Writer;
66

77
/**
8-
* Adapter for boolean values.
8+
* Adapter for {@code Boolean} type.
99
*
1010
* @author Marcos Passos
1111
* @since 1.0

src/main/java/com/marcospassos/phpserializer/adapter/CollectionAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.marcospassos.phpserializer.Writer;
77

88
/**
9-
* Adapter for handling collections.
9+
* Adapter for {@code Collection} type.
1010
*
1111
* @param <T> The type of values in the collection.
1212
*

src/main/java/com/marcospassos/phpserializer/adapter/DoubleAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.marcospassos.phpserializer.Writer;
66

77
/**
8-
* Adapter for double values.
8+
* Adapter for {@code Double} type.
99
*
1010
* @author Marcos Passos
1111
* @since 1.0

src/main/java/com/marcospassos/phpserializer/adapter/IntegerAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.marcospassos.phpserializer.Writer;
66

77
/**
8-
* Adapter for boolean values.
8+
* Adapter for {@code Integer} values.
99
*
1010
* @author Marcos Passos
1111
* @since 1.0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.marcospassos.phpserializer.adapter;
2+
3+
import com.marcospassos.phpserializer.Context;
4+
import com.marcospassos.phpserializer.TypeAdapter;
5+
import com.marcospassos.phpserializer.Writer;
6+
7+
/**
8+
* Adapter for {@code Long} values.
9+
*
10+
* @author Marcos Passos
11+
* @since 1.0
12+
*/
13+
public class LongAdapter implements TypeAdapter<Long>
14+
{
15+
@Override
16+
public void write(Long value, Writer writer, Context context)
17+
{
18+
writer.writeInteger(value);
19+
}
20+
}

src/main/java/com/marcospassos/phpserializer/adapter/MapAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.marcospassos.phpserializer.Writer;
77

88
/**
9-
* Adapter for handling maps.
9+
* Adapter for {@code Map} type.
1010
*
1111
* @param <K> the type of keys maintained by the map.
1212
* @param <V> the type of mapped values.

src/main/java/com/marcospassos/phpserializer/adapter/ObjectAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import com.marcospassos.phpserializer.util.ReflectionUtils;
1212

1313
/**
14-
* Adapter for string values.
14+
* Base adapter for {@code Object} type.
1515
*
1616
* @author Marcos Passos
1717
* @since 1.0
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
package com.marcospassos.phpserializer.adapter;
22

3+
import java.nio.charset.Charset;
34
import com.marcospassos.phpserializer.Context;
45
import com.marcospassos.phpserializer.TypeAdapter;
56
import com.marcospassos.phpserializer.Writer;
67

78
/**
8-
* Adapter for handling strings.
9+
* Adapter for string type.
910
*
1011
* @author Marcos Passos
1112
* @since 1.0
1213
*/
1314
public class StringAdapter implements TypeAdapter<String>
1415
{
16+
/**
17+
* The charset.
18+
*/
19+
private Charset charset;
20+
21+
/**
22+
* Creates a adapter for strings encoded in UTF-8.
23+
*/
24+
public StringAdapter() {
25+
this(Charset.forName("UTF-8"));
26+
}
27+
28+
/**
29+
* Creates a adapter for strings encoded with the specified charset.
30+
*
31+
* @param charset
32+
*/
33+
public StringAdapter(Charset charset) {
34+
this.charset = charset;
35+
}
36+
1537
@Override
1638
public void write(String value, Writer writer, Context context)
1739
{
18-
writer.writeString(value);
40+
writer.writeString(value, this.charset);
1941
}
2042
}

src/test/java/com/marcospassos/phpserializer/SerializerBuilderTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.marcospassos.phpserializer;
22

33
import java.lang.reflect.Field;
4+
import java.nio.charset.Charset;
45
import java.util.Map;
56
import com.marcospassos.phpserializer.exclusion.DisjunctionExclusionStrategy;
67
import com.marcospassos.phpserializer.exclusion.NoExclusionStrategy;
@@ -14,6 +15,7 @@
1415
import static org.mockito.ArgumentMatchers.any;
1516
import static org.mockito.Mockito.mock;
1617
import static org.mockito.Mockito.verify;
18+
import static org.mockito.Mockito.verifyNoMoreInteractions;
1719
import static org.mockito.Mockito.when;
1820

1921
/**
@@ -177,4 +179,63 @@ public void builderRegistersBuiltinAdaptersIfNoAdapterIsRegistered()
177179

178180
assertFalse(adapters.isEmpty());
179181
}
182+
183+
@Test
184+
public void builderRegisterStringAdapterUsingUtf8CharsetByDefault() throws Exception
185+
{
186+
SerializerFactory factory = mock(SerializerFactory.class);
187+
188+
new SerializerBuilder(factory).build();
189+
190+
ArgumentCaptor<AdapterRegistry> adapterRegistryArgument =
191+
ArgumentCaptor.forClass(AdapterRegistry.class);
192+
193+
verify(factory).create(
194+
any(NamingStrategy.class),
195+
any(FieldExclusionStrategy.class),
196+
adapterRegistryArgument.capture()
197+
);
198+
199+
AdapterRegistry registry = adapterRegistryArgument.getValue();
200+
Writer writer = mock(Writer.class);
201+
Context context = mock(Context.class);
202+
203+
TypeAdapter<String> adapter = registry.getAdapter(String.class);
204+
205+
adapter.write("foo", writer, context);
206+
207+
verify(writer).writeString("foo", Charset.forName("UTF-8"));
208+
}
209+
210+
@Test
211+
public void builderRegisterStringAdapterUsingSpecifiedCharset() throws Exception
212+
{
213+
SerializerFactory factory = mock(SerializerFactory.class);
214+
215+
Charset charset = Charset.forName("ISO-8859-1");
216+
217+
SerializerBuilder builder = new SerializerBuilder(factory);
218+
builder.registerBuiltinAdapters(charset);
219+
220+
builder.build();
221+
222+
ArgumentCaptor<AdapterRegistry> adapterRegistryArgument =
223+
ArgumentCaptor.forClass(AdapterRegistry.class);
224+
225+
verify(factory).create(
226+
any(NamingStrategy.class),
227+
any(FieldExclusionStrategy.class),
228+
adapterRegistryArgument.capture()
229+
);
230+
231+
AdapterRegistry registry = adapterRegistryArgument.getValue();
232+
Writer writer = mock(Writer.class);
233+
Context context = mock(Context.class);
234+
235+
TypeAdapter<String> adapter = registry.getAdapter(String.class);
236+
237+
adapter.write("foo", writer, context);
238+
239+
verify(writer).writeString("foo", charset);
240+
}
180241
}

0 commit comments

Comments
 (0)