Skip to content

Commit

Permalink
Merge branch '2.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 10, 2020
2 parents 5b66193 + 831537b commit 7c8463f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.type.LogicalType;
import com.fasterxml.jackson.databind.util.ClassUtil;

/**
Expand Down Expand Up @@ -77,16 +78,18 @@ public static Class<?>[] types() {
TimeZone.class,
InetAddress.class,
InetSocketAddress.class,

// Special impl:
StringBuilder.class,
};
}

/*
/**********************************************************************
/* Deserializer implementations
/* Life-cycle
/**********************************************************************
*/

protected FromStringDeserializer(Class<?> vc) {
super(vc);
}
Expand All @@ -95,7 +98,7 @@ protected FromStringDeserializer(Class<?> vc) {
* Factory method for trying to find a deserializer for one of supported
* types that have simple from-String serialization.
*/
public static Std findDeserializer(Class<?> rawType)
public static FromStringDeserializer<?> findDeserializer(Class<?> rawType)
{
int kind = 0;
if (rawType == File.class) {
Expand Down Expand Up @@ -125,12 +128,17 @@ public static Std findDeserializer(Class<?> rawType)
} else if (rawType == InetSocketAddress.class) {
kind = Std.STD_INET_SOCKET_ADDRESS;
} else if (rawType == StringBuilder.class) {
kind = Std.STD_STRING_BUILDER;
return new StringBuilderDeserializer();
} else {
return null;
}
return new Std(rawType, kind);
}

@Override // since 2.12
public LogicalType logicalType() {
return LogicalType.OtherScalar;
}

/*
/**********************************************************************
Expand Down Expand Up @@ -260,10 +268,11 @@ public static class Std extends FromStringDeserializer<Object>
public final static int STD_TIME_ZONE = 11;
public final static int STD_INET_ADDRESS = 12;
public final static int STD_INET_SOCKET_ADDRESS = 13;
public final static int STD_STRING_BUILDER = 14;
// No longer implemented here since 2.12
// public final static int STD_STRING_BUILDER = 14;

protected final int _kind;

protected Std(Class<?> valueType, int kind) {
super(valueType);
_kind = kind;
Expand Down Expand Up @@ -341,8 +350,6 @@ protected Object _deserialize(String value, DeserializationContext ctxt) throws
}
// host or unbracketed IPv6, without port number
return new InetSocketAddress(value, 0);
case STD_STRING_BUILDER:
return new StringBuilder(value);
}
VersionUtil.throwInternal();
return null;
Expand All @@ -359,8 +366,6 @@ public Object getEmptyValue(DeserializationContext ctxt)
case STD_LOCALE:
// As per [databind#1123], Locale too
return Locale.ROOT;
case STD_STRING_BUILDER:
return new StringBuilder();
}
return super.getEmptyValue(ctxt);
}
Expand All @@ -385,6 +390,44 @@ protected int _firstHyphenOrUnderscore(String str)
}
}

// @since 2.12 to simplify logic a bit: should not use coercions when reading
// String Values
static class StringBuilderDeserializer extends FromStringDeserializer<Object>
{
public StringBuilderDeserializer() {
super(StringBuilder.class);
}

@Override
public LogicalType logicalType() {
return LogicalType.Textual;
}

@Override
public Object getEmptyValue(DeserializationContext ctxt)
throws JsonMappingException
{
return new StringBuilder();
}

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
String text = p.getValueAsString();
if (text != null) {
return _deserialize(text, ctxt);
}
return super.deserialize(p, ctxt);
}

@Override
protected Object _deserialize(String value, DeserializationContext ctxt)
throws IOException
{
return new StringBuilder(value);
}
}

private static class NioPathHelper {
private static final boolean areWindowsFilePathsSupported;
static {
Expand Down Expand Up @@ -437,6 +480,6 @@ public static Path deserialize(DeserializationContext ctxt, String value) throws
} catch (Throwable e) {
return (Path) ctxt.handleInstantiationProblem(Path.class, value, e);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,12 @@ public abstract class StdScalarDeserializer<T> extends StdDeserializer<T>
protected StdScalarDeserializer(JavaType valueType) { super(valueType); }

protected StdScalarDeserializer(StdScalarDeserializer<?> src) { super(src); }

@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return typeDeserializer.deserializeTypedFromScalar(p, ctxt);
}

/**
* Overridden to simply call <code>deserialize()</code> method that does not take value
* to update, since scalar values are usually non-mergeable.
/*
/**********************************************************************
/* Overridden accessors
/**********************************************************************
*/
@Override // since 2.9
public T deserialize(JsonParser p, DeserializationContext ctxt, T intoValue) throws IOException {
// 25-Oct-2016, tatu: And if attempt is made, see if we are to complain...
ctxt.handleBadMerge(this);
// if that does not report an exception we can just delegate
return deserialize(p, ctxt);
}

@Override // since 2.12
public LogicalType logicalType() {
Expand Down Expand Up @@ -62,4 +51,27 @@ public AccessPattern getNullAccessPattern() {
public AccessPattern getEmptyAccessPattern() {
return AccessPattern.CONSTANT;
}

/*
/**********************************************************************
/* Default deserialization method impls
/**********************************************************************
*/

@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return typeDeserializer.deserializeTypedFromScalar(p, ctxt);
}

/**
* Overridden to simply call <code>deserialize()</code> method that does not take value
* to update, since scalar values are usually non-mergeable.
*/
@Override // since 2.9
public T deserialize(JsonParser p, DeserializationContext ctxt, T intoValue) throws IOException {
// 25-Oct-2016, tatu: And if attempt is made, see if we are to complain...
ctxt.handleBadMerge(this);
// if that does not report an exception we can just delegate
return deserialize(p, ctxt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ public void testScalarDefaultsFromEmpty() throws Exception
_testScalarEmptyToNull(DEFAULT_MAPPER, TimeZone.class);
_testScalarEmptyToNull(DEFAULT_MAPPER, InetAddress.class);
_testScalarEmptyToNull(DEFAULT_MAPPER, InetSocketAddress.class);

{
StringBuilder result = DEFAULT_MAPPER.readValue(JSON_EMPTY, StringBuilder.class);
assertNotNull(result);
assertEquals(0, result.length());
}
}

/*
Expand All @@ -105,8 +99,6 @@ public void testScalarEmptyToNull() throws Exception
_testScalarEmptyToNull(MAPPER_EMPTY_TO_NULL, TimeZone.class);
_testScalarEmptyToNull(MAPPER_EMPTY_TO_NULL, InetAddress.class);
_testScalarEmptyToNull(MAPPER_EMPTY_TO_NULL, InetSocketAddress.class);

_testScalarEmptyToNull(MAPPER_EMPTY_TO_NULL, StringBuilder.class);
}

public void testScalarEmptyToEmpty() throws Exception
Expand All @@ -132,12 +124,6 @@ public void testScalarEmptyToEmpty() throws Exception
_testScalarEmptyToNull(MAPPER_EMPTY_TO_EMPTY, TimeZone.class);
_testScalarEmptyToNull(MAPPER_EMPTY_TO_EMPTY, InetAddress.class);
_testScalarEmptyToNull(MAPPER_EMPTY_TO_EMPTY, InetSocketAddress.class);

{
StringBuilder result = MAPPER_EMPTY_TO_EMPTY.readValue(JSON_EMPTY, StringBuilder.class);
assertNotNull(result);
assertEquals(0, result.length());
}
}

public void testScalarEmptyToTryConvert() throws Exception
Expand All @@ -163,12 +149,6 @@ public void testScalarEmptyToTryConvert() throws Exception
_testScalarEmptyToNull(MAPPER_EMPTY_TO_TRY_CONVERT, TimeZone.class);
_testScalarEmptyToNull(MAPPER_EMPTY_TO_TRY_CONVERT, InetAddress.class);
_testScalarEmptyToNull(MAPPER_EMPTY_TO_TRY_CONVERT, InetSocketAddress.class);

{
StringBuilder result = MAPPER_EMPTY_TO_TRY_CONVERT.readValue(JSON_EMPTY, StringBuilder.class);
assertNotNull(result);
assertEquals(0, result.length());
}
}

/*
Expand All @@ -180,14 +160,51 @@ public void testScalarEmptyToTryConvert() throws Exception
public void testScalarsFailFromEmpty() throws Exception
{
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, UUID.class);

_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, File.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, URL.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, URI.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, Class.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, JavaType.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, Currency.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, Pattern.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, Locale.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, Charset.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, TimeZone.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, InetAddress.class);
_verifyScalarToFail(MAPPER_EMPTY_TO_FAIL, InetSocketAddress.class);
}

/*
/********************************************************
/* Test methods, special type(s)
/********************************************************
*/

// StringBuilder is its own special type, since it naturally maps
// from String values, hence separate testing
public void testStringBuilderDeser() throws Exception
{
// should result in an "empty" StringBuilder for all valid settings
_checkEmptyStringBuilder(DEFAULT_MAPPER.readValue(JSON_EMPTY, StringBuilder.class));
_checkEmptyStringBuilder(MAPPER_EMPTY_TO_EMPTY.readValue(JSON_EMPTY, StringBuilder.class));
_checkEmptyStringBuilder(MAPPER_EMPTY_TO_TRY_CONVERT.readValue(JSON_EMPTY, StringBuilder.class));
_checkEmptyStringBuilder(MAPPER_EMPTY_TO_NULL.readValue(JSON_EMPTY, StringBuilder.class));
// and even alleged failure should not result in that since it's not coercion
_checkEmptyStringBuilder(MAPPER_EMPTY_TO_FAIL.readValue(JSON_EMPTY, StringBuilder.class));
}

private void _checkEmptyStringBuilder(StringBuilder sb) {
assertNotNull(sb);
assertEquals(0, sb.length());
}

/*
/**********************************************************************
/* Second-level test helper methods
/**********************************************************************
*/

private void _testScalarEmptyToNull(ObjectMapper mapper, Class<?> target) throws Exception
{
assertNull(mapper.readerFor(target).readValue(JSON_EMPTY));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.databind.struct;
package com.fasterxml.jackson.databind.convert;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down
Loading

0 comments on commit 7c8463f

Please sign in to comment.