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?

Gerino
  • 1,943
  • 1
  • 16
  • 21
  • 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 :) – Gerino Jul 30 '15 at 13:11
  • Weird that we asked for this on the same day: http://stackoverflow.com/questions/31731320/serialize-property-but-do-not-deserialize-property-in-json-net I didn't see yours until after I typed mine up but I haven't found anyone else asking this. – RJ Cuthbertson Jul 30 '15 at 18:48

2 Answers2

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;
        } 
Aamol
  • 1,149
  • 1
  • 15
  • 23
  • I like it - even though it is kind of an stretch as to what should the class hierarchy be used for ;) – Gerino Aug 05 '15 at 08:54
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; }
    }
Felix Av
  • 1,254
  • 1
  • 14
  • 22
  • I don't believe this will work. The documentation (http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_MemberSerialization.htm) 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. – RJ Cuthbertson Jul 30 '15 at 18:47
  • Check out the OptIn attribute in the documentation, thats the attribute I used – Felix Av Jul 30 '15 at 19:05
  • 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. – RJ Cuthbertson Jul 30 '15 at 19:11