Skip to content
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

Issue 368: serialize Sequence as Iterable #369

Conversation

juan-palacios
Copy link

@juan-palacios juan-palacios commented Sep 11, 2020

Translating the Sequence into an Iterable and using the IterableSerializer. This will:

  • Include handling for WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED
  • Iterate over the elements in the Sequence and serialize them one by one

As explained on Issue 368, if WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED is enabled the first value of the Sequence will be retrieve twice, which means the Sequence will have to produce the first element twice.

@dinomite dinomite added this to the 2.12.0 milestone Oct 20, 2020
@dinomite dinomite added the cla-needed PR looks good (although may also require code review), but CLA needed from submitter label Oct 20, 2020
@dinomite
Copy link
Member

@juan-palacios Will review soon—would you fill out the CLA and email a scan/photo of the result to info at fasterxml dot com?

https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf

Copy link
Member

@dinomite dinomite left a comment

Choose a reason for hiding this comment

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

Looks good to me. If you're feeling interested, you could try to do this against the 2.12 branch. The findTypedValueSerializer() requires a BeanProperty argument, so I'm not sure whether it would be a simple chang.

@cowtowncoder
Copy link
Member

Couple of notes: first, use of IterableSerializer for Sequence should work fine, I think. But there are some aspects of achieving this that can get tricky, esp. regarding handling of more complex included values.

First of all, lookup should occur earlier than from serialize(), both from correctness aspect (typing of contents not available any more) and from performance (there is lookup overhead).
There are couple of places where this can be done:

  1. When locating serializer, from KotlinSerializers.kt. Has JavaType available, with generic type variables resolved (if available from property definition)
  2. From createContextual() method (need to declare as ContextualSerializer): can locate similarly to proposed code, although need to hold on the original type serializer was constructed with. One specific benefit here is that BeanProperty is available, allowing proper support of annotations on property that may affect serialization.

Second problem is use of findTypedValueSerializer(): this is usually meant to be used for root values only.

To find out issues it may be necessary to add tests that exercise values like POGOs, and especially polymorphic values.

@dinomite dinomite modified the milestones: 2.12.0, 2.12.1 Dec 1, 2020
@dinomite dinomite self-requested a review December 1, 2020 11:13
@dinomite
Copy link
Member

dinomite commented Dec 1, 2020

I think this could use more work in light of @cowtowncoder's comments

@juan-palacios
Copy link
Author

I've been on leave but I intend to look into @cowtowncoder 's comments

dinomite added a commit that referenced this pull request Dec 11, 2020
@dinomite
Copy link
Member

@juan-palacios Any update?

@dinomite dinomite modified the milestones: 2.12.1, 2.12.2 Jan 11, 2021
@dinomite dinomite modified the milestones: 2.12.2, 2.13.0 May 13, 2021
@thecoden
Copy link

Due to item copies and specialized sequences we use, we overrode the default serializer using the followin serialize method:

	override fun serialize(value: Sequence<*>, gen: JsonGenerator, provider: SerializerProvider) {
		gen.writeStartArray()

		value.forEach {
			provider.defaultSerializeValue(it, gen)
		}

		gen.writeEndArray()
	}

We tested this by replacing that with the following...note that we had to add a null for the property on findTypedValueSerializer for Jackson 2.13.1

	override fun serialize(value: Sequence<*>, gen: JsonGenerator, provider: SerializerProvider) {
		provider.findTypedValueSerializer(
			Iterable::class.java,
			true,
			null
		).serialize(
			value.asIterable(),
			gen,
			provider
		)
	}

We are looking forward to seeing better Sequence handling in Kotlin Jackson Module. We do see the benefit of using the iterable serializer to get the handling of unwrapped single values, etc. Is there any way others like us can support getting this into a release beyond the testing I described?

@k163377
Copy link
Contributor

k163377 commented Jul 8, 2023

I noticed I had implemented a similar idea in #675.
This PR is closed.

@k163377 k163377 closed this Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting updates cla-needed PR looks good (although may also require code review), but CLA needed from submitter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants