Skip to content

Commit

Permalink
Cherry-pick fixes #4, #5, #7 and #8 from #44 (wish I had tests too, m…
Browse files Browse the repository at this point in the history
…aybe next step. #1 only affects `master` will do next
  • Loading branch information
cowtowncoder committed Feb 17, 2017
1 parent 60a1bbd commit 7ed5465
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 12 deletions.
Expand Up @@ -19,6 +19,9 @@
*/
public class AvroSchema implements FormatSchema
{
/**
* Format type id used by Jackson components to refer to Avro Format.
*/
public final static String TYPE_ID = "avro";

/**
Expand Down
Expand Up @@ -50,6 +50,6 @@ public void itemsFormat(JsonFormatVisitable visitable, JavaType type)
@Override
public void itemsFormat(JsonFormatTypes type) throws JsonMappingException
{
_elementSchema = AvroSchemaHelper.simpleSchema(type);
_elementSchema = AvroSchemaHelper.simpleSchema(type, _type.getContentType());
}
}
Expand Up @@ -5,15 +5,30 @@
import java.util.List;

import org.apache.avro.Schema;
import org.apache.avro.specific.SpecificData;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;

public abstract class AvroSchemaHelper
{
/**
* Constant used by native Avro Schemas for indicating more specific
* physical class of a value; referenced indirectly to reduce direct
* dependencies to the standard avro library.
*
* @since 2.8.7
*/
protected final static String AVRO_SCHEMA_PROP_CLASS = SpecificData.CLASS_PROP;

protected static String getNamespace(JavaType type) {
Class<?> cls = type.getRawClass();
// 16-Feb-2017, tatu: Fixed as suggested by `baharclerode@github`
Class<?> enclosing = cls.getEnclosingClass();
if (enclosing != null) {
return enclosing.getName() + "$";
}
Package pkg = cls.getPackage();
return (pkg == null) ? "" : pkg.getName();
}
Expand Down Expand Up @@ -42,7 +57,7 @@ protected static Schema unionWithNull(Schema otherSchema)
return Schema.createUnion(schemas);
}

public static Schema simpleSchema(JsonFormatTypes type)
public static Schema simpleSchema(JsonFormatTypes type, JavaType hint)
{
switch (type) {
case BOOLEAN:
Expand All @@ -52,6 +67,13 @@ public static Schema simpleSchema(JsonFormatTypes type)
case NULL:
return Schema.create(Schema.Type.NULL);
case NUMBER:
// 16-Feb-2017, tatu: Fixed as suggested by `baharclerode@github`
if (hint.hasRawClass(float.class)) {
return Schema.create(Schema.Type.FLOAT);
}
if (hint.hasRawClass(long.class)) {
return Schema.create(Schema.Type.LONG);
}
return Schema.create(Schema.Type.DOUBLE);
case STRING:
return Schema.create(Schema.Type.STRING);
Expand All @@ -64,8 +86,7 @@ public static Schema simpleSchema(JsonFormatTypes type)
}
}

public static Schema numericAvroSchema(JsonParser.NumberType type)
{
public static Schema numericAvroSchema(JsonParser.NumberType type) {
switch (type) {
case INT:
return Schema.create(Schema.Type.INT);
Expand All @@ -78,8 +99,27 @@ public static Schema numericAvroSchema(JsonParser.NumberType type)
case DOUBLE:
return Schema.create(Schema.Type.DOUBLE);
default:
throw new IllegalStateException("Unrecognized number type: "+type);
}
throw new IllegalStateException("Unrecognized number type: "+type);
}

public static Schema numericAvroSchema(JsonParser.NumberType type, JavaType hint) {
Schema schema = numericAvroSchema(type);
if (hint != null) {
schema.addProp(AVRO_SCHEMA_PROP_CLASS, hint.toCanonical());
}
return schema;
}

/**
* Helper method for constructing type-tagged "native" Avro Schema instance.
*
* @since 2.8.7
*/
public static Schema typedSchema(Schema.Type nativeType, JavaType javaType) {
Schema schema = Schema.create(nativeType);
schema.addProp(AVRO_SCHEMA_PROP_CLASS, javaType.toCanonical());
return schema;
}

public static Schema anyNumberSchema()
Expand All @@ -90,7 +130,7 @@ public static Schema anyNumberSchema()
Schema.create(Schema.Type.DOUBLE)
));
}

protected static <T> T throwUnsupported() {
throw new UnsupportedOperationException("Format variation not supported");
}
Expand Down
Expand Up @@ -3,12 +3,21 @@
import org.apache.avro.Schema;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonIntegerFormatVisitor;
import com.fasterxml.jackson.dataformat.avro.AvroSchema;

public class IntegerVisitor extends JsonIntegerFormatVisitor.Base
implements SchemaBuilder
{
protected JsonParser.NumberType _type;
protected JavaType _hint;

public IntegerVisitor() {}

public IntegerVisitor(JavaType typeHint) {
_hint = typeHint;
}

@Override
public void numberType(JsonParser.NumberType type) {
Expand All @@ -20,6 +29,6 @@ public Schema builtAvroSchema() {
if (_type == null) {
throw new IllegalStateException("No number type indicated");
}
return AvroSchemaHelper.numericAvroSchema(_type);
return AvroSchemaHelper.numericAvroSchema(_type, _hint);
}
}
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
import com.fasterxml.jackson.dataformat.avro.AvroSchema;

public class VisitorFormatWrapperImpl
implements JsonFormatVisitorWrapper
Expand Down Expand Up @@ -90,17 +91,16 @@ public JsonMapFormatVisitor expectMapFormat(JavaType mapType) {
}

@Override
public JsonArrayFormatVisitor expectArrayFormat(JavaType convertedType) {
public JsonArrayFormatVisitor expectArrayFormat(final JavaType convertedType) {
// 22-Mar-2016, tatu: Actually we can detect byte[] quite easily here can't we?
if (convertedType.isArrayType()) {
JavaType vt = convertedType.getContentType();
if (vt.hasRawClass(Byte.TYPE)) {
_builder = new SchemaBuilder() {
@Override
public Schema builtAvroSchema() {
return Schema.create(Schema.Type.BYTES);
return AvroSchemaHelper.typedSchema(Schema.Type.BYTES, convertedType);
}

};
return null;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public JsonIntegerFormatVisitor expectIntegerFormat(JavaType type) {
_valueSchema = s;
return null;
}
IntegerVisitor v = new IntegerVisitor();
IntegerVisitor v = new IntegerVisitor(type);
_builder = v;
return v;
}
Expand Down
Expand Up @@ -37,7 +37,8 @@ public final boolean writeFieldName(String name)
@Override
public final AvroWriteContext createChildArrayContext() {
_verifyValueWrite();
AvroWriteContext child = new ArrayWriteContext(this, _generator, _createArray(_schema.getElementType()));
AvroWriteContext child = new ArrayWriteContext(this, _generator,
_createArray(_schema.getValueType()));
_data.put(_currentName, child.rawValue());
return child;
}
Expand Down

0 comments on commit 7ed5465

Please sign in to comment.