0

I have a variety of MVC view models to work with and each one contains a lot of information about the pages they generate. I need to take much of this information out of the view models and use a framework I wrote that makes it available client-side.

Unfortunately, when I try to serialize an entire view model to JSON, Newtonsoft returns empty-handed. So as a work-around, I have been manually building an object with an implicit type so that the information will get serialized to JSON.

However, this is tedious, and as such, I would like a simple way to iterate over all the members of the view model (or any other object for that matter) and transfer just the regular system types (strings, booleans, integers, and arrays of) etc.. over to a dictionary of string, object.

This is proving more difficult than I thought because I cannot seem to check the type property of its members to see if the object member is any one of those generic types or not. Is there an easy way to do that?

(BTW, I would like to avoid having to tag all the models with serialization attributes. There are too many of them.)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Joshua Dannemann
  • 2,003
  • 1
  • 14
  • 34
  • 1
    Can we get an example of a model to see why the JSON serializer isn't working? Maybe its easier to fix why, than to work around. – Ron Beyer May 07 '15 at 23:46
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 07 '15 at 23:50
  • @Ron I already know the reason why it will not serialize... Most of the members of the model are set up as properties. I could set the JSON serialization attribute, but then I would have to modify every model, and there are a whole bunch of them. That's why I'm trying to come up with a function to iterate over an instance of any class no matter what it is. – Joshua Dannemann May 07 '15 at 23:55
  • Thank Ziv, I will check if that works in the morning. From what see though, that procedure on the possible duplicate is not recursive. Basically, I need a reliable way using reflection to check if an item is one with members, or something like a string. If it has members, the function will recurse, if it's a string, integer, list of strings, etc.. then it adds the name and value to a dictionary. – Joshua Dannemann May 08 '15 at 00:02
  • @john Thanks. I did not know not to put tags in the title... Been doing it for years actually... – Joshua Dannemann May 08 '15 at 00:07
  • Actually, I only found [Performance Hits for Caching Data in Memory (ASP.NET)](http://stackoverflow.com/questions/29640752/performance-hits-for-caching-data-in-memory-asp-net) with a tag in the title ("(ASP.NET)"). – John Saunders May 08 '15 at 00:15
  • My others were probably edited, and I also had a different account I used to write my questions under. This one is relatively new. – Joshua Dannemann May 08 '15 at 00:59

0 Answers0