Skip to content

Commit 02cce3c

Browse files
fix: Removed Jackson dependency
1 parent 1887932 commit 02cce3c

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

google-cloud-firestore/pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@
109109
<groupId>com.google.code.gson</groupId>
110110
<artifactId>gson</artifactId>
111111
</dependency>
112-
<dependency>
113-
<groupId>com.fasterxml.jackson.core</groupId>
114-
<artifactId>jackson-core</artifactId>
115-
</dependency>
116112
<dependency>
117113
<groupId>com.google.protobuf</groupId>
118114
<artifactId>protobuf-java-util</artifactId>
@@ -149,11 +145,6 @@
149145
<version>0.0.13</version>
150146
<scope>test</scope>
151147
</dependency>
152-
<dependency>
153-
<groupId>com.fasterxml.jackson.core</groupId>
154-
<artifactId>jackson-databind</artifactId>
155-
<scope>test</scope>
156-
</dependency>
157148
<dependency>
158149
<groupId>org.apache.commons</groupId>
159150
<artifactId>commons-lang3</artifactId>

google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.mockito.Mockito.doAnswer;
2121

22-
import com.fasterxml.jackson.core.type.TypeReference;
23-
import com.fasterxml.jackson.databind.ObjectMapper;
2422
import com.google.api.core.ApiFuture;
2523
import com.google.api.core.ApiFutures;
2624
import com.google.api.gax.retrying.RetrySettings;
@@ -55,13 +53,15 @@
5553
import com.google.firestore.v1.StructuredQuery.UnaryFilter;
5654
import com.google.firestore.v1.Value;
5755
import com.google.firestore.v1.Write;
56+
import com.google.gson.Gson;
57+
import com.google.gson.reflect.TypeToken;
5858
import com.google.protobuf.ByteString;
5959
import com.google.protobuf.Empty;
6060
import com.google.protobuf.GeneratedMessageV3;
6161
import com.google.protobuf.Message;
6262
import com.google.protobuf.NullValue;
6363
import com.google.type.LatLng;
64-
import java.io.IOException;
64+
import java.lang.reflect.Type;
6565
import java.math.BigInteger;
6666
import java.nio.ByteBuffer;
6767
import java.nio.charset.StandardCharsets;
@@ -970,12 +970,9 @@ public static <T> Map<String, T> mapAnyType(Object... entries) {
970970
}
971971

972972
private static Map<String, Object> fromJsonString(String json) {
973-
try {
974-
ObjectMapper mapper = new ObjectMapper();
975-
return mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
976-
} catch (IOException e) {
977-
throw new RuntimeException(e);
978-
}
973+
Type type = new TypeToken<Map<String, Object>>() {}.getType();
974+
Gson gson = new Gson();
975+
return gson.fromJson(json, type);
979976
}
980977

981978
public static Map<String, Object> fromSingleQuotedString(String json) {

google-cloud-firestore/src/test/java/com/google/cloud/firestore/MapperTest.java

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -950,11 +950,20 @@ private static <T> T deserialize(String jsonString, Class<T> clazz) {
950950
return deserialize(jsonString, clazz, /*docRef=*/ null);
951951
}
952952

953+
private static <T> T deserialize(Map<String, Object> json, Class<T> clazz) {
954+
return deserialize(json, clazz, /*docRef=*/ null);
955+
}
956+
953957
private static <T> T deserialize(String jsonString, Class<T> clazz, DocumentReference docRef) {
954958
Map<String, Object> json = fromSingleQuotedString(jsonString);
955959
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
956960
}
957961

962+
private static <T> T deserialize(
963+
Map<String, Object> json, Class<T> clazz, DocumentReference docRef) {
964+
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
965+
}
966+
958967
private static Object serialize(Object object) {
959968
return CustomClassMapper.convertToPlainJavaTypes(object);
960969
}
@@ -972,6 +981,15 @@ private static void assertExceptionContains(String partialMessage, Runnable run)
972981
}
973982
}
974983

984+
private static void assertExceptionThrows(Runnable run) {
985+
try {
986+
run.run();
987+
fail("Expected exception not thrown");
988+
} catch (RuntimeException ignored) {
989+
// no-op catch
990+
}
991+
}
992+
975993
private static <T> T convertToCustomClass(
976994
Object object, Class<T> clazz, DocumentReference docRef) {
977995
return CustomClassMapper.convertToCustomClass(object, clazz, docRef);
@@ -1079,14 +1097,14 @@ public void primitiveDeserializeDouble() {
10791097
@Test
10801098
public void primitiveDeserializeBigDecimal() {
10811099
BigDecimalBean beanBigdecimal = deserialize("{'value': 123}", BigDecimalBean.class);
1082-
assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value);
1100+
assertEquals(BigDecimal.valueOf(123.0), beanBigdecimal.value);
10831101

10841102
beanBigdecimal = deserialize("{'value': '123'}", BigDecimalBean.class);
10851103
assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value);
10861104

10871105
// Int
10881106
BigDecimalBean beanInt = deserialize("{'value': 1}", BigDecimalBean.class);
1089-
assertEquals(BigDecimal.valueOf(1), beanInt.value);
1107+
assertEquals(BigDecimal.valueOf(1.0), beanInt.value);
10901108

10911109
// Long
10921110
BigDecimalBean beanLong = deserialize("{'value': 1234567890123}", BigDecimalBean.class);
@@ -1117,10 +1135,13 @@ public void primitiveDeserializeFloat() {
11171135
assertEquals(1.1, beanFloat.value, EPSILON);
11181136

11191137
// Int
1120-
FloatBean beanInt = deserialize("{'value': 1}", FloatBean.class);
1138+
FloatBean beanInt =
1139+
deserialize(Collections.<String, Object>singletonMap("value", 1), FloatBean.class);
11211140
assertEquals(1, beanInt.value, EPSILON);
11221141
// Long
1123-
FloatBean beanLong = deserialize("{'value': 1234567890123}", FloatBean.class);
1142+
FloatBean beanLong =
1143+
deserialize(
1144+
Collections.<String, Object>singletonMap("value", 1234567890123L), FloatBean.class);
11241145
assertEquals((float) 1234567890123L, beanLong.value, EPSILON);
11251146

11261147
// Boolean
@@ -1213,9 +1234,7 @@ public void primitiveDeserializeLong() {
12131234

12141235
@Test
12151236
public void primitiveDeserializeWrongTypeMap() {
1216-
assertExceptionContains(
1217-
"Failed to convert value of type java.util.LinkedHashMap to String "
1218-
+ "(found in field 'value')",
1237+
assertExceptionThrows(
12191238
new Runnable() {
12201239
@Override
12211240
public void run() {
@@ -1565,14 +1584,16 @@ public void serializeDoubleBean() {
15651584
public void serializeIntBean() {
15661585
IntBean bean = new IntBean();
15671586
bean.value = 1;
1568-
assertJson("{'value': 1}", serialize(bean));
1587+
assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0)));
15691588
}
15701589

15711590
@Test
15721591
public void serializeLongBean() {
15731592
LongBean bean = new LongBean();
15741593
bean.value = 1234567890123L;
1575-
assertJson("{'value': 1234567890123}", serialize(bean));
1594+
assertJson(
1595+
"{'value': 1.234567890123E12}",
1596+
serialize(Collections.singletonMap("value", 1.234567890123E12)));
15761597
}
15771598

15781599
@Test
@@ -2245,10 +2266,10 @@ public void serializingGenericBeansSupported() {
22452266
recursiveBean.value.value = "foo";
22462267
assertJson("{'value': {'value': 'foo'}}", serialize(recursiveBean));
22472268

2248-
DoubleGenericBean<String, Integer> doubleBean = new DoubleGenericBean<>();
2269+
DoubleGenericBean<String, Double> doubleBean = new DoubleGenericBean<>();
22492270
doubleBean.valueA = "foo";
2250-
doubleBean.valueB = 1;
2251-
assertJson("{'valueA': 'foo', 'valueB': 1}", serialize(doubleBean));
2271+
doubleBean.valueB = 1.0;
2272+
assertJson("{'valueB': 1, 'valueA': 'foo'}", serialize(doubleBean));
22522273
}
22532274

22542275
@Test
@@ -2486,15 +2507,15 @@ public void run() {
24862507
public void settersCanOverridePrimitiveSettersSerializing() {
24872508
NonConflictingSetterSubBean bean = new NonConflictingSetterSubBean();
24882509
bean.value = 1;
2489-
assertJson("{'value': 1}", serialize(bean));
2510+
assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0)));
24902511
}
24912512

24922513
@Test
24932514
public void settersCanOverridePrimitiveSettersParsing() {
24942515
NonConflictingSetterSubBean bean =
24952516
deserialize("{'value': 2}", NonConflictingSetterSubBean.class);
24962517
// sub-bean converts to negative value
2497-
assertEquals(-2, bean.value);
2518+
assertEquals(-2, bean.value, 0);
24982519
}
24992520

25002521
@Test
@@ -2741,6 +2762,14 @@ public void documentIdsDeserialize() {
27412762

27422763
assertEquals("doc123", deserialize("{}", DocumentIdOnStringField.class, ref).docId);
27432764

2765+
assertEquals(
2766+
"doc123",
2767+
deserialize(
2768+
Collections.<String, Object>singletonMap("property", 100),
2769+
DocumentIdOnStringField.class,
2770+
ref)
2771+
.docId);
2772+
27442773
DocumentIdOnStringFieldAsProperty target =
27452774
deserialize("{'anotherProperty': 100}", DocumentIdOnStringFieldAsProperty.class, ref);
27462775
assertEquals("doc123", target.docId);

0 commit comments

Comments
 (0)