this is a kind of data i have to de-serialize
{
"id": "M:11427",
"title": "DAX30",
"nextStartId": "S:727831",
"sections": [
{
"type": "HIGHLIGHTS",
"baseResults": [ values of type highlights ],
"hasMore": true
},
{
"type": "CHART",
"hasMore": false,
"chartTypes": [ string values ]
},
{
"type": "TWEETS",
"baseResults": [ values of type tweets ],
"hasMore": true
}]
}
I have to serialize & deserialize them all. I want to create something that can hold the values corresponding to baseResults.
there is a main class that represent the whole json
class Data
{
...
ObservableCollection<Section> sections{get;set;}
...
}
then there is a class that represents the data in sections array of main json
class Section
{
string type{get;set;}// thi elements decides what kind of data would be in baseResults
dynamic baseResults{get;set;} //it should hold ObservableCollection of either Highlights or Tweets etc.
}
base class for type of data coming in baseResults array is an abstract class class CategoryData
and its children are class Highlights
& class Tweets
I am using dynamic
since I can not assign an ObservableCollection<CategoryData>
with ObservableCollection<Highlights>
. But I don't want to use this dynamic or object type instead I want something relevant. please suggest me what could be a better approach for this problem.
Best Answer
If I'm understanding you correctly, you're trying to find a statically-typed way to say "a value of type
Section
can be either a value of typeHighlights
,Chart
, orTweets
". There are many terms for this, including tagged union, discriminated union, variant, or sum type but they all refer to the same concept. Sum types are a standard feature in statically-typed functional programming languages, but with a bit of ingenuity you can roll your own in C#. Using that answer as a template, and addingsealed
to make sure no one adds any extra subclasses, yourSection
type would look something like this:With that in place, you can declare variables of type
Section
, and act on them by passing 3 functions to theMatch
methods:You could just as easily have a collection of
Section
s and operate on them that way when you iterate over it.