Need help calling the generic method

allanau20
allanau20 used Ask the Experts™
on
Need help calling the generic method.

We have a method 'DeserializeToModel' that takes in [a string (an encoded Json) and a reference type] and deserialized it into the reference type model.

public class DeserializedJsonToModel 
  {
      public T DeserializeToModel<T>(string encodedJson, T model) where T : class, new()
      {
          DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
          settings.UseSimpleDictionaryFormat = true; //This is needed to read in the dictionary in HospitalAffiliations class

          T deserializedModel = new T();
          MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(encodedJson));
          DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedModel.GetType());
          deserializedModel = ser.ReadObject(ms) as T;
          ms.Close();
          return deserializedModel;
      }
  }

Open in new window

 
 The problem is calling the generic method on line 49 in this unit test: 'SuperHero' is a type, which is not valid in the given context      
'SuperHero' is a type, which is not valid in the given context
Here's the test class:
    [TestClass]
    public class DeserializedJsonToModelTest
    {
        private SuperHero hero;
        private DeserializedJsonToModel deserializer;

        [TestInitialize]
        public void Setup()
        {
            hero = new SuperHero()
            {
                FirstName = "Barry",
                LastName = "Allen",
                HeroName = "Flash",
                DayJob = "Investigator"
            };

            deserializer = new DeserializedJsonToModel();
        }

        [TestMethod]
        [TestCategory("Unit"), TestCategory("DeserializedJsonToModel")]
        public void DeserializedJsonToModel_Instantiated_Success()
        {
            //Arrange

            //Act

            //Assert  
            Assert.IsNotNull(deserializer);
            Assert.IsInstanceOfType(deserializer, typeof(DeserializedJsonToModel));
        }

        [TestMethod]
        [TestCategory("Unit"), TestCategory("DeserializedJsonToModel")]
        public void DeserializedJsonToModel_SuperHeroEncodedAsJson_SuperHeroModelIsReturned()
        {
            //Arrange
            string encodedJson = Json.Encode(hero);

            //Act
            var sut = deserializer.DeserializeToModel<SuperHero>(encodedJson, SuperHero);

            //Assert            
            Assert.IsInstanceOfType(sut, typeof(SuperHero));
        }
    }
    public class SuperHero
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string HeroName { get; set; }
        public string DayJob { get; set; }
    }

Open in new window

Any ideas?

TIA!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Was so close; thanks Paul for the quick reply!

Author

Commented:
I cannot see your comment anymnore??
Software Engineer
Top Expert 2011
Commented:
I was rethinking the code, what did you end up with ?

public class DeserializedJsonToModel 
  {
      public T DeserializeToModel<T>(string encodedJson) where T : class, new()
      {
          DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
          settings.UseSimpleDictionaryFormat = true; //This is needed to read in the dictionary in HospitalAffiliations class

          T deserializedModel = new T();
          MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(encodedJson));
          DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedModel.GetType());
          deserializedModel = ser.ReadObject(ms) as T;
          ms.Close();
          return deserializedModel;
      }
  }

Open in new window



 //Act
            var sut = deserializer.DeserializeToModel<SuperHero>(encodedJson);

Open in new window

Author

Commented:
you were correct that the reference type does not need to be passed. So, on the method definition I removed the 'T model' and it worked. Thanks again Paul!
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
No problem. glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial