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

Infinite floating point values cause invalid JSON to be written #335

Open
ntfwc opened this issue Jun 15, 2021 · 0 comments
Open

Infinite floating point values cause invalid JSON to be written #335

ntfwc opened this issue Jun 15, 2021 · 0 comments

Comments

@ntfwc
Copy link

ntfwc commented Jun 15, 2021

Version: 5.5

If you try to write infinite float or double values in an object, the serializer ends up writing an unquoted "Infinity".

A test that demonstrates this:

import com.beust.klaxon.Parser
import com.beust.klaxon.json
import java.io.StringReader
import kotlin.test.Test

class AppTest {
    @Test
    fun testInfinity() {
        val jsonString = json {
            obj(Pair("val1", Float.POSITIVE_INFINITY))
        }.toJsonString()

        println("jsonString=$jsonString")
        println(Parser.default().parse(StringReader(jsonString)))
    }
}

This outputs
jsonString={"val1":Infinity}
followed by the exception
com.beust.klaxon.KlaxonException: Unexpected character at position 8: 'I' (ASCII: 73)

Note: For a negative infinity the result is
jsonString={"val1":-Infinity}

I would not expect it to ever write invalid JSON. I believe it should, by default, either write a quoted "Infinity" or throw an exception.

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