I ve found a serious Bug in JSONEncoder (SR-6131) and would like to fix it.

Dear all

I've found a serious Bug in JSONEncoder SR-6131 <Issues · apple/swift-issues · GitHub; and would like to fix it.
This bug cause JSON encoding issues on Double when using a local that does not use dot as decimal separator e.g « fr_FR » (we use a coma)

Demonstration of the issue :

import Foundation

// Required to call setLocale
import Darwin

// Let's set to french
setlocale(LC_ALL,"fr_FR")

struct Shot:Codable{
    let seconds:Double
}

let shot = Shot(seconds: 1.1)

do{
    let data = try JSONEncoder().encode(shot)
    if let json = String(data:data, encoding:.utf8){
        // the result is : "{"seconds":1,1000000000000001}"
        // The decimal separator should not be "," but "."
        print(json)
    }
}catch{
    print("\(error)")
}

exit(EX_OK)

I would like to propose my fix, test it and, but i’m not mastering the contribution mechanism.

Where should i start?

I ve forked GitHub - benoit-pereira-da-silva/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence
I have a very simple Unit test that demonstrates the issue.

How can i test my

Thanks

Benoit Pereira da Silva
Ultra Mobile Developer & Movement Activist
Développeur Ultra Mobile & Militant du mouvement
https://pereira-da-silva.com <https://pereira-da-silva.com/&gt;

✄ --------------------------------
This e-mail is confidential. Distribution, copy, publication or use of this information for any purpose is prohibited without agreement of the sender.
Ce message est confidentiel. Toute distribution, copie, publication ou usage des informations contenues dans ce message sont interdits sans agrément préalable de l'expéditeur.

(moving to more relevant list)

···

On Dec 18, 2017, at 08:51, Benoit Pereira da silva via swift-users <swift-users@swift.org> wrote:

Dear all

I've found a serious Bug in JSONEncoder SR-6131 <Issues · apple/swift-issues · GitHub; and would like to fix it.
This bug cause JSON encoding issues on Double when using a local that does not use dot as decimal separator e.g « fr_FR » (we use a coma)

Demonstration of the issue :

import Foundation

// Required to call setLocale
import Darwin

// Let's set to french
setlocale(LC_ALL,"fr_FR")

struct Shot:Codable{
    let seconds:Double
}

let shot = Shot(seconds: 1.1)

do{
    let data = try JSONEncoder().encode(shot)
    if let json = String(data:data, encoding:.utf8){
        // the result is : "{"seconds":1,1000000000000001}"
        // The decimal separator should not be "," but "."
        print(json)
    }
}catch{
    print("\(error)")
}

exit(EX_OK)

I would like to propose my fix, test it and, but i’m not mastering the contribution mechanism.

Where should i start?

I ve forked GitHub - benoit-pereira-da-silva/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence
I have a very simple Unit test that demonstrates the issue.

How can i test my

Thanks

Benoit Pereira da Silva
Ultra Mobile Developer & Movement Activist
Développeur Ultra Mobile & Militant du mouvement
https://pereira-da-silva.com <https://pereira-da-silva.com/&gt;

<bannerp.jpg>

✄ --------------------------------
This e-mail is confidential. Distribution, copy, publication or use of this information for any purpose is prohibited without agreement of the sender.
Ce message est confidentiel. Toute distribution, copie, publication ou usage des informations contenues dans ce message sont interdits sans agrément préalable de l'expéditeur.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I’m pretty sure JSON isn’t localized, so this isn’t a bug. Using anything but a . as a decimal separator isn’t valid JSON.

Jon

···

On Dec 18, 2017, at 1:04 PM, Jordan Rose via swift-users <swift-users@swift.org> wrote:

(moving to more relevant list)

On Dec 18, 2017, at 08:51, Benoit Pereira da silva via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Dear all

I've found a serious Bug in JSONEncoder SR-6131 <Issues · apple/swift-issues · GitHub; and would like to fix it.
This bug cause JSON encoding issues on Double when using a local that does not use dot as decimal separator e.g « fr_FR » (we use a coma)

Demonstration of the issue :

import Foundation

// Required to call setLocale
import Darwin

// Let's set to french
setlocale(LC_ALL,"fr_FR")

struct Shot:Codable{
    let seconds:Double
}

let shot = Shot(seconds: 1.1)

do{
    let data = try JSONEncoder().encode(shot)
    if let json = String(data:data, encoding:.utf8){
        // the result is : "{"seconds":1,1000000000000001}"
        // The decimal separator should not be "," but "."
        print(json)
    }
}catch{
    print("\(error)")
}

exit(EX_OK)

I would like to propose my fix, test it and, but i’m not mastering the contribution mechanism.

Where should i start?

I ve forked GitHub - benoit-pereira-da-silva/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence
I have a very simple Unit test that demonstrates the issue.

How can i test my

Thanks

Benoit Pereira da Silva
Ultra Mobile Developer & Movement Activist
Développeur Ultra Mobile & Militant du mouvement
https://pereira-da-silva.com <https://pereira-da-silva.com/&gt;

<bannerp.jpg>

✄ --------------------------------
This e-mail is confidential. Distribution, copy, publication or use of this information for any purpose is prohibited without agreement of the sender.
Ce message est confidentiel. Toute distribution, copie, publication ou usage des informations contenues dans ce message sont interdits sans agrément préalable de l'expéditeur.

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

I’m pretty sure JSON isn’t localized, so this isn’t a bug. Using anything but a . as a decimal separator isn’t valid JSON.

Jon

···

On Dec 18, 2017, at 11:51 AM, Benoit Pereira da silva via swift-users <swift-users@swift.org> wrote:

Dear all

I've found a serious Bug in JSONEncoder SR-6131 <Issues · apple/swift-issues · GitHub; and would like to fix it.
This bug cause JSON encoding issues on Double when using a local that does not use dot as decimal separator e.g « fr_FR » (we use a coma)

Demonstration of the issue :

import Foundation

// Required to call setLocale
import Darwin

// Let's set to french
setlocale(LC_ALL,"fr_FR")

struct Shot:Codable{
    let seconds:Double
}

let shot = Shot(seconds: 1.1)

do{
    let data = try JSONEncoder().encode(shot)
    if let json = String(data:data, encoding:.utf8){
        // the result is : "{"seconds":1,1000000000000001}"
        // The decimal separator should not be "," but "."
        print(json)
    }
}catch{
    print("\(error)")
}

exit(EX_OK)

I would like to propose my fix, test it and, but i’m not mastering the contribution mechanism.

Where should i start?

I ve forked GitHub - benoit-pereira-da-silva/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence
I have a very simple Unit test that demonstrates the issue.

How can i test my

Thanks

Benoit Pereira da Silva
Ultra Mobile Developer & Movement Activist
Développeur Ultra Mobile & Militant du mouvement
https://pereira-da-silva.com <https://pereira-da-silva.com/&gt;

<bannerp.jpg>

✄ --------------------------------
This e-mail is confidential. Distribution, copy, publication or use of this information for any purpose is prohibited without agreement of the sender.
Ce message est confidentiel. Toute distribution, copie, publication ou usage des informations contenues dans ce message sont interdits sans agrément préalable de l'expéditeur.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Jon is correct: JSON uses '.' as the decimal separator.
Benoit is also correct: the bug is that JSONEncoder is incorrectly writing ',' as the decimal separator when the locale is set to fr_FR.

Further discussion will be on the swift-corelibs-dev list.

···

On Dec 18, 2017, at 10:08 AM, Jon Shier via swift-users <swift-users@swift.org> wrote:

I’m pretty sure JSON isn’t localized, so this isn’t a bug. Using anything but a . as a decimal separator isn’t valid JSON.

Jon

On Dec 18, 2017, at 11:51 AM, Benoit Pereira da silva via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Dear all

I've found a serious Bug in JSONEncoder SR-6131 <Issues · apple/swift-issues · GitHub; and would like to fix it.
This bug cause JSON encoding issues on Double when using a local that does not use dot as decimal separator e.g « fr_FR » (we use a coma)

Demonstration of the issue :

import Foundation

// Required to call setLocale
import Darwin

// Let's set to french
setlocale(LC_ALL,"fr_FR")

struct Shot:Codable{
    let seconds:Double
}

let shot = Shot(seconds: 1.1)

do{
    let data = try JSONEncoder().encode(shot)
    if let json = String(data:data, encoding:.utf8){
        // the result is : "{"seconds":1,1000000000000001}"
        // The decimal separator should not be "," but "."
        print(json)
    }
}catch{
    print("\(error)")
}

exit(EX_OK)

I would like to propose my fix, test it and, but i’m not mastering the contribution mechanism.

Where should i start?

I ve forked GitHub - benoit-pereira-da-silva/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence
I have a very simple Unit test that demonstrates the issue.

How can i test my

Thanks

Benoit Pereira da Silva
Ultra Mobile Developer & Movement Activist
Développeur Ultra Mobile & Militant du mouvement
https://pereira-da-silva.com <https://pereira-da-silva.com/&gt;

<bannerp.jpg>

✄ --------------------------------
This e-mail is confidential. Distribution, copy, publication or use of this information for any purpose is prohibited without agreement of the sender.
Ce message est confidentiel. Toute distribution, copie, publication ou usage des informations contenues dans ce message sont interdits sans agrément préalable de l'expéditeur.

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

I’m pretty sure JSON isn’t localized, so this isn’t a bug. Using anything but a . as a decimal separator isn’t valid JSON.

JSONEncoder seems to be producing that content, so this does look like a Swift library issue.

Benoit: if you have a patch that fixes the issue, feel free to submit it as a GitHub pull request (*) against master and we’ll discuss it (and kick off testing) from there. I also opened <Issues · apple/swift-issues · GitHub; so that it isn’t forgotten. ✾

(*) Creating a pull request - GitHub Docs

···

On Dec 19, 2017, at 10:27 AM, Jon Shier via swift-corelibs-dev <swift-corelibs-dev@swift.org> wrote:

Jon

On Dec 18, 2017, at 1:04 PM, Jordan Rose via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

(moving to more relevant list)

On Dec 18, 2017, at 08:51, Benoit Pereira da silva via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Dear all

I've found a serious Bug in JSONEncoder SR-6131 <Issues · apple/swift-issues · GitHub; and would like to fix it.
This bug cause JSON encoding issues on Double when using a local that does not use dot as decimal separator e.g « fr_FR » (we use a coma)

Demonstration of the issue :

import Foundation

// Required to call setLocale
import Darwin

// Let's set to french
setlocale(LC_ALL,"fr_FR")

struct Shot:Codable{
    let seconds:Double
}

let shot = Shot(seconds: 1.1)

do{
    let data = try JSONEncoder().encode(shot)
    if let json = String(data:data, encoding:.utf8){
        // the result is : "{"seconds":1,1000000000000001}"
        // The decimal separator should not be "," but "."
        print(json)
    }
}catch{
    print("\(error)")
}

exit(EX_OK)

I would like to propose my fix, test it and, but i’m not mastering the contribution mechanism.

Where should i start?

I ve forked GitHub - benoit-pereira-da-silva/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence
I have a very simple Unit test that demonstrates the issue.

How can i test my

Thanks

Benoit Pereira da Silva
Ultra Mobile Developer & Movement Activist
Développeur Ultra Mobile & Militant du mouvement
https://pereira-da-silva.com <https://pereira-da-silva.com/&gt;

<bannerp.jpg>

✄ --------------------------------
This e-mail is confidential. Distribution, copy, publication or use of this information for any purpose is prohibited without agreement of the sender.
Ce message est confidentiel. Toute distribution, copie, publication ou usage des informations contenues dans ce message sont interdits sans agrément préalable de l'expéditeur.

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org <mailto:swift-corelibs-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev