Skip to content

Commit

Permalink
Make this work with Jackson 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaber committed Sep 14, 2017
1 parent e938bb0 commit 6390e8f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 69 deletions.
13 changes: 2 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,8 @@ before_install:
- export M2_HOME=$PWD/apache-maven-3.3.9
- export PATH=$M2_HOME/bin:$PATH

install: mvn -Ddep.jackson.version=$JACKSON_VERSION -DskipTests=true -Dbasepom.check.skip-all=true -Dmaven.javadoc.skip=true -B install
script: mvn -Ddep.jackson.version=$JACKSON_VERSION -B verify

env:
- JACKSON_VERSION="2.9.1"
- JACKSON_VERSION="2.8.10"
- JACKSON_VERSION="2.7.9"
- JACKSON_VERSION="2.6.7"
- JACKSON_VERSION="2.5.5"
- JACKSON_VERSION="2.4.6"
- JACKSON_VERSION="2.3.5"
install: mvn -DskipTests=true -Dbasepom.check.skip-all=true -Dmaven.javadoc.skip=true -B install
script: mvn -B verify

cache:
directories:
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.hubspot</groupId>
<artifactId>basepom</artifactId>
<version>12.5</version>
<version>15.15</version>
</parent>

<groupId>com.hubspot.jackson</groupId>
<artifactId>jackson-datatype-protobuf</artifactId>
<version>0.9.6-SNAPSHOT</version>
<version>0.9.6-preJackson2.7-SNAPSHOT</version>
<description>Jackson Module that adds support for reading/writing protobufs</description>
<url>https://github.com/HubSpot/jackson-datatype-protobuf</url>

Expand All @@ -28,6 +28,10 @@
</developer>
</developers>

<properties>
<dep.jackson.version>2.6.7</dep.jackson.version>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.hubspot.jackson.datatype.protobuf;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -39,12 +36,6 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class ProtobufDeserializer<T extends Message> extends StdDeserializer<MessageOrBuilder> {
private static final Method INT_PARSER = determineIntParser();
private static final Method LONG_PARSER = determineLongParser();
private static final Method FLOAT_PARSER = determineFloatParser();
private static final Method DOUBLE_PARSER = determineDoubleParser();
private static final Method BOOLEAN_PARSER = determineBooleanParser();

private final T defaultInstance;
private final boolean build;
@SuppressFBWarnings(value="SE_BAD_FIELD")
Expand Down Expand Up @@ -223,15 +214,15 @@ private Object readValue(Message.Builder builder, FieldDescriptor field, Message

switch (field.getJavaType()) {
case INT:
return invoke(INT_PARSER, this, parser, context);
return _parseIntPrimitive(parser, context);
case LONG:
return invoke(LONG_PARSER, this, parser, context);
return _parseLongPrimitive(parser, context);
case FLOAT:
return invoke(FLOAT_PARSER, this, parser, context);
return _parseFloatPrimitive(parser, context);
case DOUBLE:
return invoke(DOUBLE_PARSER, this, parser, context);
return _parseDoublePrimitive(parser, context);
case BOOLEAN:
return invoke(BOOLEAN_PARSER, this, parser, context);
return _parseBooleanPrimitive(parser, context);
case STRING:
switch (parser.getCurrentToken()) {
case VALUE_STRING:
Expand Down Expand Up @@ -353,46 +344,4 @@ private static JsonMappingException mappingException(FieldDescriptor field, Dese
String message = "Can not deserialize instance of " + field.getJavaType() + " out of " + token + " token";
throw context.mappingException(message);
}

private static Method determineIntParser() {
return findParseMethod("_parseIntPrimitive", "_parseInteger");
}

private static Method determineLongParser() {
return findParseMethod("_parseLongPrimitive", "_parseLong");
}

private static Method determineFloatParser() {
return findParseMethod("_parseFloatPrimitive", "_parseFloat");
}

private static Method determineDoubleParser() {
return findParseMethod("_parseDoublePrimitive", "_parseDouble");
}

private static Method determineBooleanParser() {
return findParseMethod("_parseBooleanPrimitive", "_parseBoolean");
}

private static Method findParseMethod(String... names) {
for (String name : names) {
try {
return StdDeserializer.class.getDeclaredMethod(name, JsonParser.class, DeserializationContext.class);
} catch (NoSuchMethodException e) {
// ignored
}
}

throw new RuntimeException("No method found on " + StdDeserializer.class + " for names: " + Arrays.asList(names));
}

private static Object invoke(Method method, Object instance, Object... args) {
try {
return method.invoke(instance, args);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
}

0 comments on commit 6390e8f

Please sign in to comment.