lucianomarisi / jsonutilities Goto Github PK
View Code? Open in Web Editor NEWEasily load JSON objects and decode them into structs or classes
License: MIT License
Easily load JSON objects and decode them into structs or classes
License: MIT License
Since now generic subscripts have been introduced I think we should be aiming to move the API to them as this cleaner and simpler. For example:
let jsonDictionary: [String: Any] = ...
let value: String? = jsonDictionary[keyPath: "data.name"]
Would replace:
let jsonDictionary: [String: Any] = ...
let value: String? = jsonDictionary.json(atKeyPath: "data.name")
I've been updating our app to Swift 5 and had this problem with JSONUtilities' convenience functions for optional values. https://forums.swift.org/t/swift-5-throwing-and-non-throwing-function/28877
It seems like we have 2 options:
try?
in their own. See PR #32I’m think option 2 is best, as said in the thread linked above, overloading functions on whether it throws isn’t recommended apparently, and it doesn’t even work unless the type signature is different, so it might break again in the future.
Currently our API to load a JSON dictionary from a filename uses a static function:
let filename = "myjsonfile"
let dictionary = try JSONDictionary.from(filename: filename)
This could be simplified by using an initiliazer which would be a more natural and simpler way of initializing a dictionary:
extension Dictionary where Key == String, Value == Any {
init(filename: String) throws {
let dictionary: [String: Any] = /// load the dictionary from the filename
self = dictionary
}
}
let filename = "myjsonfile"
let dictionary = try JSONDictionary(filename: "something")
It would great to somehow hook into when InvalidItemBehaviours happen, especially the non throwing kinds, like .remove
.value
, and .custom
when it doesn't throw.
That way a client app can log a warning or somehow be alerted to otherwise unnoticed events. (To for example alert the backend that certain elements are malformed)
Because there is no state that is passed through the decoding process, maybe just a static func on InvalidItemNotifier that would maintain a static array of listeners:
static func addListener(handler: (InvalidItemEvent) -> ()) -> InvalidItemNotifier.Listener
Thoughts?
JSONUtilities doesn't build anymore in Carthage 0.26.0+
Not sure what the reason is. I've opened an issue there
Carthage/Carthage#2230
If a key is "some.key" this treated as a keypath made of "some" and "key". Even though it's rare that an API would provide keys with dots it would be useful to add the ability to escape the dots.
For example:
let name: String = try jsonDictionary.json(atKeyPath: "some\\.key")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.