Last active
August 29, 2015 14:21
-
-
Save hagbarddenstore/c7f05a7bb166a05534d9 to your computer and use it in GitHub Desktop.
The question is, how to parse JSON which contains a defined set of types, but allowing multiple types in an array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
protected Animal(string type) | |
{ | |
Type = type; | |
} | |
public string Type { get; private set; } | |
} | |
class Dog : Animal | |
{ | |
public Dog(string name) : base("Dog") | |
{ | |
Name = name; | |
} | |
public string Name { get; private set; } | |
} | |
class Cat : Animal | |
{ | |
public Cat(string name) : bsae("Cat") | |
{ | |
Name = name; | |
} | |
public string Name { get; private set; } | |
} | |
class JsonObject | |
{ | |
public Animal[] Animals { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"animals": [ | |
{ | |
"type": "Dog", | |
"name": "Buster" | |
}, | |
{ | |
"type": "Cat", | |
"name": "Lucy" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Animal struct { | |
Type string `json:"type"` | |
} | |
type Cat struct { | |
Animal | |
Name string `json:"name"` | |
} | |
type Dog struct { | |
Animal | |
Name string `json:"name"` | |
} | |
type JsonObject struct { | |
Animals []??? // Which type should I use? Doesn't compile with "Animal" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment