JSON parser should reject invalid values for Boolean properties instead of converting to false

Using Micronaut Serde, any string value other than "true" returns false, and any numeric value other than 0 returns true.

Is this desirable behavior or should I open an issue?

For example, reading string values ​​"", "True", "TRUE" or "enabled" from a JSON returns false, which can generate erroneous entries in an API.

Actual Behaviour

Comparing with Javascript behavior, if you run JSON.parse("True") or JSON.parse("TRUE") in the browser console, an exception will be thrown: SyntaxError: Unexpected token 'T', "True" is not valid JSON. According to the JSON specification, the expected values ​​are true or false, without quotes. If we wanted to strictly follow the specification, no string values ​​would be allowed.

import io.micronaut.serde.ObjectMapper
import io.micronaut.serde.annotation.Serdeable
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
class CurrentBehaviorOfBooleanSerdeSpec extends Specification {

    @Inject ObjectMapper objectMapper

    // All asserts in this method are passing
    void "test current behavior"() {
        expect:
        objectMapper.readValue('{"enabled": true}', Test).enabled == true
        objectMapper.readValue('{"enabled": "true"}', Test).enabled == true
        objectMapper.readValue('{"enabled": false}', Test).enabled == false
        objectMapper.readValue('{"enabled": "false"}', Test).enabled == false

        // If there is at least one uppercase letter, returns false, even if the value is true
        // It is acceptable because true or false as a string is not in the Json specification
        objectMapper.readValue('{"enabled": "True"}', Test).enabled == false
        objectMapper.readValue('{"enabled": "TRUE"}', Test).enabled == false

        // Any numeric value other than 0 returns true
        objectMapper.readValue('{"enabled": 0}', Test).enabled == false
        objectMapper.readValue('{"enabled": 1}', Test).enabled == true
        objectMapper.readValue('{"enabled": 99}', Test).enabled == true

        // Any string value other than true returns false
        objectMapper.readValue('{"enabled": ""}', Test).enabled == false
        objectMapper.readValue('{"enabled": " "}', Test).enabled == false
        objectMapper.readValue('{"enabled": "enabled"}', Test).enabled == false
    }

    @Serdeable
    static class Test {
        Boolean enabled
    }

}

Expected Behaviour

All commands in "test expected behavior with invalid values" should throw an exception instead of returning true or false.

import io.micronaut.serde.ObjectMapper
import io.micronaut.serde.annotation.Serdeable
import io.micronaut.serde.exceptions.InvalidPropertyFormatException
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
class ExpectedBehaviorOfBooleanSerdeSpec extends Specification {

    @Inject ObjectMapper objectMapper

    // All asserts in this method are passing
    void "test expected behavior with valid values"() {
        expect:
        objectMapper.readValue('{"enabled": true}', Test).enabled == true
        objectMapper.readValue('{"enabled": "true"}', Test).enabled == true
        objectMapper.readValue('{"enabled": false}', Test).enabled == false
        objectMapper.readValue('{"enabled": "false"}', Test).enabled == false

        // Could only accept numeric values 0 and 1
        objectMapper.readValue('{"enabled": 0}', Test).enabled == false
        objectMapper.readValue('{"enabled": 1}', Test).enabled == true
    }

    // All asserts in this method are failing
    void "test expected behavior with invalid values"() {
        when:
        // Any string value other than true or false could throw an exception, instead of returning false
        objectMapper.readValue('{"enabled": "True"}', Test).enabled
        objectMapper.readValue('{"enabled": "TRUE"}', Test).enabled
        objectMapper.readValue('{"enabled": "False"}', Test).enabled
        objectMapper.readValue('{"enabled": "FALSE"}', Test).enabled
        objectMapper.readValue('{"enabled": ""}', Test).enabled
        objectMapper.readValue('{"enabled": " "}', Test).enabled
        objectMapper.readValue('{"enabled": "enabled"}', Test).enabled

        // Any numeric value other than 0 and 1 could throw an exception, instead of returning true
        objectMapper.readValue('{"enabled": 99}', Test).enabled

        then:
        thrown(InvalidPropertyFormatException)
    }

    @Serdeable
    static class Test {
        Boolean enabled
    }

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật