tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.14k stars 1.03k forks source link

Feature: Deterministic printing #1073

Open bes opened 5 years ago

bes commented 5 years ago

I would like to propose a feature that would help a lot when debugging using printing to log.

For a given Mappable, it would be nice if the toJSONString(prettyPrint: true) could get a new property deterministic: Bool, that way every time you print a Mappable you can be confident that all properties are printed in the same order.

The easiest way would just be to sort the keys alphabetically, for every Mappable and sub Mappable.

Thank you for considering my feature request.

SerggioC commented 4 years ago

It can be done easily, just enable this option to outside: https://developer.apple.com/documentation/foundation/jsonserialization/writingoptions/2888322-sortedkeys

I had to change the ObjecMapper source code to enable this option because hashing a json would never return the same value.