Skip to content

Commit

Permalink
fix: Removed Jackson dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetshuklaoist committed Jan 28, 2021
1 parent 1887932 commit 02cce3c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
9 changes: 0 additions & 9 deletions google-cloud-firestore/pom.xml
Expand Up @@ -109,10 +109,6 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
Expand Down Expand Up @@ -149,11 +145,6 @@
<version>0.0.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Expand Up @@ -19,8 +19,6 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doAnswer;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.retrying.RetrySettings;
Expand Down Expand Up @@ -55,13 +53,15 @@
import com.google.firestore.v1.StructuredQuery.UnaryFilter;
import com.google.firestore.v1.Value;
import com.google.firestore.v1.Write;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.Message;
import com.google.protobuf.NullValue;
import com.google.type.LatLng;
import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -970,12 +970,9 @@ public static <T> Map<String, T> mapAnyType(Object... entries) {
}

private static Map<String, Object> fromJsonString(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
throw new RuntimeException(e);
}
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Gson gson = new Gson();
return gson.fromJson(json, type);
}

public static Map<String, Object> fromSingleQuotedString(String json) {
Expand Down
Expand Up @@ -950,11 +950,20 @@ private static <T> T deserialize(String jsonString, Class<T> clazz) {
return deserialize(jsonString, clazz, /*docRef=*/ null);
}

private static <T> T deserialize(Map<String, Object> json, Class<T> clazz) {
return deserialize(json, clazz, /*docRef=*/ null);
}

private static <T> T deserialize(String jsonString, Class<T> clazz, DocumentReference docRef) {
Map<String, Object> json = fromSingleQuotedString(jsonString);
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
}

private static <T> T deserialize(
Map<String, Object> json, Class<T> clazz, DocumentReference docRef) {
return CustomClassMapper.convertToCustomClass(json, clazz, docRef);
}

private static Object serialize(Object object) {
return CustomClassMapper.convertToPlainJavaTypes(object);
}
Expand All @@ -972,6 +981,15 @@ private static void assertExceptionContains(String partialMessage, Runnable run)
}
}

private static void assertExceptionThrows(Runnable run) {
try {
run.run();
fail("Expected exception not thrown");
} catch (RuntimeException ignored) {
// no-op catch
}
}

private static <T> T convertToCustomClass(
Object object, Class<T> clazz, DocumentReference docRef) {
return CustomClassMapper.convertToCustomClass(object, clazz, docRef);
Expand Down Expand Up @@ -1079,14 +1097,14 @@ public void primitiveDeserializeDouble() {
@Test
public void primitiveDeserializeBigDecimal() {
BigDecimalBean beanBigdecimal = deserialize("{'value': 123}", BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(123), beanBigdecimal.value);
assertEquals(BigDecimal.valueOf(123.0), beanBigdecimal.value);

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

// Int
BigDecimalBean beanInt = deserialize("{'value': 1}", BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1), beanInt.value);
assertEquals(BigDecimal.valueOf(1.0), beanInt.value);

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

// Int
FloatBean beanInt = deserialize("{'value': 1}", FloatBean.class);
FloatBean beanInt =
deserialize(Collections.<String, Object>singletonMap("value", 1), FloatBean.class);
assertEquals(1, beanInt.value, EPSILON);
// Long
FloatBean beanLong = deserialize("{'value': 1234567890123}", FloatBean.class);
FloatBean beanLong =
deserialize(
Collections.<String, Object>singletonMap("value", 1234567890123L), FloatBean.class);
assertEquals((float) 1234567890123L, beanLong.value, EPSILON);

// Boolean
Expand Down Expand Up @@ -1213,9 +1234,7 @@ public void primitiveDeserializeLong() {

@Test
public void primitiveDeserializeWrongTypeMap() {
assertExceptionContains(
"Failed to convert value of type java.util.LinkedHashMap to String "
+ "(found in field 'value')",
assertExceptionThrows(
new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -1565,14 +1584,16 @@ public void serializeDoubleBean() {
public void serializeIntBean() {
IntBean bean = new IntBean();
bean.value = 1;
assertJson("{'value': 1}", serialize(bean));
assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0)));
}

@Test
public void serializeLongBean() {
LongBean bean = new LongBean();
bean.value = 1234567890123L;
assertJson("{'value': 1234567890123}", serialize(bean));
assertJson(
"{'value': 1.234567890123E12}",
serialize(Collections.singletonMap("value", 1.234567890123E12)));
}

@Test
Expand Down Expand Up @@ -2245,10 +2266,10 @@ public void serializingGenericBeansSupported() {
recursiveBean.value.value = "foo";
assertJson("{'value': {'value': 'foo'}}", serialize(recursiveBean));

DoubleGenericBean<String, Integer> doubleBean = new DoubleGenericBean<>();
DoubleGenericBean<String, Double> doubleBean = new DoubleGenericBean<>();
doubleBean.valueA = "foo";
doubleBean.valueB = 1;
assertJson("{'valueA': 'foo', 'valueB': 1}", serialize(doubleBean));
doubleBean.valueB = 1.0;
assertJson("{'valueB': 1, 'valueA': 'foo'}", serialize(doubleBean));
}

@Test
Expand Down Expand Up @@ -2486,15 +2507,15 @@ public void run() {
public void settersCanOverridePrimitiveSettersSerializing() {
NonConflictingSetterSubBean bean = new NonConflictingSetterSubBean();
bean.value = 1;
assertJson("{'value': 1}", serialize(bean));
assertJson("{'value': 1}", serialize(Collections.singletonMap("value", 1.0)));
}

@Test
public void settersCanOverridePrimitiveSettersParsing() {
NonConflictingSetterSubBean bean =
deserialize("{'value': 2}", NonConflictingSetterSubBean.class);
// sub-bean converts to negative value
assertEquals(-2, bean.value);
assertEquals(-2, bean.value, 0);
}

@Test
Expand Down Expand Up @@ -2741,6 +2762,14 @@ public void documentIdsDeserialize() {

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

assertEquals(
"doc123",
deserialize(
Collections.<String, Object>singletonMap("property", 100),
DocumentIdOnStringField.class,
ref)
.docId);

DocumentIdOnStringFieldAsProperty target =
deserialize("{'anotherProperty': 100}", DocumentIdOnStringFieldAsProperty.class, ref);
assertEquals("doc123", target.docId);
Expand Down

0 comments on commit 02cce3c

Please sign in to comment.