-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Unable to serialize top-level Java8 Stream #302
Comments
Hmmh. Yes, so the work-around(s) used for It'd be nice to figure out something more general for iterable types (since they are not quite what |
Realized that support probably requires upgrade of JDK baseline for XML module to be Java 8 -- something that is probably reasonable for 2.11, but can not be done in a patch for 2.10. |
At this point Java 8 requirement is no longer blocker, fwtw. |
May I ask what the expected behavior is here? Serialization of both Iterable and Stream interfaces types just like how Collection works? |
@JooHyukKim I would expect that Streams should be treated similarly to an Iterable - like a Collection but ideally without pulling the full Iterable or Stream into memory at one time. |
@pjfanning Hmm, what I can think of, to acheive the goal here is to obtain an instance of By using iterator, we can
What do you think? |
Feels like the TypeUtil method should be changed to something like
Or it might be possible to get access to the I would suggest adding the broken test cases and seeing if the first approach above is enough to fix it. |
I did try exactly that with |
|
ModificationTypeUtil : public static boolean isIndexedType(Class<?> cls)
{
return (cls.isArray() && cls != byte[].class && cls != char[].class)
|| Collection.class.isAssignableFrom(cls) || Iterator.class.isAssignableFrom(cls) || Stream.class.isAssignableFrom(cls);
} Used Test case :
public void testElement() throws JsonProcessingException {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
assertEquals("<ArrayList><item>a</item><item>b</item></ArrayList>",
OBJECT_MAPPER.writeValueAsString(list));
}
public void testElem() throws JsonProcessingException {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
assertEquals("<Itr><item>a</item><item>b</item></Itr>",
OBJECT_MAPPER.writeValueAsString(list.iterator()));
}
public void testopLevelTwoElements() throws JsonProcessingException {
assertEquals("<Adapter><item>a</item><item>b</item></Adapter>",
OBJECT_MAPPER.writeValueAsString(Stream.of("a", "b").iterator()));
}
public void testTopLevelTwoElements() throws JsonProcessingException {
OBJECT_MAPPER.writeValueAsString(Stream.of("a", "b"));
} Direct Stream serialization Error message
Further works (if agreed, I will push through)
@pjfanning May I ask for you thoughts? |
use Iterable instead of Collection - Collection is a subclass of Iterable |
Did you register the Jdk8Module in your Stream test? |
@pjfanning no, just plain init. |
jackson needs Jdk8Module to support Streams - see https://github.com/FasterXML/jackson-modules-java8 |
Hmm, using |
@pjfanning Oh, thank you! 🙏🏼🙏🏼 I missed to consider backward compatibility and just simply thought it would all work since baseline jdk is raised to 8. |
I will try to tidy up everything that has come up in convos from this issue and #329, then try to come up with something. |
A
Stream
nested in another object is serialized using a value wrapper just as a collection or array, serializing aStream
directly fails when more than one element is available as it tries to write a second root. If the Stream emits only a single element serialization works but without wrapping:The root element name is
Head
for Stream serialization as it's determined from the actual class nameReferencePipeline$Head
.The cause lies in
TypeUtil.isIndexedType(Class<?>)
which just checks for Arrays and Collections and only if this returns true, a field name is written (and _nextName set).It works for the nested Stream as
_nextName
is set due to the Stream being written as a field (multiple times).The text was updated successfully, but these errors were encountered: