Skip to content

Commit ff818b4

Browse files
committed
feat(serialization): added gson type adapter for byte[]
1 parent b37e634 commit ff818b4

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<logback-version>1.2.3</logback-version>
4747
<guava-version>27.1-android</guava-version>
4848
<gson-version>2.8.5</gson-version>
49+
<commons-codec-version>1.12</commons-codec-version>
4950
<commons-io-version>2.6</commons-io-version>
5051
<commons-lang3-version>3.8.1</commons-lang3-version>
5152
<rx-version>2.2.7</rx-version>
@@ -81,6 +82,11 @@
8182
<version>${okhttp3-version}</version>
8283
<scope>test</scope>
8384
</dependency>
85+
<dependency>
86+
<groupId>commons-codec</groupId>
87+
<artifactId>commons-codec</artifactId>
88+
<version>${commons-codec-version}</version>
89+
</dependency>
8490
<dependency>
8591
<groupId>commons-io</groupId>
8692
<artifactId>commons-io</artifactId>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.cloud.sdk.core.util;
14+
15+
import java.io.IOException;
16+
17+
import org.apache.commons.codec.binary.Base64;
18+
19+
import com.google.gson.TypeAdapter;
20+
import com.google.gson.stream.JsonReader;
21+
import com.google.gson.stream.JsonWriter;
22+
23+
/**
24+
* This class serves as a GSON type adapter for the "byte[]" type.
25+
* Values of type byte[] will be serialized into a string of base64-encoded characters.
26+
*/
27+
public class ByteArrayTypeAdapter extends TypeAdapter<byte[]> {
28+
29+
@Override
30+
public void write(JsonWriter out, byte[] value) throws IOException {
31+
out.value(Base64.encodeBase64String(value));
32+
}
33+
34+
@Override
35+
public byte[] read(JsonReader in) throws IOException {
36+
return Base64.decodeBase64(in.nextString());
37+
}
38+
}

src/main/java/com/ibm/cloud/sdk/core/util/GsonSingleton.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private static void registerTypeAdapters(GsonBuilder builder) {
5252
// Date serializer and deserializer
5353
builder.registerTypeAdapter(Date.class, new DateDeserializer());
5454
builder.registerTypeAdapter(Date.class, new DateSerializer());
55+
builder.registerTypeAdapter(byte[].class, new ByteArrayTypeAdapter());
5556

5657
// Type adapter factory for DynamicModel subclasses.
5758
builder.registerTypeAdapterFactory(new DynamicModelTypeAdapterFactory());
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.cloud.sdk.core.test.util;
14+
15+
import static org.testng.Assert.assertEquals;
16+
17+
import org.junit.Test;
18+
19+
import com.ibm.cloud.sdk.core.service.model.GenericModel;
20+
import com.ibm.cloud.sdk.core.util.GsonSingleton;
21+
22+
/**
23+
*
24+
*/
25+
public class ByteArrayTypeAdapterTest {
26+
private boolean displayOutput = false;
27+
28+
private String serialize(Object obj) {
29+
return GsonSingleton.getGson().toJson(obj);
30+
}
31+
32+
private <T> T deserialize(String json, Class<T> clazz) {
33+
return GsonSingleton.getGson().fromJson(json, clazz);
34+
}
35+
36+
private <T> void testSerDeser(T model, Class<T> clazz) {
37+
String jsonString = serialize(model);
38+
if (displayOutput) {
39+
System.out.println("serialized " + model.getClass().getSimpleName() + ": " + jsonString);
40+
}
41+
T newModel = deserialize(jsonString, clazz);
42+
if (displayOutput) {
43+
System.out.println("de-serialized " + model.getClass().getSimpleName() + ": " + newModel.toString());
44+
}
45+
assertEquals(newModel, model);
46+
}
47+
48+
public class Model1 extends GenericModel {
49+
public String name;
50+
public byte[] bytes;
51+
52+
public Model1(String name, byte[] bytes) {
53+
this.name = name;
54+
this.bytes = bytes;
55+
}
56+
}
57+
58+
@Test
59+
public void testByteArray1() {
60+
Model1 model1 = new Model1("name1", new byte[] {01, 02, 03, 04, 05});
61+
62+
testSerDeser(model1, Model1.class);
63+
}
64+
65+
@Test
66+
public void testByteArray2() {
67+
String bytes = "This is a test of the emergency broadast system. This is only a test.";
68+
Model1 model1 = new Model1("name2", bytes.getBytes());
69+
70+
testSerDeser(model1, Model1.class);
71+
}
72+
}

0 commit comments

Comments
 (0)