Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonIgnore Attribute #7

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

Add JsonIgnore Attribute #7

wants to merge 2 commits into from

Conversation

paulmoore
Copy link

I've added an attribute which can be applied to fields and properties to ignore them during JSON serialization and deserialization. Using [JsonIgnore(JsonIgnoreWhen.Serializing)] will not serialize the field but still deserialize data into it. [JsonIgnore(JsonIgnoreWhen.Deserializing)] will serialize the field but not deserialize it. The default [JsonIgnore] ignores the field during serialization and deserialization.

I've also added some initial support for importing/exporting floats and signed long ints. I did this because LitJson works very well with Unity which uses floats for most floating-point numbers.

Unit tests also included.

When JsonIgnore is applied to a field or property, that member is
ignored when deserializing and/or serializing.
You can specify JsonIgnoreWhen.Serializing to only ignore
serialization, *.Deserializing to only ignore deserializing, or default
which is to ignore the field either way.
Added unit tests for JsonIgnore, mroe to come.
Updated Makefile.
Updated .gitignore to include debug and OSX files.
Added float to the base exporter table.
@michaelbartnett
Copy link
Contributor

I like this IgnoreWhenSerializing/IgnoreWhenDeserializing. Any way to consolidate it with d141017 ?

@oddgames-david
Copy link

If you want to use this with the latest master copy the JsonAttribute.cs and then use this as your JsonMapper.cs: https://gist.github.com/lbv/87aa07d4f187743ef8ad

Edit: Moved the code into a gist.

@NVentimiglia
Copy link

This pull is a must.

In my home version I also have a [JsonStrict] and [JsonInclude] attributes. I did this for cases for instances when I was inherent from a base class I had no control over. This allowed me to blacklist all properties and fields (strict) and then white list the properties I wanted (include). Would you mind adding this to your pull ?

@lbv
Copy link
Collaborator

lbv commented Aug 15, 2014

Hello,

Sorry for the long silence.

I like the idea of this request, especially if people find it useful in common scenarios, as illustrated by @NVentimiglia.

My only question is about the name of the attributes. Are the JsonIgnore/JsonIgnoreWhen/JsonInclude symbols used in other JSON libraries?

Please bear with me, as it's been a long time I've coded in C# and followed the development of common JSON libraries for different languages. But if there is some type of consensus among JSON library writers, I'd be happy to follow it. I want to make LitJSON easy to use and a good community player.

@NVentimiglia
Copy link

Im currently using This variant of LITJson. It includes a JsonIgnore annotation.

https://github.com/VictorySquare/UnityLitJson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants