cbeust/klaxon

Infinite floating point values cause invalid JSON to be written

ntfwc opened this issue · 0 comments

ntfwc commented

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.