Coder Social home page Coder Social logo

Comments (4)

tonyd256 avatar tonyd256 commented on May 23, 2024

@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.

Blackjacx avatar Blackjacx commented on May 23, 2024

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.

Blackjacx avatar Blackjacx commented on May 23, 2024

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.

Blackjacx avatar Blackjacx commented on May 23, 2024

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)

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.