JSONEncoder has changed

Prior to Sonoma 14.0 and Xcode 15.0.1 my custom class was encoded in a consistent pattern that I exploited to append the file many thousands of times. Now, the encodings are of a seemingly random pattern that cannot be exploited to append. This may be a result of efforts to make the encoding more efficient or virtually any other reason.

My questions are:

  1. how do I revert to the previous JSONEncoder while using current OS & Xcode versions?, i.e, what files and in what locations do I restore?
  2. is it likely to be sustainable if I did so?

It has been previously noted that atomic files are not "append able" which is why I used JSON in the first place.

Any info or suggestion is appreciated. Thank you.

Accepted Reply

If you mean the order of keys is now different (and you aren’t using the sortedKeys output option) then see this thread for discussion of why that is.

how do I revert to the previous JSONEncoder while using current OS & Xcode versions?

You can’t. That class is built into the Foundation framework in the operating system.

But one workaround could be to try the old JSONSerialization class. It’s not great with Swift but (when I tried this a while ago) you may get the old key ordering behavior, at least for now.

(Or is your issue different from key ordering? It’s not clear from the original question.)

  • Hi, what if it was assumed the encoder would produce the same results all the time and was used it in an app to identify items that communicate with a backend. And we would like to reproduce exactly the same output? And using .sortedKeys does create consistent results within Xcode 15, however compared to the output of Xcode14, it is not the same.

    Is there a way to reproduce the previous encoding result?

    Thank you

  • More specifically, my class has a property of type Double, which is encoded after rounding to 3 fraction digits, on Xcode15 rounds to 3 franction digits, however it is not rounding all doubles to 3 fraction digits on Xcode14, therefore the base64EncodedString is giving a different string as identifier on different Xcodes. JSON sorting was another problem, but even though I sort it, the double doesn't decode to the same value.

Add a Comment

Replies

If you mean the order of keys is now different (and you aren’t using the sortedKeys output option) then see this thread for discussion of why that is.

how do I revert to the previous JSONEncoder while using current OS & Xcode versions?

You can’t. That class is built into the Foundation framework in the operating system.

But one workaround could be to try the old JSONSerialization class. It’s not great with Swift but (when I tried this a while ago) you may get the old key ordering behavior, at least for now.

(Or is your issue different from key ordering? It’s not clear from the original question.)

  • Hi, what if it was assumed the encoder would produce the same results all the time and was used it in an app to identify items that communicate with a backend. And we would like to reproduce exactly the same output? And using .sortedKeys does create consistent results within Xcode 15, however compared to the output of Xcode14, it is not the same.

    Is there a way to reproduce the previous encoding result?

    Thank you

  • More specifically, my class has a property of type Double, which is encoded after rounding to 3 fraction digits, on Xcode15 rounds to 3 franction digits, however it is not rounding all doubles to 3 fraction digits on Xcode14, therefore the base64EncodedString is giving a different string as identifier on different Xcodes. JSON sorting was another problem, but even though I sort it, the double doesn't decode to the same value.

Add a Comment

Ah, thank you! Yes, using .sortedKeys returned the desired behavior. It is curious that this must have been a default as evidenced by consistent formatting for over a year, that is, until now. Thanks again.