rundfunk47 / juice Goto Github PK
View Code? Open in Web Editor NEWA modern and simple JSON Encoder / Decoder for Swift 3
License: MIT License
A modern and simple JSON Encoder / Decoder for Swift 3
License: MIT License
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
{"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.
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.
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.