Search code examples
c#javascriptserializerscriptignore

How to make JavaScriptSerializer ignore [ScriptIgnore] tags?


I need to make JavaScriptSerializer ignore [ScriptIgnore] tags. I think I can do this with a custom JavaScriptConverter. However, I am not so sure. All I need to do is ignore the attribute, and the rest can be done with the base definition. You can see the code for JavaScriptSerializer here.

I am worried that implementing a custom converter, such as this example will end up not properly serializing a complex custom object graph.

Looking back to Microsoft's code for JavaScriptSerializer, on line 252-253 the code:

// Ignore all fields marked as [ScriptIgnore]
if (fieldInfo.IsDefined(typeof(ScriptIgnoreAttribute), true /*inherits*/)) 
 continue;

is the only part that I really need to bypass. I just need to have that line of code commented out. Unfortunately, that isn't really possible, so I need some method to be able to just ignore that one part of the whole process.

How can I ignore that line of code, or somehow provide a custom implementation which does not ignore [ScriptIgnore] attributes?


Solution

  • I ended up having to mimic the JavaScriptSerializer class and provide some customization inside of the mimic'd class. Not sure I would suggest doing it, or that it is very readable. On the other hand, it works, and I love it.