Skip to content

Commit 6558de9

Browse files
vkryachkoBrian Chen
authored and
Brian Chen
committed
Add Map<String, Object> support to JsonEncoder. (#1017)
1 parent 39bdc53 commit 6558de9

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,25 @@ JsonValueObjectEncoderContext add(@Nullable Object o) throws IOException, Encodi
192192
jsonWriter.endArray();
193193
return this;
194194
}
195+
if (o instanceof Map) {
196+
@SuppressWarnings("unchecked")
197+
Map<Object, Object> map = (Map<Object, Object>) o;
198+
jsonWriter.beginObject();
199+
for (Map.Entry<Object, Object> entry : map.entrySet()) {
200+
Object key = entry.getKey();
201+
try {
202+
add((String) key, entry.getValue());
203+
} catch (ClassCastException ex) {
204+
throw new EncodingException(
205+
String.format(
206+
"Only String keys are currently supported in maps, got %s of type %s instead.",
207+
key, key.getClass()),
208+
ex);
209+
}
210+
}
211+
jsonWriter.endObject();
212+
return this;
213+
}
195214
@SuppressWarnings("unchecked") // safe because get the encoder by checking the object's type.
196215
ObjectEncoder<Object> objectEncoder = (ObjectEncoder<Object>) objectEncoders.get(o.getClass());
197216
if (objectEncoder != null) {

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.encoders.json;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static org.junit.Assert.assertThrows;
1819
import static org.mockito.ArgumentMatchers.any;
1920
import static org.mockito.Mockito.doThrow;
2021
import static org.mockito.Mockito.mock;
@@ -29,8 +30,8 @@
2930
import java.io.Writer;
3031
import java.nio.charset.Charset;
3132
import java.util.Calendar;
33+
import java.util.Collections;
3234
import java.util.TimeZone;
33-
import org.junit.Assert;
3435
import org.junit.Test;
3536
import org.junit.runner.RunWith;
3637

@@ -204,6 +205,38 @@ public void testEncodingCollection() throws IOException, EncodingException {
204205
"[{\"Name\":\"innerClass\"},{\"Name\":\"innerClass\"}]"));
205206
}
206207

208+
@Test
209+
public void testEncodingMap_withStringKey_shouldSucceed() throws EncodingException {
210+
ObjectEncoder<DummyClass> objectEncoder =
211+
(o, ctx) -> {
212+
ctx.add("map", Collections.singletonMap("key", true));
213+
};
214+
215+
String result =
216+
new JsonDataEncoderBuilder()
217+
.registerEncoder(DummyClass.class, objectEncoder)
218+
.build()
219+
.encode(DummyClass.INSTANCE);
220+
221+
assertThat(result).isEqualTo(String.format("{\"map\":%s}", "{\"key\":true}"));
222+
}
223+
224+
@Test
225+
public void testEncodingMap_withNonStringKey_shouldFail() {
226+
ObjectEncoder<DummyClass> objectEncoder =
227+
(o, ctx) -> {
228+
ctx.add("map", Collections.singletonMap(1L, true));
229+
};
230+
231+
DataEncoder encoder =
232+
new JsonDataEncoderBuilder().registerEncoder(DummyClass.class, objectEncoder).build();
233+
234+
assertThrows(
235+
"Only String keys are currently supported in maps",
236+
EncodingException.class,
237+
() -> encoder.encode(DummyClass.INSTANCE));
238+
}
239+
207240
@Test
208241
public void testEncodingBytes() throws IOException, EncodingException {
209242
ObjectEncoder<DummyClass> objectEncoder =
@@ -336,7 +369,7 @@ public void testEncodingComplexTypes_InnerExtendedEncoder()
336369
@Test
337370
public void testMissingEncoder() throws IOException, EncodingException {
338371
DataEncoder dataEncoder = new JsonDataEncoderBuilder().build();
339-
Assert.assertThrows(EncodingException.class, () -> dataEncoder.encode(DummyClass.INSTANCE));
372+
assertThrows(EncodingException.class, () -> dataEncoder.encode(DummyClass.INSTANCE));
340373
}
341374

342375
@Test
@@ -345,7 +378,7 @@ public void testEncoderError() throws IOException, EncodingException {
345378
Writer mockWriter = mock(Writer.class);
346379
doThrow(IOException.class).when(mockWriter).write(any(String.class));
347380

348-
Assert.assertThrows(
381+
assertThrows(
349382
IOException.class,
350383
() ->
351384
new JsonDataEncoderBuilder()

0 commit comments

Comments
 (0)