Uploaded image for project: 'Swift'
  1. Swift
  2. SR-9279

JSONEncoder with dateEncodingStrategy of millisecondsSince1970 produces Float type in resulting JSON

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Foundation
    • Labels:

      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}

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              rkhalili Ryan Khalili
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: