Skip to content

Commit 2fd8060

Browse files
authored
Remove parameter charset from the serializer builder
1 parent 904818b commit 2fd8060

File tree

5 files changed

+49
-21
lines changed

5 files changed

+49
-21
lines changed

CHANGELOG.md

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

10+
## [0.7.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.7.0) (2018-02-28)
11+
12+
### Changed
13+
14+
- Remove parameter `charset` from `SerializerBuilder::registerBuiltinAdapters()` in favor of `SerializerBuilder::setCharset()`
15+
1016
## [0.6.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.6.0) (2018-02-28)
1117

1218
### Changed

README.md

Lines changed: 18 additions & 5 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.6.0</version>
59+
<version>0.7.0</version>
6060
</dependency>
6161
</dependencies>
6262
```
@@ -76,17 +76,30 @@ model. The library ships a builder that help us with this task:
7676

7777
```java
7878
Serializer serializer = new SerializerBuilder()
79+
7980
// Adds a custom exclusion strategy to determine which field
8081
// should be serialized or not (default: no exclusion)
8182
.addExclusionStrategy(new MyCustomExclusionStrategy())
83+
8284
// Sets the naming strategy to convert the name of classes
8385
// and fields from Java to PHP (default: PsrNamingStrategy)
8486
.setNamingStrategy(new MyCustomNamingStrategy())
87+
8588
// Registers all builtin adapters, using UTF-8 for encoding strings
8689
// (default: all built-in adapters, UTF-8 charset)
87-
.registerBuiltinAdapters(Charset.forName("UTF-8"))
90+
.registerBuiltinAdapters()
91+
92+
// Sets ISO-8859-1 as the default charset
93+
//
94+
// Notice that setCharset() register a new adapter configured with the
95+
// specified charset. Calling setCharset() prior to registerBuiltinAdapters()
96+
// will have no effect as the previous configuration will get overriden
97+
// by the default adapter which encodes strings in UTF-8.
98+
.setCharset(Charset.forName("ISO-8859-1"))
99+
88100
// Register a custom type adapter
89101
.registerAdapter(CustomObject.class, new CustomObjectAdapter())
102+
90103
// Creates the serialized based on the given configuration
91104
.build();
92105
```
@@ -228,8 +241,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
228241
DEALINGS IN THE SOFTWARE.
229242
```
230243

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
244+
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.7.0-blue.svg
245+
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.7.0%7Cjar
233246
[coverall-status]: https://coveralls.io/github/marcospassos/java-php-serializer
234247
[coverall-badge]: https://coveralls.io/repos/github/marcospassos/java-php-serializer/badge.svg
235248
[travis-badge]: https://travis-ci.org/marcospassos/java-php-serializer.svg?branch=master
@@ -244,4 +257,4 @@ DEALINGS IN THE SOFTWARE.
244257
[issue-tracker]: https://github.com/marcospassos/java-php-serializer/issues
245258
[repository]: https://github.com/marcospassos/java-php-serializer
246259
[releases-page]: https://github.com/marcospassos/java-php-serializer/releases
247-
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.6.0
260+
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.7.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.6.0-SNAPSHOT</version>
9+
<version>0.7.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java PHP Serializer</name>

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ public Serializer build()
8989
);
9090
}
9191

92+
/**
93+
* Configures the serializer to use the specified charset for
94+
* serializing strings.
95+
*
96+
* Calling this method will override any registered adapter for
97+
* {@code String}.
98+
*
99+
* @param charset The default charset to serialize strings.
100+
*
101+
* @return The current builder.
102+
*/
103+
public SerializerBuilder setCharset(Charset charset)
104+
{
105+
registerAdapter(String.class, new StringAdapter(charset));
106+
107+
return this;
108+
}
109+
92110
/**
93111
* Configures the serializer to apply a specific naming policy strategy to
94112
* an objects during serialization.
@@ -152,23 +170,14 @@ public SerializerBuilder registerAdapter(Class type, TypeAdapter adapter)
152170
/**
153171
* Registers all builtin adapters.
154172
*
173+
* Calling this method will override any adapter already registered for
174+
* the types handled by the builtin adapters.
175+
*
155176
* Strings are serialized in UTF-8 by default.
156177
*
157178
* @return The current builder.
158179
*/
159180
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)
172181
{
173182
ArrayAdapter arrayAdapter = new ArrayAdapter();
174183

@@ -188,7 +197,7 @@ public SerializerBuilder registerBuiltinAdapters(Charset charset)
188197
registerAdapter(Double.class, new IntegerAdapter());
189198
registerAdapter(Integer.class, new IntegerAdapter());
190199
registerAdapter(Long.class, new LongAdapter());
191-
registerAdapter(String.class, new StringAdapter(charset));
200+
registerAdapter(String.class, new StringAdapter());
192201
registerAdapter(Object.class, new ReferableObjectAdapter<>(
193202
new ObjectAdapter<>()
194203
));
@@ -247,4 +256,4 @@ private FieldExclusionStrategy getExclusionStrategy()
247256

248257
return exclusionStrategies.get(0);
249258
}
250-
}
259+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public void builderRegisterStringAdapterUsingSpecifiedCharset() throws Exception
215215
Charset charset = Charset.forName("ISO-8859-1");
216216

217217
SerializerBuilder builder = new SerializerBuilder(factory);
218-
builder.registerBuiltinAdapters(charset);
218+
builder.setCharset(charset);
219219

220220
builder.build();
221221

0 commit comments

Comments
 (0)