Comments (7)
it should be as simple as just making it conform to that protocol. Make CurrencyValue
conform to JSONDecodable and then implement the decode method for it.
from argo.
I have now this:
extension CurrencyValue : JSONDecodable {
static func create(rawValue: String) -> CurrencyValue {
if let newValue = CurrencyValue(rawValue: rawValue) {
return newValue
}
return .unknown
}
static func decode(j: JSONValue) -> CurrencyValue? {
return CurrencyValue.create
<^> j <| "amount"
<*> j <| "currency"
}
}
But I fail somehow at the implementation of the decode method. I didn't get the Idea of that functional operators yet. Can you give me a tip?
from argo.
so your CurrencyValue
type is an enum that represents a single string. To create an instance of the enum you have written the create
function that takes a single string and parses it into a CurrencyValue
instance. Now in the decode
function you want to create that CurrencyValue
with the string in the JSONValue
using the create
method.
without getting into what fmap and apply mean ... since your create function takes one parameter you should only give it one parameter ... in this example you are trying to pass in 2 parameters. Replace "amount"
with "currency"
and remove the second line.
from argo.
I tested this, but it didn't work. I now follow the idea to store whatever comes from the server as raw value and implement a parser function for every enum inside the model struct, to access the enum value... Maybe there is a more elegant solution to this.
The problem was probably that the JSONValue 'j' coming to decode() is already the value of the JSON field with the key currency, so there is nothing more to decode. I would just need to call the create function with the raw value. But then I think I circumvent the system and loose the error checking because I do not use the operators then... Am I at least a littl bit right :-D?
from argo.
If j
is already the value you want you could just do return CurrencyValue.create <^> j.value()
or ... <^> String.decode(j)
from argo.
Ahhh cool thank you very much 👍
That saves a lot of work!
from argo.
No problem. Closing this now.
from argo.
Related Issues (20)
- error: no such module 'Runes' HOT 7
- Problem with Decodable on xcode 9 HOT 3
- RFC: Renaming the Decodable protocol
- How to decode a "Dictionary" to "[Model]"? HOT 3
- Parse nested array failed HOT 5
- Parse nested model fails when adding a specific key HOT 2
- How to decode dynamic keys HOT 2
- RFC: Renaming (or removing) the `<|` family of operators HOT 5
- Decode type 'Any' HOT 2
- RFC: Make Swift.Decodable conform to Argo.Decodable in Swift 4.1 HOT 4
- Convert all wrong type return JSON to specific type HOT 6
- Decoding the same object with different keys HOT 6
- How to parse an empty dictionary object in Argo HOT 1
- [Question] Array decoding HOT 4
- Using pure can't compile using Swift 3 HOT 2
- Decodable' is ambiguous for type lookup in this context (Swift4) HOT 1
- Parsing a json array with strings and objects intermixed HOT 2
- Xcode 11.0 compatibility issue HOT 3
- Xcode 11.2.1 Binary operator '<^>' cannot be applied to operands of type HOT 7
- Upgrade to version 5.0.0 blocked due to Ogra library HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argo.