0

I need to parse JSOG in my iOS application on swift. I'm trying to write deserializer by myself, but I don't know how to work with so abstract values. I have something like this, but it didn't work.

func parseElemJsog(refs: inout [String: Any], elem: Any) -> Any {
    if elem is [Any] {

        for subelem in elem as! [Any] {
            parseElemJsog(refs: &refs, elem: subelem)
        }

    } else if elem is [String : Any] {

        var dict = elem as! [String : Any]

        if dict["@ref"] == nil {
            refs[dict["@id"] as! String] = dict
            dict.removeValue(forKey: "@id")
            for subelem in dict {
                if subelem.key != "@id" {
                    parseElemJsog(refs: &refs, elem: subelem.value, resutl: &resutl)
                }
            }
        } else {
            let refId = dict["@ref"] as! String
            for (key, value) in refs[refId] as! [String: Any] {
                dict[key] = value
            }
        }
    } else {

    }
}

And I can't use JSONDecoder because I haven't got some model of data. Can anyone advice me some libraries or method to parse jsog? I don't know what to do me. Thanks for attention.

I want to parse something like this:

[
    {
        "@id": "1",
        "name": "Sally",
        "secretSanta": {
            "@id": "2",
            "name": "Bob",
            "secretSanta": {
                "@id": "3",
                "name": "Fred",
                "secretSanta": { "@ref": "1" }
            }
        }
    },
    { "@ref": "2" },
    { "@ref": "3" }
]
TiiRiiX
  • 133
  • 1
  • 9
  • What is `JSOG` (with `G`)? – vadian Jan 10 '19 at 12:13
  • @vadian Is It is convention which allows arbitrary object graphs to be represented in JSON. Helps to avoid error by recursive structures. https://github.com/jsog/jsog – TiiRiiX Jan 10 '19 at 12:17
  • 1
    Actually this pattern requires a two-phase parser. First to collect all objects with `@id` and then replace the `@ref` occurrences with the corresponding objects. – vadian Jan 10 '19 at 12:42

0 Answers0