Skip to content

Commit

Permalink
Minor clean up for #195
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 29, 2020
1 parent c570549 commit 0295a5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.apache.avro.Schema;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.avro.deser.ScalarDecoder.*;
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaHelper;

Expand All @@ -22,7 +21,6 @@ public abstract class AvroReaderFactory
protected final static ScalarDecoder READER_LONG = new LongReader();
protected final static ScalarDecoder READER_NULL = new NullReader();
protected final static ScalarDecoder READER_STRING = new StringReader();
private final static ObjectMapper DEFAULT_MAPPER = new ObjectMapper();

/**
* To resolve cyclic types, need to keep track of resolved named
Expand Down Expand Up @@ -345,7 +343,7 @@ protected AvroStructureReader createRecordReader(Schema writerSchema, Schema rea
if (!defaultFields.isEmpty()) {
for (Schema.Field defaultField : defaultFields) {
AvroFieldReader fr = AvroFieldDefaulters.createDefaulter(defaultField.name(),
AvroSchemaHelper.parseDefaultValue(defaultField.defaultVal())
AvroSchemaHelper.objectToJsonNode(defaultField.defaultVal())
);
if (fr == null) {
throw new IllegalArgumentException("Unsupported default type: "+defaultField.schema().getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public abstract class AvroSchemaHelper
{
/**
* Dedicated mapper for handling default values (String <-> JsonNode <-> Object)
*
* @since 2.11
*/
private static final ObjectMapper DEFAULT_VALUE_MAPPER = new ObjectMapper();

Expand Down Expand Up @@ -341,7 +343,9 @@ public static String getFullName(Schema schema) {
return namespace + name;
}
StringBuilder sb = new StringBuilder(1 + namespace.length() + name.length());
// Check if this is a nested class
// 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying
// to support differences between avro-lib 1.8 and 1.9...
// Check if this is a nested class
String nestedClassName = sb.append(namespace).append('$').append(name).toString();
try {
Class.forName(nestedClassName);
Expand All @@ -356,11 +360,17 @@ public static String getFullName(Schema schema) {
}
}

public static JsonNode parseDefaultValue(Object defaultValue) {
/**
* @since 2.11
*/
public static JsonNode objectToJsonNode(Object defaultValue) {
return DEFAULT_VALUE_MAPPER.convertValue(defaultValue, JsonNode.class);
}

public static Object convertDefaultValueToObject(JsonNode defaultJsonValue) {
/**
* @since 2.11
*/
public static Object jsonNodeToObject(JsonNode defaultJsonValue) {
return DEFAULT_VALUE_MAPPER.convertValue(defaultJsonValue, Object.class);
}

Expand All @@ -370,6 +380,8 @@ public static Object convertDefaultValueToObject(JsonNode defaultJsonValue) {
* @param defaultValue The default value to parse, in the form of a JSON string
* @return a parsed JSON representation of the default value
* @throws JsonMappingException If {@code defaultValue} is not valid JSON
*
* @since 2.11
*/
public static JsonNode parseDefaultValue(String defaultValue) throws JsonMappingException {
if (defaultValue == null) {
Expand All @@ -378,6 +390,9 @@ public static JsonNode parseDefaultValue(String defaultValue) throws JsonMapping
try {
return DEFAULT_VALUE_MAPPER.readTree(defaultValue);
} catch (JsonProcessingException e) {
if (e instanceof JsonMappingException) {
throw (JsonMappingException) e;
}
throw new JsonMappingException(null, "Failed to parse default value", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected Schema.Field schemaFieldForWriter(BeanProperty prop, boolean optional)
JsonNode defaultValue = AvroSchemaHelper.parseDefaultValue(prop.getMetadata().getDefaultValue());
writerSchema = reorderUnionToMatchDefaultType(writerSchema, defaultValue);
Schema.Field field = new Schema.Field(prop.getName(), writerSchema, prop.getMetadata().getDescription(),
AvroSchemaHelper.convertDefaultValueToObject(defaultValue));
AvroSchemaHelper.jsonNodeToObject(defaultValue));

AvroMeta meta = prop.getAnnotation(AvroMeta.class);
if (meta != null) {
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project: jackson-datatypes-binaryModules:
#192: (ion) Allow `IonObjectMapper` with class name annotation introspector to deserialize
generic subtypes
(reported, fix provided by Binh T)
#195: Remove dependencies upon Jackson 1.X and Avro's JacksonUtils
(contributed by Bryan H)
- `AvroGenerator` overrides `getOutputContext()` properly

2.10.2 (05-Jan-2020)
Expand Down

0 comments on commit 0295a5e

Please sign in to comment.