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

JsonArrayBuilder's unaryPlus extensions don't work for Number? #418

Closed
jcayzac opened this issue Mar 26, 2019 · 4 comments
Closed

JsonArrayBuilder's unaryPlus extensions don't work for Number? #418

jcayzac opened this issue Mar 26, 2019 · 4 comments
Assignees
Labels

Comments

@jcayzac
Copy link

jcayzac commented Mar 26, 2019

Using kotlinx-serialization-runtime:0.10.0 the following code produces an empty array:

jsonArray {
    +2
    +4
    +6
    +8
}

Being more verbose works as expected:

jsonArray {
    +JsonPrimitive(2)
    +JsonPrimitive(4)
    +JsonPrimitive(6)
    +JsonPrimitive(8)
}

It seems that https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/common/src/test/kotlin/kotlinx/serialization/json/serializers/JsonArraySerializerTest.kt lacks tests for the the unaryPlus overloads defined in JsonArrayBuilder, maybe.

@jcayzac
Copy link
Author

jcayzac commented Mar 26, 2019

The overload for String? works, this returns an array of size 3:

jsonArray {
    +"foo"
    +"bar"
    +"baz"
}

@jcayzac
Copy link
Author

jcayzac commented Mar 26, 2019

I ended up using JsonArray(listOf(2, 4, 6, 8).map(::JsonPrimitive)) for now…

@q-litzler
Copy link
Contributor

q-litzler commented Mar 26, 2019

jsonArray {
  +(1 as Number)
}

This will work. The problem is that when you only write +1, you don't overload the correct unaryPlus operator. (You can command click on the + in your IDE, and you will end up in the sources of the stdlib instead of the kotlinx.serialization)

@toddobryan
Copy link

toddobryan commented Sep 7, 2019

Can we get some movement on this. I was just bitten by it. There's a solution in dev, but it hasn't been merged back into master. In the comments on the commit, add is the much less verbose choice, especially when you consider that you can do add(Number), add(String), add(Boolean), and add(null) for all the primitives, and add(JsonElement) for non-primitives. It's too bad that unaryPlus gets confused with Numbers, but it seems like add is the most concise and consistent way of doing this given that issue.

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

No branches or pull requests

5 participants