You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@tayloraswift I looked into solutions for this issue again, then realized that I should use JSONDecodable instead of JSONObjectDecodable. I just assumed that the later would be preferred for objects & wouldn't be any more constrained than the former.
My app obtains JSON in a Foundation.Data from a network call.
I want to eventually output that JSON in a String, but with various object keys mapped to new values, while passing through all other keys as is.
I'm looking at swift-json because I want to support duplicate keys in the same object (which isn't supported by Foundation JSON processing, IIUC).
I don't expect to receive duplicate keys, but there is no formal spec for the JSON, and I am not in control of the server that provides the JSON.
I want to sort the keys after I've mapped them (preferably like localizedStandardCompare), but I want duplicate keys to remain in their original order relative to one another.
JSON.ObjectDecoder seems to expose only var index: [CodingKey: JSON.Node], which cannot support duplicate keys.
How should I decode supporting duplicate keys?
I assume that, if I decode use a [(JSON.Key, JSON.Node)] interface, JSON encoding will honor the order of the elements in the array (so I can affect the output by sorting the array).
I would presumably decode in an init(…) for a struct, as I need to extract a few properties from the JSON to determine what to do with various JSON & to output specific information about the JSON in some other places in my app. The init(…) would save the given JSON as a private let json: String property of the struct, then use the json property's value for the struct's var description: String, so that my app can output the JSON with mapped keys.
Any help will be appreciated.
Thanks for the library.
PS: it might be useful to list bullet points in the README.md providing reasons to use swift-json, including:
Pure Swift (i.e. does not require Foundation or any other frameworks)
High performance
High throughput
Supports duplicate JSON object keys
Fast compilation
Low file size cost
≤Such> style interface instead of Foundation's <other> style interface
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
@tayloraswift I looked into solutions for this issue again, then realized that I should use
JSONDecodableinstead ofJSONObjectDecodable. I just assumed that the later would be preferred for objects & wouldn't be any more constrained than the former.My app obtains JSON in a
Foundation.Datafrom a network call.I want to eventually output that JSON in a
String, but with various object keys mapped to new values, while passing through all other keys as is.I'm looking at swift-json because I want to support duplicate keys in the same object (which isn't supported by Foundation JSON processing, IIUC).
I don't expect to receive duplicate keys, but there is no formal spec for the JSON, and I am not in control of the server that provides the JSON.
I want to sort the keys after I've mapped them (preferably like
localizedStandardCompare), but I want duplicate keys to remain in their original order relative to one another.JSON.ObjectDecoderseems to expose onlyvar index: [CodingKey: JSON.Node], which cannot support duplicate keys.How should I decode supporting duplicate keys?
I assume that, if I decode use a
[(JSON.Key, JSON.Node)]interface, JSON encoding will honor the order of the elements in the array (so I can affect the output by sorting the array).I would presumably decode in an
init(…)for a struct, as I need to extract a few properties from the JSON to determine what to do with various JSON & to output specific information about the JSON in some other places in my app. Theinit(…)would save the given JSON as aprivate let json: Stringproperty of the struct, then use thejsonproperty's value for the struct'svar description: String, so that my app can output the JSON with mapped keys.Any help will be appreciated.
Thanks for the library.
PS: it might be useful to list bullet points in the
README.mdproviding reasons to use swift-json, including:Beta Was this translation helpful? Give feedback.
All reactions