Skip to content

Commit

Permalink
fix: Removed Jackson dependency (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetshuklaoist committed Mar 12, 2021
1 parent e44c092 commit 7ada73d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 43 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 @@ -20,9 +20,11 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.mapAnyType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.cloud.Timestamp;
import com.google.cloud.firestore.annotation.DocumentId;
import com.google.cloud.firestore.annotation.Exclude;
import com.google.cloud.firestore.annotation.PropertyName;
Expand All @@ -43,6 +45,7 @@
import java.util.Objects;
import java.util.Set;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
Expand Down Expand Up @@ -698,11 +701,11 @@ public String getValue() {
}

private static class PackageConstructorBean {
private String value;
private Timestamp value;

PackageConstructorBean() {}

public String getValue() {
public Timestamp getValue() {
return value;
}
}
Expand Down Expand Up @@ -950,11 +953,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 Down Expand Up @@ -1079,23 +1091,35 @@ 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);
BigDecimalBean beanInt =
deserialize(Collections.<String, Object>singletonMap("value", 1), BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1), beanInt.value);

// Long
BigDecimalBean beanLong = deserialize("{'value': 1234567890123}", BigDecimalBean.class);
BigDecimalBean beanLong =
deserialize(
Collections.<String, Object>singletonMap("value", 1234567890123L),
BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1234567890123L), beanLong.value);

// Double
BigDecimalBean beanDouble = deserialize("{'value': 1.1}", BigDecimalBean.class);
BigDecimalBean beanDouble =
deserialize(Collections.<String, Object>singletonMap("value", 1.1), BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1.1), beanDouble.value);

// BigDecimal
BigDecimalBean beanBigDecimal =
deserialize(
Collections.<String, Object>singletonMap("value", BigDecimal.valueOf(1.2)),
BigDecimalBean.class);
assertEquals(BigDecimal.valueOf(1.2), beanBigDecimal.value);

// Boolean
try {
deserialize("{'value': true}", BigDecimalBean.class);
Expand All @@ -1117,10 +1141,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,15 +1240,18 @@ public void primitiveDeserializeLong() {

@Test
public void primitiveDeserializeWrongTypeMap() {
assertExceptionContains(
"Failed to convert value of type java.util.LinkedHashMap to String "
+ "(found in field 'value')",
new Runnable() {
@Override
public void run() {
deserialize("{'value': {'foo': 'bar'}}", StringBean.class);
}
});
String expectedExceptionMessage =
".* Failed to convert value of type .*Map to String \\(found in field 'value'\\).*";
Throwable exception =
assertThrows(
RuntimeException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
deserialize("{'value': {'foo': 'bar'}}", StringBean.class);
}
});
assertTrue(exception.getMessage().matches(expectedExceptionMessage));
}

@Test
Expand Down Expand Up @@ -1565,14 +1595,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 @@ -2061,8 +2093,12 @@ public void run() {

@Test
public void packageConstructorCanBeDeserialized() {
PackageConstructorBean bean = deserialize("{'value': 'foo'}", PackageConstructorBean.class);
assertEquals("foo", bean.value);
Timestamp timestamp = Timestamp.now();
PackageConstructorBean bean =
deserialize(
Collections.<String, Object>singletonMap("value", timestamp),
PackageConstructorBean.class);
assertEquals(timestamp, bean.value);
}

@Test
Expand Down Expand Up @@ -2245,10 +2281,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,7 +2522,7 @@ 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
Expand Down Expand Up @@ -2741,6 +2777,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 7ada73d

Please sign in to comment.