Skip to content

Adding Single Int Constructor Support (Issue #25) #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected POJODefinition introspectDefinition()
Constructor<?> defaultCtor = null;
Constructor<?> stringCtor = null;
Constructor<?> longCtor = null;
Constructor<?> intCtor = null;

// A few things only matter during deserialization: constructors,
// secondary ignoral information:
Expand All @@ -99,14 +100,16 @@ protected POJODefinition introspectDefinition()
stringCtor = ctor;
} else if (argType == Long.class || argType == Long.TYPE) {
longCtor = ctor;
} else if(argType == Integer.class || argType == Integer.TYPE) {
intCtor = ctor;
}
}
}
}

POJODefinition def = new POJODefinition(_type,
_pruneProperties(_forSerialization),
defaultCtor, stringCtor, longCtor);
defaultCtor, stringCtor, longCtor, intCtor);
if (_ignorableNames != null) {
def = def.withIgnorals(_ignorableNames);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private POJODefinition _construct(Class<?> beanType, int features)
Constructor<?> defaultCtor = null;
Constructor<?> stringCtor = null;
Constructor<?> longCtor = null;
Constructor<?> intCtor = null;

for (Constructor<?> ctor : beanType.getDeclaredConstructors()) {
Class<?>[] argTypes = ctor.getParameterTypes();
Expand All @@ -64,6 +65,8 @@ private POJODefinition _construct(Class<?> beanType, int features)
stringCtor = ctor;
} else if (argType == Long.class || argType == Long.TYPE) {
longCtor = ctor;
} else if (argType == Integer.class || argType == Integer.TYPE) {
intCtor = ctor;
}
}
}
Expand All @@ -78,7 +81,7 @@ private POJODefinition _construct(Class<?> beanType, int features)
props[i++] = builder.build();
}
}
return new POJODefinition(beanType, props, defaultCtor, stringCtor, longCtor);
return new POJODefinition(beanType, props, defaultCtor, stringCtor, longCtor, intCtor);
}

private static void _introspect(Class<?> currType, Map<String, PropBuilder> props,
Expand All @@ -94,8 +97,7 @@ private static void _introspect(Class<?> currType, Map<String, PropBuilder> prop
// then public fields (since 2.8); may or may not be ultimately included
// but at this point still possible
for (Field f : currType.getDeclaredFields()) {
if (!Modifier.isPublic(f.getModifiers())
|| f.isEnumConstant() || f.isSynthetic()) {
if (!Modifier.isPublic(f.getModifiers()) || f.isEnumConstant() || f.isSynthetic()) {
continue;
}
// Only include static members if (a) inclusion feature enabled and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ public class BeanReader
protected final Constructor<?> _defaultCtor;
protected final Constructor<?> _stringCtor;
protected final Constructor<?> _longCtor;
protected final Constructor<?> _intCtor;

/**
* Constructors used for deserialization use case
*/
public BeanReader(Class<?> type, Map<String, BeanPropertyReader> props,
Constructor<?> defaultCtor, Constructor<?> stringCtor, Constructor<?> longCtor,
Constructor<?> defaultCtor, Constructor<?> stringCtor, Constructor<?> longCtor, Constructor<?> intCtor,
Set<String> ignorableNames, Map<String, String> aliasMapping)
{
super(type);
_propsByName = props;
_defaultCtor = defaultCtor;
_stringCtor = stringCtor;
_longCtor = longCtor;
_intCtor = intCtor;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe propogation of HashMap<FIRST_ARG,CONSTRUCTOR> should be a better alternative, to support future items. instead of adding individually in all the following files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the general idea (could use EnumMap too), but how about going even more OO and have Constructors (or whatever its named) container that encapsulates these details?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using enum map, but that would require us explicitly manage a list of supported items, with the code getters and setters; a problem not present in HashMap.


if (ignorableNames == null) {
ignorableNames = Collections.<String>emptySet();
}
Expand All @@ -59,8 +62,8 @@ public BeanReader(Class<?> type, Map<String, BeanPropertyReader> props,

@Deprecated // since 2.11
public BeanReader(Class<?> type, Map<String, BeanPropertyReader> props,
Constructor<?> defaultCtor, Constructor<?> stringCtor, Constructor<?> longCtor) {
this(type, props, defaultCtor, stringCtor, longCtor, null, null);
Constructor<?> defaultCtor, Constructor<?> stringCtor, Constructor<?> longCtor,Constructor<?> intCtor) {
this(type, props, defaultCtor, stringCtor, longCtor,intCtor, null, null);
}

public Map<String,BeanPropertyReader> propertiesByName() { return _propsByName; }
Expand Down Expand Up @@ -191,10 +194,13 @@ protected Object create(String str) throws Exception {
}

protected Object create(long l) throws Exception {
if (_longCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have single-long constructor to use");
if (_longCtor != null) {
return _longCtor.newInstance(l);
} else if(_intCtor != null) {
return _intCtor.newInstance((int) l);
} else {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have single-long or single-int constructor to use");
}
return _longCtor.newInstance(l);
}

protected void handleUnknown(JSONReader reader, JsonParser parser, String fieldName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ public class POJODefinition
public final Constructor<?> defaultCtor;
public final Constructor<?> stringCtor;
public final Constructor<?> longCtor;
public final Constructor<?> intCtor;

public POJODefinition(Class<?> type, Prop[] props,
Constructor<?> defaultCtor0, Constructor<?> stringCtor0, Constructor<?> longCtor0)
Constructor<?> defaultCtor0, Constructor<?> stringCtor0, Constructor<?> longCtor0,Constructor<?> intCtor0)
{
_type = type;
_properties = props;
defaultCtor = defaultCtor0;
stringCtor = stringCtor0;
longCtor = longCtor0;
intCtor = intCtor0;
_ignorableNames = null;
}

Expand All @@ -49,6 +51,7 @@ protected POJODefinition(POJODefinition base,
defaultCtor = base.defaultCtor;
stringCtor = base.stringCtor;
longCtor = base.longCtor;
intCtor = base.intCtor;
_ignorableNames = ignorableN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
Constructor<?> defaultCtor = beanDef.defaultCtor;
Constructor<?> stringCtor = beanDef.stringCtor;
Constructor<?> longCtor = beanDef.longCtor;
Constructor<?> intCtor = beanDef.intCtor;

final boolean forceAccess = JSON.Feature.FORCE_REFLECTION_ACCESS.isEnabled(_features);
if (forceAccess) {
Expand Down Expand Up @@ -514,7 +515,7 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
}
}
}
return new BeanReader(raw, propMap, defaultCtor, stringCtor, longCtor,
return new BeanReader(raw, propMap, defaultCtor, stringCtor, longCtor,intCtor,
beanDef.getIgnorableNames(), aliasMapping);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// for [jackson-jr#25], allowing single-int constructors
public class ReadWithCtors25Test extends TestBase
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to move this to passing tests, once the implementation is approved

{
static class FromInt1 {
public static class FromInt1 {
protected int value;
public FromInt1(int v) { value = v; }
}

static class FromInt2 {
public static class FromInt2 {
protected int value;
public FromInt2(Integer v) { value = v.intValue(); }
}
Expand Down