Change JsonIgnore dynamically

Mar 21, 2008 at 1:36 PM
Hi everybody
Is there a way to set/remove the jsonignore dynamically? What I'd like
to achieve is to limit the amount of data to be transferred to the
data strictly needed by a given request. If I set JsonIgnore that
field will always be ignored by json serializer even when it's needed;
conversely a property without jsonignore will always be serialized
even when it's not needed. I know I could implement a custom
serializer for but I was wondering if there's a different way to
accomplish this.
Thanks in advance
Apr 19, 2008 at 10:58 PM
I've made some local modifications to JsonIgnore and the serializer so that the JsonIgnore can be conditional. Basically, I've set up a optional property on the attribute that if that value matches the value of the member being serialized, then it ignores the value. An example:

public bool AnimCollapse { get; set; }

So, in this case if AnimCollapse was false, it would get ignored. A "true" will get serialized out. This is useful for me with the extJS serialization that I'm doing because in some cases the use their bools as trinaries. Plus its helpful to slim down the JSON to remove items set to default values and such.

James, if you want the code for this, let me know.
Apr 26, 2008 at 12:43 AM
Good idea. I'd be in interested in seeing it.
Apr 26, 2008 at 3:32 PM
Sorry, I'm not using TFS so I couldn't generate a patch properly in a quick manner - I've put a zip file out on my server (3K) that has two files in it. One is the updated JsonIgnoreAttribute class with the added stuff to make the ignore work like I've described. The other file has the two modified methods in Serializer that look at the ignore attribute slightly differently to see whether they should ignore the serialization. The file is here:

It's been working well with me so far, for the ints, bools, and strings that I'm messing with. I realized just a second ago that my switching logic in the JSONIgnore is bogus, because its going to screw up long ints - but that can be fixed easy enough. It's a start anyway, and has GREATLY slimmed down my JSON, which is making it a lot easier to debug too.
Apr 27, 2008 at 10:50 AM
Thanks for the code.

I have been thinking about this some more and the way I might implement this is by using the .NET DefaultValueAttribute and then adding a flag to the JsonSerializer class (UseDefaultValue). When serializing if the property value equals the attribute's default value then don't output the JSON. When deserializing if the JSON for a property is missing then set the property to the attribute's default value.

What do you think?
Apr 27, 2008 at 1:23 PM
I think that sounds ok. My particular case though was more for the serialization that the deserialization. My cases are more around that extJS has a lot of default values on their properties and I didn't want to serialize these huge JSON strings down to the browser that were filled with defaults. So, if a bool would default to false on extJS, I didn't want to bother sending it down if it was false back in the .NET.

Your suggestion is the other half. Right now I'm just setting the default values in the object's initializer. Though, now that I think about it, I'm not sure how much UseDefaultValue would help me actually, because the initalizer is going to load the default values, then the deserialization is going to run, which will just overwrite the default values I've already put in there. Since I need the default values for more than just when I'm using JSON, I'm not sure it would give me any benefit...

But, it would still be a good feature to have I think. If a dev has an object that is only loaded in JSON serialization, then it might save them a step.
Dec 29, 2008 at 10:00 AM
Hi everybody
Any news about this feature? Has it been implemented in new relesases of the library?

Dec 30, 2008 at 7:10 AM
Jan 4, 2009 at 8:52 AM