Comments (4)
@Blackjacx This can be done using Argo although it's a little bit more manual work. If this is your initial JSONValue
you can use the subscript to get at the creatures
JSONValue
. Then you would have to call .value()
on that to get the Dictionary<String, JSONValue>
back. That dictionary will have the keys as your creature type and the values as your creature object. You can now iterate over that dictionary to create your objects. Here'd an example of what it might look like:
- I'm assuming your
creature
object has all those 3 properties on it.
let value = // the initial JSONValue
let creatures = j["creatures"]?.value()
let array: [Creatures]? = creatures?.map { (key, value) in
let type = CreatureTypeEnum(rawValue: key)
return Creature.create
<^> type
<*> value <| "name"
<*> value <| "description"
}
Something like that.
So this can be done, although you don't get to use much of the fun stuff with Argo. If you have control over the JSON that is returned I would recommend that you make it look more like this:
{
"creatures": [
{
"type": "mammal",
"name": "human",
"description": "something"
},
....
]
}
This will make the parsing and using Argo dead simple. I think this would improved the parsing with any JSON parser or even in Objective-C. An example with Argo for this:
let value = // the initial JSONValue
let creatures: [Creature]? = value <|| "creatures"
static func decode(j: JSONValue) -> Creature? {
return Creature.create
<^> j <| "type"
<*> j <| "name"
<*> j <| "description"
}
Hope that helps!
from argo.
Yes I know thats a stupid JSON structure. But in our context it makes sense. Unfortunately the target structure needs to be a dictionary [CreatureTypeEnum : Creature] for the same reason. It seems to me that you create an array - am I right? And where do I get the 'value' variable from? As input to the decode I have 'j'. So what is the initial JSON value of 'value'? My Creatures object has only the two properties as shown in the JSON.
Thank you very much vor your help!
from argo.
Here is a complete example of my structs and enums. The last one is that which does not work:
enum CreatureTypeEnum : String {
case mammal = "mammal"
case fish = "fish"
}
struct Creature {
var name: String
var description: String
}
struct CreatureCollection {
var creatureForType = [CreatureTypeEnum : Creature]()
}
extension Creature : JSONDecodable {
static func create(name: String)(description: String) -> Creature {
return Creature(name: name, description: description)
}
static func decode(j: JSONValue) -> Creature? {
return Creature.create
<^> j <| "name"
<*> j <| "description"
}
}
extension CreatureCollection : JSONDecodable {
static func create(creatureForType: [CreatureTypeEnum : Creature]) -> CreatureCollection {
return CreatureCollection(creatureForType: creatureForType)
}
static func decode(j: JSONValue) -> CreatureCollection? {
let value = j.value() // COMPILER ERROR: Argument for generic parameter A connot be inferred
var creaturesForType = [CreatureTypeEnum: Creature]()
let array: [Creature]? = value?.map { (key, value) in
let type = CreatureTypeEnum(rawValue: key)
let creature = Creature.decode(value)
dictionaryOfCreatures[type] = creature
return creature
}
return CreatureCollection(creatureForType: creaturesForType)
}
}
The array from let array ...
is not used but inside the closure I fill my dictionary and return that in the end. Is that a suitable way of decoding that structure in your eyes? Ahh and j
is already my creatures dictionary, with all the raw CreatureTypeEnums as raw-string keys.
from argo.
OK I finally gave up trying this and changed my object not to be a dictionary of creatures indexed by the creature type but I now have a creature variable for each creature type. that seems to work better and is more intuitive.
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.