GithubHelp home page GithubHelp logo

saoudrizwan / dynamicjson Goto Github PK

View Code? Open in Web Editor NEW
713.0 23.0 23.0 48 KB

Access JSON properties dynamically like JavaScript using Swift 4.2's new @dynamicMemberLookup feature

Home Page: https://twitter.com/sdrzn/status/1080340307802173440

License: MIT License

Objective-C 3.32% Swift 88.94% Ruby 7.74%
swift ios swift4-2 swift4 ios-swift macos tvos watchos

dynamicjson's People

Contributors

saoudrizwan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dynamicjson's Issues

How can I convert Dictionary<String,Any> to JSON?

I used the source code in a playground, and when I want to create a JSON object from a dictionary (it is [String: Any]), it gives me this:

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

I used this:

let dictionary: [String: Any] = [
  "name"    : "Omid",
  "age"     : 29,
  "married" : false
]

let dictionaryAsJSON = JSON(dictionary)

print(dictionaryAsJSON.description)

I think it is only in runtime, because Xcode does not show any issue, until running plaground.

screen shot 2019-01-03 at 19 36 06

Treat optionals as `JSON.null` to remove all the explicit unwrapping

The thing you present primarily looks ugly because of the optional chaining at every single step:

if let street = json.person?.address?[0].street.stringValue { }

But it doesn't have to be like that (it is dynamic anyways). I think you could improve that a lot by essentially adding "nil messaging":

if let street = json.person.address[0].street.stringValue { .. }

Instead of returning an optional in every item of the chain, only make the leaf accessors (stringValue, intValue, etc) optional. When a lookup is not successful, return JSON.null (which itself returns JSON.null on all JSON accesses, so that the result propagates).

If you'd like you could also add a JSON.lookupError(parent, key) as a result for missed lookups for debugging purposes (and which also propagates down the call chain, and could even persist the lookup path from the root of the first lookup failure).

JSON values aren't decomposed in JSON dictionary literals

Composing a dictionary literal with JSON properties results in those properties being null, instead of decomposing them into dictionaries themselves. For example, when

let foo = ["foo": 3] as JSON
let bar = ["bar": foo] as JSON

bar should equal

[
    "foo": [
        "bar": 3
    ]
] as JSON

However, bar actually equals

[
    "foo": JSON.null
] as JSON

To fix this issue, the JSON.init(_ object: Any) initializer should set self to object when object is an instance of JSON. I'll submit a PR for this later today; let me know what you think!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.