0

I have two classes which implement an interface IDownloadable. I get a list of jsons which contain BOTH classes randomized. I want to filter all T's out of that list, but the problem is that the serialization always "works" because of that interface. Here is my code so far (broken down):

public List<T> GetObjectsFromList<T>(List<string> list)where T:IDownloadable{
    var list = new List<T>();
    foreach(string s in list){
        T t = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
        if(t is T && t != null)
            list.Add(T);
    }
}

Is there a way to say that the serialization always has to use all objects from the json? EG:

Json:

{
"Test":1,
"Bla": 2
}

Class:

class A{
   int Test;
}

class B{
   int Test;
   int Bla;
}

_

//this should not work because we dont use "Bla" from the json
A a = Newtonsoft.Json.JsonConvert.DeserializeObject<A>(json);

//this should work because we use every object from the json
B b = Newtonsoft.Json.JsonConvert.DeserializeObject<B>(json);

PS: i have no problem switching to a different json serializer

Loading
  • 1,098
  • 1
  • 12
  • 25

0 Answers0