Skip to content

Use IterationType in TypeUtil #599

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -96,7 +96,7 @@ public void serializeValue(JsonGenerator gen, Object value) throws IOException
rootName = _rootNameLookup.findRootName(cls, _config);
}
_initWithRootName(xgen, rootName);
asArray = TypeUtil.isIndexedType(cls);
asArray = TypeUtil.isIndexedType(_config.constructType(cls));
if (asArray) {
_startRootArray(xgen, rootName);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,
}
_initWithRootName(xgen, rootName);
asArray = (rootType == null)
? TypeUtil.isIndexedType(value.getClass())
? TypeUtil.isIndexedType(_config.constructType(value.getClass()))
: TypeUtil.isIndexedType(rootType);
if (asArray) {
_startRootArray(xgen, rootName);
Expand Down Expand Up @@ -204,7 +204,7 @@ public void serializePolymorphic(JsonGenerator gen, Object value, JavaType rootT
}
_initWithRootName(xgen, rootName);
asArray = (rootType == null)
? TypeUtil.isIndexedType(value.getClass())
? TypeUtil.isIndexedType(_config.constructType(value.getClass()))
: TypeUtil.isIndexedType(rootType);
if (asArray) {
_startRootArray(xgen, rootName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TypeUtil
public static boolean isIndexedType(JavaType type)
{
Class<?> cls = type.getRawClass();
if (type.isContainerType() || canHandleLikeAnIterable(cls)) {
if (type.isContainerType() || type.isIterationType()) {
// One special case; byte[] will be serialized as base64-encoded String, not real array, so:
// (actually, ditto for char[]; thought to be a String)
if (cls == byte[].class || cls == char[].class) {
Expand All @@ -30,20 +30,4 @@ public static boolean isIndexedType(JavaType type)
}
return false;
}

public static boolean isIndexedType(Class<?> cls)
{
return (cls.isArray() && cls != byte[].class && cls != char[].class)
|| Collection.class.isAssignableFrom(cls) || canHandleLikeAnIterable(cls);
}

/**
* See <a href="https://github.com/FasterXML/jackson-dataformat-xml/pull/597">related disccussions</a>
* for detailed history.
*
* @since 2.16
*/
private static boolean canHandleLikeAnIterable(Class<?> cls) {
return Iterator.class.isAssignableFrom(cls) || Stream.class.isAssignableFrom(cls);
}
}