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

Fails to parse empty sets or singleton sets #333

Open
dandroid126 opened this issue May 6, 2021 · 0 comments
Open

Fails to parse empty sets or singleton sets #333

dandroid126 opened this issue May 6, 2021 · 0 comments

Comments

@dandroid126
Copy link

After writing an object with a Set of size of 0 or 1 to a json string, this json string cannot be parsed. An exception is thrown. This case works with Lists, but fails with Sets.

Here is an example test case:

fun main() {
    val listTest1 = ListTest(arrayListOf("a", "b", "c"))
    println(listTest1)
    val listJson1 = Klaxon().toJsonString(listTest1)
    println(listJson1)
    val listTest2 = Klaxon().parse<ListTest>(listJson1)
    println(listTest2)
    assert(listTest1 == listTest2)

    val listTest3 = ListTest(arrayListOf("a"))
    println(listTest3)
    val listJson2 = Klaxon().toJsonString(listTest3)
    println(listJson2)
    val listTest4 = Klaxon().parse<ListTest>(listJson2)
    println(listTest4)
    assert(listTest3 == listTest4)

    val setTest1 = SetTest(hashSetOf("a", "b", "c"))
    println(setTest1)
    val setJson1 = Klaxon().toJsonString(setTest1)
    println(setJson1)
    val setTest2 = Klaxon().parse<SetTest>(setJson1)
    println(setTest2)
    assert(setTest1 == setTest2)

    val setTest3 = SetTest(hashSetOf("a"))
    println(setTest3)
    val setJson2 = Klaxon().toJsonString(setTest3)
    println(setJson2)
    val setTest4 = Klaxon().parse<SetTest>(setJson2)
    println(setTest4)
    assert(setTest3 == setTest4)
}

data class SetTest(val set: HashSet<String>) {
    override fun equals(other: Any?): Boolean {
        return other is SetTest && set == other.set
    }
}
data class ListTest(val list: ArrayList<String>) {
    override fun equals(other: Any?): Boolean {
        return other is ListTest && list == other.list
    }
}

In this case, the exception thrown is Parameter set: expected kotlin.collections.HashSet<kotlin.String> /* = java.util.HashSet<kotlin.String> */ but received java.util.Collections$SingletonSet (value: [a])

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

No branches or pull requests

1 participant