1

I know of [JsonIgnore] which ignores properties altogether, I know of ShouldSerializePropertyName which gives conditional serialization, but I cannot find anything to mark property to be serialized into JSON normally, but not set during deserialization.

I could write a workaround:

[JsonIgnore]
public string MyValue{get;set;}

public string MyValueForJson {get{return MyValue;}

but it is a last resort. Is there some way - other than custom converters etc. - to express that I don't want that field to be populated during deserialization?

2
  • As a side note: I decided to actually go with separate models for serialization/deserialization for various reasons, so the question is no longer important - however, I think that it's still a good question :) Commented Jul 30, 2015 at 13:11
  • Weird that we asked for this on the same day: stackoverflow.com/questions/31731320/… I didn't see yours until after I typed mine up but I haven't found anyone else asking this. Commented Jul 30, 2015 at 18:48

2 Answers 2

1

As I understand you want to serialize object with all properties in json string but retrieve only selected properties while deserialization on that string.

If this is case then, I had a similar requirement where I created BaseType and DerivedType classes then I serialize derived type into Json string while deserialization I want it in Base type instance. So wrote this code :

    using System.Web.Script.Serialization;

    public static TB CastToBase<T, TB>(this T derivedTypeInstance) 
        {
            var serializer = new JavaScriptSerializer();
            var baseTypeInstance = serializer.Deserialize<TB>(serializer.Serialize(derivedTypeInstance));
            return baseTypeInstance;
        } 
Sign up to request clarification or add additional context in comments.

1 Comment

I like it - even though it is kind of an stretch as to what should the class hierarchy be used for ;)
0

I think you can put [JsonObject(MemberSerialization.OptIn)] property on the class, which requires to explicitly state which properties to serialize. Then you can use both [JsonProperty] and [JsonIgnore] on a property you'd like to serialize as normal but ignore on deserialization.

Example:

    [JsonObject(MemberSerialization.OptIn)]
    private class UserData
    {
        [JsonProperty]      
        public string Token { get; set; }

        [JsonProperty]
        public string Username { get; set; }

        [JsonIgnore]
        [JsonProperty]
        public string Password { get; set; }
    }

3 Comments

I don't believe this will work. The documentation (newtonsoft.com/json/help/html/…) states: OptOut - All public members are serialized by default. Members can be excluded using JsonIgnoreAttribute or NonSerializedAttribute. OptIn - Only members must be marked with JsonPropertyAttribute or DataMemberAttribute are serialized.
Check out the OptIn attribute in the documentation, thats the attribute I used
Does this work? I thought JsonIgnore was for OptOut and JsonProperty was used for OptIn, but either way those attributes affect both serialization and deserialization, not just one side of the coin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.