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

JSONEncoder should allow custom key ordering behavior

    XMLWordPrintable

    Details

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

      Description

      I am working with a server that requires a certain key to be present first in an object ("for performance reasons" they said)...

      Unfortunately, implementing `encode(to` manually and encoding in a specific order does not end up having any effect on the key order of the resulting document.

      Discussions about key order not being a valid JSON concern aside (which I agree with) it would be nice to have functionality for determining the order of keys by providing a custom function, similar to how we handle custom date formatting logic.

      I'd love to have something like this:

       

      let encoder = JSONEncoder()
      encoder.outputFormatting = .customKeyOrdering { keys in
        // return a new array of sorted keys however I need
      
      }
      

       

       

      (And if Radar is a better place for this I'd be happy to submit it there instead.)

        Attachments

          Activity

            People

            Assignee:
            itaiferber Itai Ferber
            Reporter:
            subdigital Ben Scheirman
            Votes:
            3 Vote for this issue
            Watchers:
            7 Start watching this issue

              Dates

              Created:
              Updated: