I have a struct that I describe in a JSON file, say the struct is named Dog
struct Dog {
var color: UIColor
}
I'm keeping track of my dogs in a dictionary in a JSON file where each dog's name act as an identifier. So I could have the next JSON:
"myDogs": {
"goodBoy": {
"color": "#000000"
},
"veryGoodBoy": {
"color": "#FFFFFF
}
}
To descrive my 2 dogs, it being a dictionary I (obviously) want to avoid duplicate keys, so I would expect that when I decode myDogs
that is of type [String: Dog]
if the JSON has 2 dogs with the same key (name) I would get some error.
To my surprise it's not happening, instead it just ignores all the dogs after the first one, i.e for this JSON
"myDogs": {
"goodBoy": {
"color": "#000000"
},
"veryGoodBoy": {
"color": "#FFFFFF
}
"goodBoy": {
"color": "#FF0000"
}
}
I would get a dictionary with 2 dogs, one name "goodBoy" with black color, and another named "veryGoodBoy" with white color.
Is there a way to raise an exception (or at least log an error) in the decoding stage?
Thanks in advance.
Edit
Does JSON syntax allow duplicate keys in an object? helps but it doesnt answer my question.
Basically I'm asking is there a way in Swift to raise an exception in the decoding stage without rewriting JSONDecoder()