Skip to content

Instantly share code, notes, and snippets.

@ChrisMarshallNY
Created April 13, 2020 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMarshallNY/7d7137319bc34fc8d97c48b88782d794 to your computer and use it in GitHub Desktop.
Save ChrisMarshallNY/7d7137319bc34fc8d97c48b88782d794 to your computer and use it in GitHub Desktop.
Companion Repos to the Series on Writing A Parser in Swift
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": "Element Value 01"},
{"arrayElement": "Element Value 02"},
{"arrayElement": "Element Value 03"},
{"arrayElement": "Element Value 04"},
{"arrayElement": "Element Value 05"}
]
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
{"containerElement": [ {"arrayElement": {"value": "Element Value 01"}},
{"arrayElement": {"value": "Element Value 02"}},
{"arrayElement": {"value": "Element Value 03"}},
{"arrayElement": {"value": "Element Value 04"}},
{"arrayElement": {"value": "Element Value 05"}}
]
}
-->
<containerElement>
<arrayElement>
<value>Element Value 01</value>
<value>Element Value 02</value>
<value>Element Value 03</value>
<value>Element Value 04</value>
<value>Element Value 05</value>
</arrayElement>
</containerElement>
<!--
{"containerElement": {"arrayElement": [ {"value": "Element Value 01"},
{"value": "Element Value 02"},
{"value": "Element Value 03"},
{"value": "Element Value 04"},
{"value": "Element Value 05"}
]
}
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
-->
<containerElement>
<arrayElement value="Element Value 01"/>
<arrayElement value="Element Value 02"/>
<arrayElement value="Element Value 03"/>
<arrayElement value="Element Value 04"/>
<arrayElement value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": {"attributes": {"value": "Element Value 01"}}},
{"arrayElement": {"attributes": {"value": "Element Value 02"}}},
{"arrayElement": {"attributes": {"value": "Element Value 03"}}},
{"arrayElement": {"attributes": {"value": "Element Value 04"}}},
{"arrayElement": {"attributes": {"value": "Element Value 05"}}}
]
}
-->
<containerElement>
<arrayElement index="0">Element Value 01</arrayElement>
<arrayElement index="1">Element Value 02</arrayElement>
<arrayElement index="2">Element Value 03</arrayElement>
<arrayElement index="3">Element Value 04</arrayElement>
<arrayElement index="4">Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": 0}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": 1}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": 2}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": 3}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": 4}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": "1"}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": "2"}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": "3"}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": "4"}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": {"index": "0"}}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": {"index": "1"}}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": {"index": "2"}}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": {"index": "3"}}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": {"index": "4"}}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"index": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"index": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"index": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"index": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
<containerElement>
<arrayElement row="0" value="Element Value 01"/>
<arrayElement row="1" value="Element Value 02"/>
<arrayElement row="2" value="Element Value 03"/>
<arrayElement row="3" value="Element Value 04"/>
<arrayElement row="4" value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"row": 0}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": 1}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": 2}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": 3}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": 4}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": 0}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": 1}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": 2}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": 3}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": 4}, {"value": "Element Value 05"}]}]}
]
}
{"containerElement": [ {"arrayElement": {"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
*/
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": "Element Value 01"},
{"arrayElement": "Element Value 02"},
{"arrayElement": "Element Value 03"},
{"arrayElement": "Element Value 04"},
{"arrayElement": "Element Value 05"}
]
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
{"containerElement": [ {"arrayElement": {"value": "Element Value 01"}},
{"arrayElement": {"value": "Element Value 02"}},
{"arrayElement": {"value": "Element Value 03"}},
{"arrayElement": {"value": "Element Value 04"}},
{"arrayElement": {"value": "Element Value 05"}}
]
}
-->
<containerElement>
<arrayElement>
<value>Element Value 01</value>
<value>Element Value 02</value>
<value>Element Value 03</value>
<value>Element Value 04</value>
<value>Element Value 05</value>
</arrayElement>
</containerElement>
<!--
{"containerElement": {"arrayElement": [ {"value": "Element Value 01"},
{"value": "Element Value 02"},
{"value": "Element Value 03"},
{"value": "Element Value 04"},
{"value": "Element Value 05"}
]
}
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
-->
<containerElement>
<arrayElement value="Element Value 01"/>
<arrayElement value="Element Value 02"/>
<arrayElement value="Element Value 03"/>
<arrayElement value="Element Value 04"/>
<arrayElement value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": {"attributes": {"value": "Element Value 01"}}},
{"arrayElement": {"attributes": {"value": "Element Value 02"}}},
{"arrayElement": {"attributes": {"value": "Element Value 03"}}},
{"arrayElement": {"attributes": {"value": "Element Value 04"}}},
{"arrayElement": {"attributes": {"value": "Element Value 05"}}}
]
}
-->
<containerElement>
<arrayElement index="0">Element Value 01</arrayElement>
<arrayElement index="1">Element Value 02</arrayElement>
<arrayElement index="2">Element Value 03</arrayElement>
<arrayElement index="3">Element Value 04</arrayElement>
<arrayElement index="4">Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": 0}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": 1}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": 2}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": 3}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": 4}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": "1"}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": "2"}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": "3"}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": "4"}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": {"index": "0"}}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": {"index": "1"}}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": {"index": "2"}}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": {"index": "3"}}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": {"index": "4"}}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"index": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"index": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"index": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"index": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
<containerElement>
<arrayElement row="0" value="Element Value 01"/>
<arrayElement row="1" value="Element Value 02"/>
<arrayElement row="2" value="Element Value 03"/>
<arrayElement row="3" value="Element Value 04"/>
<arrayElement row="4" value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"row": 0}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": 1}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": 2}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": 3}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": 4}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": 0}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": 1}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": 2}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": 3}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": 4}, {"value": "Element Value 05"}]}]}
]
}
{"containerElement": [ {"arrayElement": {"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
*/
import Foundation
func convertJSONToDictionary(_ inJSON: String) -> [String: Any] {
do {
if let stringData = inJSON.data(using: .utf8), let jsonDict = try JSONSerialization.jsonObject(with: stringData) as? [String: Any] {
return jsonDict
}
} catch(let error) {
print(String(describing: error))
}
return [:]
}
var jsonInterpretations: [[String: Any]] = []
var jsonInterpretationsSecondaryTests: [[String: Any]] = []
func loadJSONIntoDictionaries() {
// Initial test set
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"Element Value 01\"}, {\"arrayElement\": \"Element Value 02\"}, {\"arrayElement\": \"Element Value 03\"}, {\"arrayElement\": \"Element Value 04\"}, {\"arrayElement\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"value\": \"Element Value 01\"}}, {\"arrayElement\": {\"value\": \"Element Value 02\"}}, {\"arrayElement\": {\"value\": \"Element Value 03\"}}, {\"arrayElement\": {\"value\": \"Element Value 04\"}}, {\"arrayElement\": {\"value\": \"Element Value 05\"}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 01\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 02\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 03\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 04\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 05\"}}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": 0}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 1}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 2}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 3}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 4}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": {\"index\": \"0\"}}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"1\"}}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"2\"}}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"3\"}}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"4\"}}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
// Extra Credit
jsonInterpretations.append(convertJSONToDictionary("{\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}]}"))
// Differing Data
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": 3}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"3.14\"}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 1}, {\"value\": \"0123456789!@~*&^%$#.,;:?/'\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": 1234567890}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 0}, {\"value\": \"12345.6789\"}]}}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"3\"}, {\"arrayElement\": \"3.14\"}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": \"1234567890\"}, {\"arrayElement\": \"12345.6789\"}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": 3}, {\"arrayElement\": 3.14}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": 1234567890}, {\"arrayElement\": 12345.6789}]}"))
}
loadJSONIntoDictionaries()
let whatWeWantToSee: [String] = ["Element Value 01", "Element Value 02", "Element Value 03", "Element Value 04", "Element Value 05"]
let whatWeWantToSee2: [String] = ["0123456789!@~*&^%$#.,;:?/'", "12345.6789", "1234567890", "3", "3.14"]
func myParser(_ inDictionaryToBeParsed: [String: Any]) -> [String] {
return []
}
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": "Element Value 01"},
{"arrayElement": "Element Value 02"},
{"arrayElement": "Element Value 03"},
{"arrayElement": "Element Value 04"},
{"arrayElement": "Element Value 05"}
]
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
{"containerElement": [ {"arrayElement": {"value": "Element Value 01"}},
{"arrayElement": {"value": "Element Value 02"}},
{"arrayElement": {"value": "Element Value 03"}},
{"arrayElement": {"value": "Element Value 04"}},
{"arrayElement": {"value": "Element Value 05"}}
]
}
-->
<containerElement>
<arrayElement>
<value>Element Value 01</value>
<value>Element Value 02</value>
<value>Element Value 03</value>
<value>Element Value 04</value>
<value>Element Value 05</value>
</arrayElement>
</containerElement>
<!--
{"containerElement": {"arrayElement": [ {"value": "Element Value 01"},
{"value": "Element Value 02"},
{"value": "Element Value 03"},
{"value": "Element Value 04"},
{"value": "Element Value 05"}
]
}
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
-->
<containerElement>
<arrayElement value="Element Value 01"/>
<arrayElement value="Element Value 02"/>
<arrayElement value="Element Value 03"/>
<arrayElement value="Element Value 04"/>
<arrayElement value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": {"attributes": {"value": "Element Value 01"}}},
{"arrayElement": {"attributes": {"value": "Element Value 02"}}},
{"arrayElement": {"attributes": {"value": "Element Value 03"}}},
{"arrayElement": {"attributes": {"value": "Element Value 04"}}},
{"arrayElement": {"attributes": {"value": "Element Value 05"}}}
]
}
-->
<containerElement>
<arrayElement index="0">Element Value 01</arrayElement>
<arrayElement index="1">Element Value 02</arrayElement>
<arrayElement index="2">Element Value 03</arrayElement>
<arrayElement index="3">Element Value 04</arrayElement>
<arrayElement index="4">Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": 0}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": 1}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": 2}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": 3}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": 4}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": "1"}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": "2"}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": "3"}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": "4"}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": {"index": "0"}}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": {"index": "1"}}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": {"index": "2"}}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": {"index": "3"}}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": {"index": "4"}}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"index": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"index": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"index": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"index": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
<containerElement>
<arrayElement row="0" value="Element Value 01"/>
<arrayElement row="1" value="Element Value 02"/>
<arrayElement row="2" value="Element Value 03"/>
<arrayElement row="3" value="Element Value 04"/>
<arrayElement row="4" value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"row": 0}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": 1}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": 2}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": 3}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": 4}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": 0}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": 1}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": 2}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": 3}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": 4}, {"value": "Element Value 05"}]}]}
]
}
{"containerElement": [ {"arrayElement": {"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
*/
import Foundation
func convertJSONToDictionary(_ inJSON: String) -> [String: Any] {
do {
if let stringData = inJSON.data(using: .utf8), let jsonDict = try JSONSerialization.jsonObject(with: stringData) as? [String: Any] {
return jsonDict
}
} catch(let error) {
print(String(describing: error))
}
return [:]
}
var jsonInterpretations: [[String: Any]] = []
var jsonInterpretationsSecondaryTests: [[String: Any]] = []
func loadJSONIntoDictionaries() {
// Initial test set
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"Element Value 01\"}, {\"arrayElement\": \"Element Value 02\"}, {\"arrayElement\": \"Element Value 03\"}, {\"arrayElement\": \"Element Value 04\"}, {\"arrayElement\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"value\": \"Element Value 01\"}}, {\"arrayElement\": {\"value\": \"Element Value 02\"}}, {\"arrayElement\": {\"value\": \"Element Value 03\"}}, {\"arrayElement\": {\"value\": \"Element Value 04\"}}, {\"arrayElement\": {\"value\": \"Element Value 05\"}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 01\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 02\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 03\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 04\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 05\"}}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": 0}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 1}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 2}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 3}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 4}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": {\"index\": \"0\"}}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"1\"}}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"2\"}}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"3\"}}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"4\"}}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
// Extra Credit
jsonInterpretations.append(convertJSONToDictionary("{\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}]}"))
// Differing Data
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": 3}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"3.14\"}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 1}, {\"value\": \"0123456789!@~*&^%$#.,;:?/'\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": 1234567890}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 0}, {\"value\": \"12345.6789\"}]}}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"3\"}, {\"arrayElement\": \"3.14\"}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": \"1234567890\"}, {\"arrayElement\": \"12345.6789\"}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": 3}, {\"arrayElement\": 3.14}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": 1234567890}, {\"arrayElement\": 12345.6789}]}"))
}
loadJSONIntoDictionaries()
let whatWeWantToSee: [String] = ["Element Value 01", "Element Value 02", "Element Value 03", "Element Value 04", "Element Value 05"]
let whatWeWantToSee2: [String] = ["0123456789!@~*&^%$#.,;:?/'", "12345.6789", "1234567890", "3", "3.14"]
func recursiveDescentParser(_ inDictionaryToBeParsed: [String: Any]) -> [String] {
return []
}
// Test Set 1
jsonInterpretations.forEach {
let parsedResult = recursiveDescentParser($0)
if whatWeWantToSee != parsedResult {
// print("ERROR! Bad Result for \($0)")
print("ERROR! Bad Result!")
} else {
// print("\($0) Passes!")
print("Passes!")
}
}
// Test Set 2
//jsonInterpretationsSecondaryTests.forEach {
// let parsedResult = recursiveDescentParser($0)
// if whatWeWantToSee2 != parsedResult {
// print("ERROR! Bad Result for \($0)")
// } else {
// print("\($0) Passes!")
// }
//}
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": "Element Value 01"},
{"arrayElement": "Element Value 02"},
{"arrayElement": "Element Value 03"},
{"arrayElement": "Element Value 04"},
{"arrayElement": "Element Value 05"}
]
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
{"containerElement": [ {"arrayElement": {"value": "Element Value 01"}},
{"arrayElement": {"value": "Element Value 02"}},
{"arrayElement": {"value": "Element Value 03"}},
{"arrayElement": {"value": "Element Value 04"}},
{"arrayElement": {"value": "Element Value 05"}}
]
}
-->
<containerElement>
<arrayElement>
<value>Element Value 01</value>
<value>Element Value 02</value>
<value>Element Value 03</value>
<value>Element Value 04</value>
<value>Element Value 05</value>
</arrayElement>
</containerElement>
<!--
{"containerElement": {"arrayElement": [ {"value": "Element Value 01"},
{"value": "Element Value 02"},
{"value": "Element Value 03"},
{"value": "Element Value 04"},
{"value": "Element Value 05"}
]
}
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
-->
<containerElement>
<arrayElement value="Element Value 01"/>
<arrayElement value="Element Value 02"/>
<arrayElement value="Element Value 03"/>
<arrayElement value="Element Value 04"/>
<arrayElement value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": {"attributes": {"value": "Element Value 01"}}},
{"arrayElement": {"attributes": {"value": "Element Value 02"}}},
{"arrayElement": {"attributes": {"value": "Element Value 03"}}},
{"arrayElement": {"attributes": {"value": "Element Value 04"}}},
{"arrayElement": {"attributes": {"value": "Element Value 05"}}}
]
}
-->
<containerElement>
<arrayElement index="0">Element Value 01</arrayElement>
<arrayElement index="1">Element Value 02</arrayElement>
<arrayElement index="2">Element Value 03</arrayElement>
<arrayElement index="3">Element Value 04</arrayElement>
<arrayElement index="4">Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": 0}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": 1}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": 2}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": 3}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": 4}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": "1"}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": "2"}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": "3"}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": "4"}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": {"index": "0"}}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": {"index": "1"}}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": {"index": "2"}}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": {"index": "3"}}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": {"index": "4"}}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"index": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"index": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"index": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"index": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
<containerElement>
<arrayElement row="0" value="Element Value 01"/>
<arrayElement row="1" value="Element Value 02"/>
<arrayElement row="2" value="Element Value 03"/>
<arrayElement row="3" value="Element Value 04"/>
<arrayElement row="4" value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"row": 0}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": 1}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": 2}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": 3}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": 4}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": 0}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": 1}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": 2}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": 3}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": 4}, {"value": "Element Value 05"}]}]}
]
}
{"containerElement": [ {"arrayElement": {"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
*/
import Foundation
func convertJSONToDictionary(_ inJSON: String) -> [String: Any] {
do {
if let stringData = inJSON.data(using: .utf8), let jsonDict = try JSONSerialization.jsonObject(with: stringData) as? [String: Any] {
return jsonDict
}
} catch(let error) {
print(String(describing: error))
}
return [:]
}
var jsonInterpretations: [[String: Any]] = []
var jsonInterpretationsSecondaryTests: [[String: Any]] = []
func loadJSONIntoDictionaries() {
// Initial test set
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"Element Value 01\"}, {\"arrayElement\": \"Element Value 02\"}, {\"arrayElement\": \"Element Value 03\"}, {\"arrayElement\": \"Element Value 04\"}, {\"arrayElement\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"value\": \"Element Value 01\"}}, {\"arrayElement\": {\"value\": \"Element Value 02\"}}, {\"arrayElement\": {\"value\": \"Element Value 03\"}}, {\"arrayElement\": {\"value\": \"Element Value 04\"}}, {\"arrayElement\": {\"value\": \"Element Value 05\"}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 01\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 02\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 03\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 04\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 05\"}}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": 0}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 1}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 2}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 3}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 4}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": {\"index\": \"0\"}}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"1\"}}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"2\"}}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"3\"}}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"4\"}}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
// Extra Credit
jsonInterpretations.append(convertJSONToDictionary("{\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}]}"))
// Differing Data
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": 3}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"3.14\"}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 1}, {\"value\": \"0123456789!@~*&^%$#.,;:?/'\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": 1234567890}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 0}, {\"value\": \"12345.6789\"}]}}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"3\"}, {\"arrayElement\": \"3.14\"}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": \"1234567890\"}, {\"arrayElement\": \"12345.6789\"}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": 3}, {\"arrayElement\": 3.14}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": 1234567890}, {\"arrayElement\": 12345.6789}]}"))
}
loadJSONIntoDictionaries()
let whatWeWantToSee: [String] = ["Element Value 01", "Element Value 02", "Element Value 03", "Element Value 04", "Element Value 05"]
let whatWeWantToSee2: [String] = ["0123456789!@~*&^%$#.,;:?/'", "12345.6789", "1234567890", "3", "3.14"]
func recursiveDescentParser(_ inDictionaryToBeParsed: [AnyHashable: Any]) -> [String] {
var ret = [String]()
inDictionaryToBeParsed.forEach {
if let asString = $0.value as? String {
ret += [asString]
} else if let asDictionary = $0.value as? [AnyHashable: Any] {
ret += recursiveDescentParser(asDictionary)
} else if let asDictionaryArray = $0.value as? [[AnyHashable: Any]] {
asDictionaryArray.forEach { elem in
ret += recursiveDescentParser(elem)
}
} else if let asArray = $0.value as? [String] {
asArray.forEach { elem in
ret += recursiveDescentParser(["": elem])
}
}
}
return ret
}
// Test Set 1
jsonInterpretations.forEach {
let parsedResult = recursiveDescentParser($0)
if whatWeWantToSee != parsedResult {
// print("ERROR! Bad Result for \($0)")
print("ERROR! Bad Result!")
} else {
// print("\($0) Passes!")
print("Passes!")
}
}
// Test Set 2
//jsonInterpretationsSecondaryTests.forEach {
// let parsedResult = recursiveDescentParser($0)
// if whatWeWantToSee2 != parsedResult {
// print("ERROR! Bad Result for \($0)")
// } else {
// print("\($0) Passes!")
// }
//}
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": "Element Value 01"},
{"arrayElement": "Element Value 02"},
{"arrayElement": "Element Value 03"},
{"arrayElement": "Element Value 04"},
{"arrayElement": "Element Value 05"}
]
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
{"containerElement": [ {"arrayElement": {"value": "Element Value 01"}},
{"arrayElement": {"value": "Element Value 02"}},
{"arrayElement": {"value": "Element Value 03"}},
{"arrayElement": {"value": "Element Value 04"}},
{"arrayElement": {"value": "Element Value 05"}}
]
}
-->
<containerElement>
<arrayElement>
<value>Element Value 01</value>
<value>Element Value 02</value>
<value>Element Value 03</value>
<value>Element Value 04</value>
<value>Element Value 05</value>
</arrayElement>
</containerElement>
<!--
{"containerElement": {"arrayElement": [ {"value": "Element Value 01"},
{"value": "Element Value 02"},
{"value": "Element Value 03"},
{"value": "Element Value 04"},
{"value": "Element Value 05"}
]
}
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
-->
<containerElement>
<arrayElement value="Element Value 01"/>
<arrayElement value="Element Value 02"/>
<arrayElement value="Element Value 03"/>
<arrayElement value="Element Value 04"/>
<arrayElement value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": {"attributes": {"value": "Element Value 01"}}},
{"arrayElement": {"attributes": {"value": "Element Value 02"}}},
{"arrayElement": {"attributes": {"value": "Element Value 03"}}},
{"arrayElement": {"attributes": {"value": "Element Value 04"}}},
{"arrayElement": {"attributes": {"value": "Element Value 05"}}}
]
}
-->
<containerElement>
<arrayElement index="0">Element Value 01</arrayElement>
<arrayElement index="1">Element Value 02</arrayElement>
<arrayElement index="2">Element Value 03</arrayElement>
<arrayElement index="3">Element Value 04</arrayElement>
<arrayElement index="4">Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": 0}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": 1}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": 2}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": 3}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": 4}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": "1"}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": "2"}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": "3"}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": "4"}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": {"index": "0"}}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": {"index": "1"}}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": {"index": "2"}}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": {"index": "3"}}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": {"index": "4"}}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"index": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"index": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"index": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"index": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
<containerElement>
<arrayElement row="0" value="Element Value 01"/>
<arrayElement row="1" value="Element Value 02"/>
<arrayElement row="2" value="Element Value 03"/>
<arrayElement row="3" value="Element Value 04"/>
<arrayElement row="4" value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"row": 0}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": 1}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": 2}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": 3}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": 4}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": 0}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": 1}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": 2}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": 3}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": 4}, {"value": "Element Value 05"}]}]}
]
}
{"containerElement": [ {"arrayElement": {"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
*/
import Foundation
func convertJSONToDictionary(_ inJSON: String) -> [String: Any] {
do {
if let stringData = inJSON.data(using: .utf8), let jsonDict = try JSONSerialization.jsonObject(with: stringData) as? [String: Any] {
return jsonDict
}
} catch(let error) {
print(String(describing: error))
}
return [:]
}
var jsonInterpretations: [[String: Any]] = []
var jsonInterpretationsSecondaryTests: [[String: Any]] = []
func loadJSONIntoDictionaries() {
// Initial test set
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"Element Value 01\"}, {\"arrayElement\": \"Element Value 02\"}, {\"arrayElement\": \"Element Value 03\"}, {\"arrayElement\": \"Element Value 04\"}, {\"arrayElement\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"value\": \"Element Value 01\"}}, {\"arrayElement\": {\"value\": \"Element Value 02\"}}, {\"arrayElement\": {\"value\": \"Element Value 03\"}}, {\"arrayElement\": {\"value\": \"Element Value 04\"}}, {\"arrayElement\": {\"value\": \"Element Value 05\"}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 01\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 02\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 03\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 04\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 05\"}}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": 0}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 1}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 2}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 3}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 4}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": {\"index\": \"0\"}}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"1\"}}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"2\"}}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"3\"}}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"4\"}}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
// Extra Credit
jsonInterpretations.append(convertJSONToDictionary("{\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}]}"))
// Differing Data
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": 3}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"3.14\"}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 1}, {\"value\": \"0123456789!@~*&^%$#.,;:?/'\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": 1234567890}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 0}, {\"value\": \"12345.6789\"}]}}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"3\"}, {\"arrayElement\": \"3.14\"}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": \"1234567890\"}, {\"arrayElement\": \"12345.6789\"}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": 3}, {\"arrayElement\": 3.14}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": 1234567890}, {\"arrayElement\": 12345.6789}]}"))
}
loadJSONIntoDictionaries()
let whatWeWantToSee: [String] = ["Element Value 01", "Element Value 02", "Element Value 03", "Element Value 04", "Element Value 05"]
let whatWeWantToSee2: [String] = ["0123456789!@~*&^%$#.,;:?/'", "12345.6789", "1234567890", "3", "3.14"]
func recursiveDescentParser(_ inDictionaryToBeParsed: [AnyHashable: Any], blackList inBlacklist: [String] = []) -> [String] {
var ret = [String]()
inDictionaryToBeParsed.forEach {
if let asFloat = ($0.value as? Double ?? Double($0.value as? String ?? "ERROR")) {
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 1
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 20
if let keyString = $0.key as? String, !inBlacklist.contains(keyString) {
ret += [String(formatter.string(from: asFloat as NSNumber) ?? "")]
}
} else if let asString = $0.value as? String, let keyString = $0.key as? String, !inBlacklist.contains(keyString) {
ret += [asString]
} else if let asDictionary = $0.value as? [AnyHashable: Any] {
ret += recursiveDescentParser(asDictionary, blackList: inBlacklist)
} else if let asDictionaryArray = $0.value as? [[AnyHashable: Any]] {
asDictionaryArray.forEach { elem in
ret += recursiveDescentParser(elem, blackList: inBlacklist)
}
} else if let asArray = $0.value as? [String] {
asArray.forEach { elem in
ret += recursiveDescentParser(["": elem], blackList: inBlacklist)
}
}
}
return ret.sorted()
}
// Test Set 1
jsonInterpretations.forEach {
let parsedResult = recursiveDescentParser($0, blackList: ["index", "row"])
if whatWeWantToSee != parsedResult {
print("ERROR! Bad Result: \(parsedResult) for \($0)")
} else {
// print("\($0) Passes!")
}
}
// Test Set 2
jsonInterpretationsSecondaryTests.forEach {
let parsedResult = recursiveDescentParser($0, blackList: ["index", "row"])
if whatWeWantToSee2 != parsedResult {
print("ERROR! Bad Result: \(parsedResult) for \($0)")
} else {
// print("\($0) Passes!")
}
}
/*
<containerElement>
<arrayElement>Element Value 01</arrayElement>
<arrayElement>Element Value 02</arrayElement>
<arrayElement>Element Value 03</arrayElement>
<arrayElement>Element Value 04</arrayElement>
<arrayElement>Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": "Element Value 01"},
{"arrayElement": "Element Value 02"},
{"arrayElement": "Element Value 03"},
{"arrayElement": "Element Value 04"},
{"arrayElement": "Element Value 05"}
]
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
{"containerElement": [ {"arrayElement": {"value": "Element Value 01"}},
{"arrayElement": {"value": "Element Value 02"}},
{"arrayElement": {"value": "Element Value 03"}},
{"arrayElement": {"value": "Element Value 04"}},
{"arrayElement": {"value": "Element Value 05"}}
]
}
-->
<containerElement>
<arrayElement>
<value>Element Value 01</value>
<value>Element Value 02</value>
<value>Element Value 03</value>
<value>Element Value 04</value>
<value>Element Value 05</value>
</arrayElement>
</containerElement>
<!--
{"containerElement": {"arrayElement": [ {"value": "Element Value 01"},
{"value": "Element Value 02"},
{"value": "Element Value 03"},
{"value": "Element Value 04"},
{"value": "Element Value 05"}
]
}
}
{"containerElement": {"arrayElement": [ "Element Value 01",
"Element Value 02",
"Element Value 03",
"Element Value 04",
"Element Value 05"
]
}
}
-->
<containerElement>
<arrayElement value="Element Value 01"/>
<arrayElement value="Element Value 02"/>
<arrayElement value="Element Value 03"/>
<arrayElement value="Element Value 04"/>
<arrayElement value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": {"attributes": {"value": "Element Value 01"}}},
{"arrayElement": {"attributes": {"value": "Element Value 02"}}},
{"arrayElement": {"attributes": {"value": "Element Value 03"}}},
{"arrayElement": {"attributes": {"value": "Element Value 04"}}},
{"arrayElement": {"attributes": {"value": "Element Value 05"}}}
]
}
-->
<containerElement>
<arrayElement index="0">Element Value 01</arrayElement>
<arrayElement index="1">Element Value 02</arrayElement>
<arrayElement index="2">Element Value 03</arrayElement>
<arrayElement index="3">Element Value 04</arrayElement>
<arrayElement index="4">Element Value 05</arrayElement>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": 0}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": 1}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": 2}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": 3}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": 4}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}]}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": [{"index": "1"}]}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": [{"index": "2"}]}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": [{"index": "3"}]}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": [{"index": "4"}]}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": {"index": "0"}}, {"value": "Element Value 01"}]},
{"arrayElement": [{"attributes": {"index": "1"}}, {"value": "Element Value 02"}]},
{"arrayElement": [{"attributes": {"index": "2"}}, {"value": "Element Value 03"}]},
{"arrayElement": [{"attributes": {"index": "3"}}, {"value": "Element Value 04"}]},
{"arrayElement": [{"attributes": {"index": "4"}}, {"value": "Element Value 05"}]}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"index": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"index": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"index": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"index": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"index": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
<containerElement>
<arrayElement row="0" value="Element Value 01"/>
<arrayElement row="1" value="Element Value 02"/>
<arrayElement row="2" value="Element Value 03"/>
<arrayElement row="3" value="Element Value 04"/>
<arrayElement row="4" value="Element Value 05"/>
</containerElement>
<!--
{"containerElement": [ {"arrayElement": {"attributes": [{"row": 0}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": 1}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": 2}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": 3}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": 4}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": 0}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": 1}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": 2}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": 3}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": 4}, {"value": "Element Value 05"}]}]}
]
}
{"containerElement": [ {"arrayElement": {"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}},
{"arrayElement": {"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}},
{"arrayElement": {"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}},
{"arrayElement": {"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}},
{"arrayElement": {"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}}
]
}
{"containerElement": [ {"arrayElement": [{"attributes": [{"row": "0"}, {"value": "Element Value 01"}]}]},
{"arrayElement": [{"attributes": [{"row": "1"}, {"value": "Element Value 02"}]}]},
{"arrayElement": [{"attributes": [{"row": "2"}, {"value": "Element Value 03"}]}]},
{"arrayElement": [{"attributes": [{"row": "3"}, {"value": "Element Value 04"}]}]},
{"arrayElement": [{"attributes": [{"row": "4"}, {"value": "Element Value 05"}]}]}
]
}
-->
*/
import Foundation
// Exercises
enum SomeEnum {
case someCase(String)
}
let enumInstance = SomeEnum.someCase("someString")
struct SomeStruct {
struct someCase {
var value: String
init(_ inValue: String) {
value = inValue
}
}
}
let fauxEnumInstance = SomeStruct.someCase("someString")
enum SomeOtherEnum {
case someCase(String)
case someOtherCase(Int)
}
struct SomeOtherStruct {
struct someCase {
var value: String
init(_ inValue: String) {
value = inValue
}
}
struct someOtherCase {
var value: Int
init(_ inValue: Int) {
value = inValue
}
}
}
enum SomeFixedEnum: Int {
case zero = 0
case one = 1
}
struct SomeFixedStruct {
struct zero {
static var rawValue: Int {
return 0
}
}
struct one {
static var rawValue: Int {
return 1
}
}
}
struct FauxUnionStruct {
struct stringVal {
var value: String
init(_ inValue: String) {
value = inValue
}
}
struct intVal {
var value: Int
init(_ inValue: Int) {
value = inValue
}
}
struct floatVal {
var value: Double
init(_ inValue: Double) {
value = inValue
}
}
}
let aStringInstance = FauxUnionStruct.stringVal("someString")
let anIntInstance = FauxUnionStruct.intVal(123456789)
let aFloatInstance = FauxUnionStruct.floatVal(1234.56789)
print(aStringInstance.value)
print(anIntInstance.value)
print(aFloatInstance.value)
enum FauxUnionEnum {
case stringVal(String)
case intVal(Int)
case floatVal(Double)
var string: String {
switch self {
case .stringVal(let str):
return str
case .intVal(let int):
return String(int)
case .floatVal(let flt):
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 1
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 4
return formatter.string(from: flt as NSNumber) ?? ""
}
}
var int: Int {
switch self {
case .stringVal(let str):
return Int(str) ?? 0
case .intVal(let int):
return int
case .floatVal(let flt):
return Int(flt)
}
}
var float: Double {
switch self {
case .stringVal(let str):
return Double(str) ?? Double.nan
case .intVal(let int):
return Double(int)
case .floatVal(let flt):
return flt
}
}
}
let anotherStringInstance = FauxUnionEnum.stringVal("someString")
let anotherIntInstance = FauxUnionEnum.intVal(123456789)
let anotherFloatInstance = FauxUnionEnum.floatVal(1234.56789)
print(anotherStringInstance)
print(anotherIntInstance)
print(anotherFloatInstance)
print(anotherStringInstance.string)
print(anotherStringInstance.int)
print(anotherStringInstance.float)
print(anotherIntInstance.string)
print(anotherIntInstance.int)
print(anotherIntInstance.float)
print(anotherFloatInstance.string)
print(anotherFloatInstance.int)
print(anotherFloatInstance.float)
// Back to the Parser
func convertJSONToDictionary(_ inJSON: String) -> [String: Any] {
do {
if let stringData = inJSON.data(using: .utf8), let jsonDict = try JSONSerialization.jsonObject(with: stringData) as? [String: Any] {
return jsonDict
}
} catch(let error) {
print(String(describing: error))
}
return [:]
}
var jsonInterpretations: [[String: Any]] = []
var jsonInterpretationsSecondaryTests: [[String: Any]] = []
func loadJSONIntoDictionaries() {
// Initial test set
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"Element Value 01\"}, {\"arrayElement\": \"Element Value 02\"}, {\"arrayElement\": \"Element Value 03\"}, {\"arrayElement\": \"Element Value 04\"}, {\"arrayElement\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"value\": \"Element Value 01\"}}, {\"arrayElement\": {\"value\": \"Element Value 02\"}}, {\"arrayElement\": {\"value\": \"Element Value 03\"}}, {\"arrayElement\": {\"value\": \"Element Value 04\"}}, {\"arrayElement\": {\"value\": \"Element Value 05\"}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": {\"arrayElement\": [\"Element Value 01\", \"Element Value 02\", \"Element Value 03\", \"Element Value 04\", \"Element Value 05\"]}}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 01\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 02\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 03\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 04\"}}}, {\"arrayElement\": {\"attributes\": {\"value\": \"Element Value 05\"}}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": 0}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 1}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 2}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 3}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": 4}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}]}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}]}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}]}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}]}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}]}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": {\"index\": \"0\"}}, {\"value\": \"Element Value 01\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"1\"}}, {\"value\": \"Element Value 02\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"2\"}}, {\"value\": \"Element Value 03\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"3\"}}, {\"value\": \"Element Value 04\"}]}, {\"arrayElement\": [{\"attributes\": {\"index\": \"4\"}}, {\"value\": \"Element Value 05\"}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"index\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"index\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}]}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": [{\"attributes\": [{\"row\": \"0\"}, {\"value\": \"Element Value 01\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"1\"}, {\"value\": \"Element Value 02\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"2\"}, {\"value\": \"Element Value 03\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"3\"}, {\"value\": \"Element Value 04\"}]}]}, {\"arrayElement\": [{\"attributes\": [{\"row\": \"4\"}, {\"value\": \"Element Value 05\"}]}]}]}"))
// Extra Credit
jsonInterpretations.append(convertJSONToDictionary("{\"arrayElement\": [{\"value\": \"Element Value 01\"}, {\"value\": \"Element Value 02\"}, {\"value\": \"Element Value 03\"}, {\"value\": \"Element Value 04\"}, {\"value\": \"Element Value 05\"}]}"))
jsonInterpretations.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": \"Element Value 04\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"Element Value 03\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 1}, {\"value\": \"Element Value 02\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": \"Element Value 05\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 0}, {\"value\": \"Element Value 01\"}]}}]}"))
// New Format, same set of tests.
jsonInterpretations.append(convertJSONToDictionary("{\"arrayElement\": [{\"1\": \"Element Value 01\"}, {\"2\": \"Element Value 02\"}, {\"3\": \"Element Value 03\"}, {\"4\": \"Element Value 04\"}, {\"5\": \"Element Value 05\"}]}"))
// Differing Data
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": {\"attributes\": [{\"row\": 3}, {\"value\": 3}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 2}, {\"value\": \"3.14\"}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 1}, {\"value\": \"0123456789!@~*&^%$#.,;:?/'\"}]}}, {\"arrayElement\": {\"attributes\": [{\"row\": 4}, {\"value\": 1234567890}]}}, {\"arrayElement\": {\"attributes\": [{\"index\": 0}, {\"value\": \"12345.6789\"}]}}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": \"3\"}, {\"arrayElement\": \"3.14\"}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": \"1234567890\"}, {\"arrayElement\": \"12345.6789\"}]}"))
jsonInterpretationsSecondaryTests.append(convertJSONToDictionary("{\"containerElement\": [{\"arrayElement\": 3}, {\"arrayElement\": 3.14}, {\"arrayElement\": \"0123456789!@~*&^%$#.,;:?/'\"}, {\"arrayElement\": 1234567890}, {\"arrayElement\": 12345.6789}]}"))
}
enum ParserResponse: Comparable, Equatable {
case string(String)
case integer(Int)
case float(Double)
var intValue: Int {
switch self {
case .integer(let intVal):
return intVal
case .float(let floatVal):
return Int(floatVal)
case .string(let strVal):
return Int(strVal) ?? 0
}
}
var floatValue: Double {
switch self {
case .integer(let intVal):
return Double(intVal)
case .float(let floatVal):
return floatVal
case .string(let strVal):
return Double(strVal) ?? Double.nan
}
}
var stringValue: String {
switch self {
case .integer(let intVal):
return String(intVal)
case .float(let floatVal):
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 1
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 20
return formatter.string(from: floatVal as NSNumber) ?? ""
case .string(let strVal):
return strVal
}
}
static func == (lhs: ParserResponse, rhs: ParserResponse) -> Bool {
switch lhs {
case .integer:
if case .float = rhs {
return lhs.floatValue == rhs.floatValue
} else if case .string = rhs {
return lhs.floatValue == rhs.floatValue
} else {
return lhs.intValue == rhs.intValue
}
case .float:
return lhs.floatValue == rhs.floatValue
case .string:
return lhs.stringValue == rhs.stringValue
}
}
static func < (lhs: ParserResponse, rhs: ParserResponse) -> Bool {
switch lhs {
case .integer:
if case .float = rhs {
return lhs.floatValue < rhs.floatValue
} else if case .string = rhs {
return lhs.floatValue < rhs.floatValue
} else {
return lhs.intValue < rhs.intValue
}
case .float:
return lhs.floatValue < rhs.floatValue
case .string:
return lhs.stringValue < rhs.stringValue
}
}
}
extension String {
init(_ inParserResponse: ParserResponse) {
self.init(inParserResponse.stringValue)
}
}
extension Int {
init(_ inParserResponse: ParserResponse) {
self.init(inParserResponse.intValue)
}
}
extension Double {
init(_ inParserResponse: ParserResponse) {
self.init(inParserResponse.floatValue)
}
}
func recursiveDescentParser(_ inDictionaryToBeParsed: [AnyHashable: Any], blackList inBlacklist: [String] = [], whiteList inWhiteList: [String] = []) -> [ParserResponse] {
var ret = [ParserResponse]()
func isKeyValid(_ inKey: String) -> Bool {
if inBlacklist.contains(inKey) {
return false
}
if inWhiteList.isEmpty || inWhiteList.contains(inKey) {
return true
}
return false
}
inDictionaryToBeParsed.forEach {
if let asFloat = ($0.value as? Double ?? Double($0.value as? String ?? "ERROR")) {
if let keyString = $0.key as? String, isKeyValid(keyString) {
if floor(asFloat) == asFloat {
ret += [.integer(Int(asFloat))]
} else {
ret += [.float(asFloat)]
}
}
} else if let asString = $0.value as? String, let keyString = $0.key as? String, isKeyValid(keyString) {
ret += [.string(asString)]
} else if let asDictionary = $0.value as? [AnyHashable: Any] {
ret += recursiveDescentParser(asDictionary, blackList: inBlacklist, whiteList: inWhiteList)
} else if let asDictionaryArray = $0.value as? [[AnyHashable: Any]] {
asDictionaryArray.forEach { elem in
ret += recursiveDescentParser(elem, blackList: inBlacklist, whiteList: inWhiteList)
}
} else if let asArray = $0.value as? [String] {
asArray.forEach { elem in
ret += recursiveDescentParser(["": elem], blackList: inBlacklist, whiteList: inWhiteList)
}
}
}
return ret.sorted()
}
loadJSONIntoDictionaries()
//let whatWeWantToSee: [String] = ["Element Value 01", "Element Value 02", "Element Value 03", "Element Value 04", "Element Value 05"]
let whatWeWantToSee: [ParserResponse] = [.string("Element Value 01"), .string("Element Value 02"), .string("Element Value 03"), .string("Element Value 04"), .string("Element Value 05")]
//let whatWeWantToSee2: [String] = ["0123456789!@~*&^%$#.,;:?/'", "12345.6789", "1234567890", "3", "3.14"]
let whatWeWantToSee2: [ParserResponse] = [.string("0123456789!@~*&^%$#.,;:?/'"), .integer(3), .float(3.14), .float(12345.6789), .integer(1234567890)]
//let whatWeWantToSee3: [String] = ["0", "1", "2", "3", "4"]
let whatWeWantToSee3: [ParserResponse] = [.integer(0), .integer(1), .integer(2), .integer(3), .integer(4)]
// Test Set 1
jsonInterpretations.forEach {
let parsedResult = recursiveDescentParser($0, blackList: ["index", "row"])
if whatWeWantToSee != parsedResult {
print("ERROR! Bad Result: \(parsedResult) for \($0)")
} else {
// print("\($0) Passes!")
}
}
// Test Set 2
jsonInterpretationsSecondaryTests.forEach {
let parsedResult = recursiveDescentParser($0, blackList: ["index", "row"])
if whatWeWantToSee2 != parsedResult {
print("ERROR! Bad Result: \(parsedResult) for \($0)")
} else {
// print("\($0) Passes!")
}
}
// Test Set 3
jsonInterpretations.forEach {
let parsedResult = recursiveDescentParser($0, whiteList: ["index", "row"])
if !parsedResult.isEmpty, whatWeWantToSee3 != parsedResult {
print("ERROR! Bad Result: \(parsedResult) for \($0)")
} else {
// print("\($0) Passes!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment