Skip to content

Commit 4492da7

Browse files
rlazovkryachko
authored andcommitted
Add support for bytes in Encoders (#998)
* Add support for bytes in Encoders. * Add tests. * Fix tests. * Merge byte[] into add(object) logic. * Update api.txt * Fix format
1 parent 2827026 commit 4492da7

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

encoders/firebase-encoders/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package com.google.firebase.encoders {
2929
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(double) throws com.google.firebase.encoders.EncodingException;
3030
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(int) throws com.google.firebase.encoders.EncodingException;
3131
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(boolean) throws com.google.firebase.encoders.EncodingException;
32+
method @NonNull public com.google.firebase.encoders.ValueEncoderContext add(@NonNull byte[]) throws com.google.firebase.encoders.EncodingException;
3233
}
3334

3435
}

encoders/firebase-encoders/src/main/java/com/google/firebase/encoders/ValueEncoderContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ public interface ValueEncoderContext {
4242
/** Adds {@code value} as a primitive encoded value. */
4343
@NonNull
4444
ValueEncoderContext add(boolean value) throws IOException, EncodingException;
45+
46+
/** Adds {@code value} as a encoded array of bytes. */
47+
@NonNull
48+
ValueEncoderContext add(@NonNull byte[] bytes) throws IOException, EncodingException;
4549
}

encoders/firebase-encoders/src/main/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContext.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.encoders.json;
1616

17+
import android.util.Base64;
1718
import android.util.JsonWriter;
1819
import androidx.annotation.NonNull;
1920
import androidx.annotation.Nullable;
@@ -107,6 +108,18 @@ public JsonValueObjectEncoderContext add(boolean value) throws IOException, Enco
107108
return this;
108109
}
109110

111+
@NonNull
112+
@Override
113+
public JsonValueObjectEncoderContext add(@Nullable byte[] bytes)
114+
throws IOException, EncodingException {
115+
if (bytes == null) {
116+
jsonWriter.nullValue();
117+
} else {
118+
jsonWriter.value(Base64.encodeToString(bytes, Base64.NO_WRAP));
119+
}
120+
return this;
121+
}
122+
110123
@NonNull
111124
JsonValueObjectEncoderContext add(@Nullable Object o) throws IOException, EncodingException {
112125
if (o == null) {
@@ -115,6 +128,13 @@ JsonValueObjectEncoderContext add(@Nullable Object o) throws IOException, Encodi
115128
}
116129
// TODO: Add missing primitive types.
117130
if (o.getClass().isArray()) {
131+
// Byte[] are a special case of arrays, because they are not mapped to an array, but to a
132+
// string.
133+
if (o.getClass().getComponentType() == byte.class) {
134+
byte[] bytes = (byte[]) o;
135+
return add(bytes);
136+
}
137+
118138
jsonWriter.beginArray();
119139
if (o.getClass().getComponentType() == int.class) {
120140
int[] array = (int[]) o;

encoders/firebase-encoders/src/test/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContextTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.firebase.encoders.ValueEncoder;
2828
import java.io.IOException;
2929
import java.io.Writer;
30+
import java.nio.charset.Charset;
3031
import java.util.Calendar;
3132
import java.util.TimeZone;
3233
import org.junit.Assert;
@@ -145,6 +146,22 @@ public void testEncodingCollection() throws IOException, EncodingException {
145146
"[{\"Name\":\"innerClass\"},{\"Name\":\"innerClass\"}]"));
146147
}
147148

149+
@Test
150+
public void testEncodingBytes() throws IOException, EncodingException {
151+
ObjectEncoder<DummyClass> objectEncoder =
152+
(o, ctx) -> {
153+
ctx.add("Bytes", "My {custom} value.".getBytes(Charset.forName("UTF-8")));
154+
};
155+
156+
String result =
157+
new JsonDataEncoderBuilder()
158+
.registerEncoder(DummyClass.class, objectEncoder)
159+
.build()
160+
.encode(DummyClass.INSTANCE);
161+
162+
assertThat(result).isEqualTo(String.format("{\"Bytes\":%s}", "\"TXkge2N1c3RvbX0gdmFsdWUu\""));
163+
}
164+
148165
@Test
149166
public void testEncodingCollectionBoxedPrimitives() throws IOException, EncodingException {
150167
ObjectEncoder<DummyClass> objectEncoder =

0 commit comments

Comments
 (0)