Skip to content

[SR-9279] JSONEncoder with dateEncodingStrategy of millisecondsSince1970 produces Float type in resulting JSON #3587

Open
@swift-ci

Description

@swift-ci
Previous ID SR-9279
Radar None
Original Reporter rkhalili (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Codable
Assignee None
Priority Medium

md5: 26375280f8cb81c834f177bb618146aa

Issue Description:

When JSON-encoding a dictionary with a Date object, and specifying millisecondsSince1970 for the date encoding strategy, the resulting encoded value for the Date object is a floating point number with some amount of decimal places, instead of an integer representing milliseconds. This doesn't seem correct, I feel like this can cause incompatibility with systems or APIs that don't expect floats for millisecond values (which was the case for my company's Java back-end API).

To reproduce:

let test = ["key": Date()]
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .millisecondsSince1970
if let result = try? encoder.encode(test) {
    if let backToString = String(data: result, encoding: .utf8) {
        print("JSON: " + backToString)
    }
}

Expected output:

JSON: {"key":1542355551611}

Actual output:

JSON: {"key":1542355551611.2129}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions