Skip to content

Commit

Permalink
minor clean up, prepare for @JsonAlias testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 24, 2017
1 parent 5285e4a commit e5e475f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ public ObjectMapper enableDefaultTypingAsProperty(DefaultTyping applicability, S
typer = typer.typeProperty(propertyName);
return setDefaultTyping(typer);
}

/**
* Method for disabling automatic inclusion of type information; if so, only
* explicitly annotated types (ones with
Expand Down Expand Up @@ -1611,7 +1611,7 @@ public ObjectMapper setTypeFactory(TypeFactory f)
_serializationConfig = _serializationConfig.with(f);
return this;
}

/**
* Convenience method for constructing {@link JavaType} out of given
* type (typically <code>java.lang.Class</code>), but without explicit
Expand All @@ -1620,7 +1620,7 @@ public ObjectMapper setTypeFactory(TypeFactory f)
public JavaType constructType(Type t) {
return _typeFactory.constructType(t);
}

/*
/**********************************************************
/* Configuration, deserialization
Expand All @@ -1640,7 +1640,7 @@ public JavaType constructType(Type t) {
public JsonNodeFactory getNodeFactory() {
return _deserializationConfig.getNodeFactory();
}

/**
* Method for specifying {@link JsonNodeFactory} to use for
* constructing root level tree nodes (via method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1346,8 +1346,8 @@ public Linked<T> trimByVisibility() {

@Override
public String toString() {
String msg = value.toString()+"[visible="+isVisible+",ignore="+isMarkedIgnored
+",explicitName="+isNameExplicit+"]";
String msg = String.format("%s[visible=%b,ignore=%b,explicitName=%b]",
value.toString(), isVisible, isMarkedIgnored, isNameExplicit);
if (next != null) {
msg = msg + ", "+next.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ public static class SimpleBean {
public int x, y;
}

static class AliasBean {
@JsonAlias({ "nm", "Name" })
public String name;

int _xyz;

int _a;

@JsonCreator
public AliasBean(@JsonProperty("a")
@JsonAlias("A") int a) {
_a = a;
}

@JsonAlias({ "Xyz" })
public void setXyz(int x) {
_xyz = x;
}
}

/*
/**********************************************************
/* Other helper classes
Expand Down Expand Up @@ -186,17 +206,16 @@ public void testIssue442PrivateUnwrapped() throws Exception
/* Test methods, annotations disabled
/**********************************************************
*/

public void testAnnotationsDisabled() throws Exception
{
// first: verify that annotation introspection is enabled by default
ObjectMapper m = new ObjectMapper();
assertTrue(m.getDeserializationConfig().isEnabled(MapperFeature.USE_ANNOTATIONS));
assertTrue(MAPPER.getDeserializationConfig().isEnabled(MapperFeature.USE_ANNOTATIONS));
// with annotations, property is renamed
AnnoBean bean = m.readValue("{ \"y\" : 0 }", AnnoBean.class);
AnnoBean bean = MAPPER.readValue("{ \"y\" : 0 }", AnnoBean.class);
assertEquals(0, bean.value);

m = new ObjectMapper();
ObjectMapper m = new ObjectMapper();
m.configure(MapperFeature.USE_ANNOTATIONS, false);
// without annotations, should default to default bean-based name...
bean = m.readValue("{ \"x\" : 0 }", AnnoBean.class);
Expand All @@ -222,4 +241,27 @@ public void testNoAccessOverrides() throws Exception
assertEquals(1, bean.x);
assertEquals(2, bean.y);
}

/*
public void testSimpleAliases() throws Exception
{
AliasBean bean = MAPPER.readValue(aposToQuotes("{'Name':'Foobar','a':3,'xyz':37}"),
AliasBean.class);
assertEquals("Foobar", bean.name);
assertEquals(3, bean._a);
assertEquals(37, bean._xyz);
bean = MAPPER.readValue(aposToQuotes("{'name':'Foobar','A':3,'xyz':37}"),
AliasBean.class);
assertEquals("Foobar", bean.name);
assertEquals(3, bean._a);
assertEquals(37, bean._xyz);
bean = MAPPER.readValue(aposToQuotes("{'name':'Foobar','a':3,'Xyz':37}"),
AliasBean.class);
assertEquals("Foobar", bean.name);
assertEquals(3, bean._a);
assertEquals(37, bean._xyz);
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testWithUnwrapping() throws Exception
}

// Alas: can't pass, until [databind#265] fixed:
/*
// 23-Feb-2017, tatu: or its follow-up: error message is now more descriptive...
public void testWithCreatorUnwrapping() throws Exception
{
final String json = aposToQuotes("{'loc.x':4,'name':'Foobar','loc.y': 7}}");
Expand All @@ -141,13 +141,22 @@ public void testWithCreatorUnwrapping() throws Exception
mapper.setInjectableValues(new InjectableValues.Std()
.addValue(String.class, "stuffValue")
);
UnwrappingCreatorValue result = mapper.readValue(json, UnwrappingCreatorValue.class);

@SuppressWarnings("unused")
UnwrappingCreatorValue result;
try {
result = mapper.readValue(json, UnwrappingCreatorValue.class);
fail("Did not expect to really pass -- should maybe update the test");
} catch (InvalidDefinitionException e) {
verifyException(e, "combination not yet supported");
}

/*
assertNotNull(result);
assertNotNull(result.location);
assertEquals("Foobar", result.name);
assertEquals(4, result.location.x);
assertEquals(7, result.location.y);
*/
}
*/
}

0 comments on commit e5e475f

Please sign in to comment.