Giter Site home page Giter Site logo

juice's People

Contributors

dearprakash avatar rundfunk47 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

dearprakash

juice's Issues

Exception from deep within when decoding a derived class inside an object

Hi thanks for the updated version 1.0.9.

I just stumbled upon another issue with Juice.

I have a class B which inherits from a super class A:

class A: Juice.Decodable {
    var a: String?

    required init(fromJson json: Juice.JSONDictionary) throws {
        a = try? json.decode(["a"])
    }
}

class B: A {
    var b: String?

    required init(fromJson json: Juice.JSONDictionary) throws {
        b = try? json.decode(["b"])
        try super.init(fromJson: json)
    }
}

When I decode B (in my case an array of B's (not shown in this reduced example)) I get an exception from deep inside of Juice.

Example code:

        // Decoding simple object B -> Works
        do {
            let string = "{\"a\": \"Apple 0\", \"b\": \"Brother 0\"}"
            let dict = try toStrictlyTypedJSON(toLooselyTypedJSON(string)) as! JSONDictionary
            let o = try B(fromJson: dict)
            print(o.a, o.b)
        } catch {
            print(error.localizedDescription)
        }

        // B as a part of an object -> Fails
        do {
            let string = "{\"object\": {\"a\": \"Apple 1\", \"b\": \"Brother 1\"}}"
            let dict = try toStrictlyTypedJSON(toLooselyTypedJSON(string)) as! JSONDictionary

            // This works
            let oa: A = try dict.decode("object")
            print(oa.a)

            // This fails with exception "Could not cast value of type 'A' to 'B'"
            let ob: B = try dict.decode("object")
            print(ob.a, ob.b)
        } catch {
            print(error.localizedDescription)
        }

My knowledge of generics is a bit to shallow to find the reason.

All the best

null should be a valid type

{"a": "Apple 0", "b": null}

I get Juice.NotDecodableJSONError when trying to convert the string as follows:

let string = "{\"a\": \"Apple\", \"b\": null}"
let dict = try! toStrictlyTypedJSON(toLooselyTypedJSON(string)) as! JSONDictionary

A have verified the json with https://jsonformatter.curiousconcept.com/ and null is a valid type.

Parsing json number without decimal part into Double? fails

I receive runtimeHours as json to my app;

{
...
"runtimeHours": 1234,
...
}

In my model the value is an optional Double.
I found that my values always is nil using this code:

runtimeHours = try? json.decode(["runtimeHours"])

I thinks this is a bug, 1234 should be treated as a valid Double.

If I do try! instead the exception is:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Juice.DictionaryDecodingError(keyPath: ["runtimeHours"], underlyingError: Juice.MismatchError.typeMismatch(expected: Swift.Double, got: 1234))

Finally, my workaround now is this:

        if let intValue: Int = try? json.decode(["runtimeHours"]) {
            runtimeHours = Double(intValue)
        } else {
            runtimeHours = try? json.decode(["runtimeHours"])
        }

BTW, I like your pod a lot. Cheers.

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.