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

Iterable<T> is not serializable as a property #151

Closed
Whathecode opened this issue Jun 15, 2018 · 2 comments
Closed

Iterable<T> is not serializable as a property #151

Whathecode opened this issue Jun 15, 2018 · 2 comments

Comments

@Whathecode
Copy link
Contributor

Whathecode commented Jun 15, 2018

Is there a specific reason why Iterable<T> cannot be serialized when it is a property, but it can be when it is serialized individually? When the type is specified as List<T> instead of Iterable<T> serialization does succeed.

The following tests all pass on v0.5.1:

@Test
fun `can serialize Iterable`()
{
    val iterable: Iterable<Int> = listOf( 1 )
    val serialized = JSON.stringify( PolymorphicSerializer, iterable )
}

@Serializable
data class ContainsIterable( val list: Iterable<Int> )

@Test
fun `cannot serialize Iterable as a property`()
{
    val iterableList = ContainsIterable( listOf( 1 ) )
    assertFailsWith<NoSuchMethodError>
    {
        val serialized = JSON.stringify( iterableList )
    }
}

@Serializable
data class ContainsList( val list: List<Int> )

@Test
fun `can serialize List as a property`()
{
    val list = ContainsList( listOf( 1 ) )
    val serialized = JSON.stringify( list )
}
@sandwwraith
Copy link
Member

There is no specific serializer for Iterable. Your first test works because you've explicitly specified serializer for iterable as Polymorphic, you could do the same for property with annotation @Serializable(with=...).

Personally I think Iterable is a too broad interface to be serializable, because while it can be an usual list (and why don't use List instead), it also can be lazy and/or infinite stream of elements.

@Whathecode
Copy link
Contributor Author

Very good point, @sandwwraith. Thank you for explaining!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants