0

Base class contains a property that is hide by a new property in sub class but of different type. when deserializing, i am facing issue. I cann't change the name of the property in c# or json but can add namespace if possible.

namespace xyz
{
    public class A
    {
        public ICollection<xyz.OrganizationAttribute> OrganizationAttributes { get; set; }
    }
}

namespace pqr
{
    public class AX : A
    {
        public new ICollection<pqr.OrganizationAttribute> OrganizationAttributes { get; set; }
    }
}

Update:

    JsonConvert.SerializeObject( axObject, new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.Objects,
        ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
        MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead,
        DefaultValueHandling = DefaultValueHandling.Ignore
    } );

    JsonConvert.DeserializeObject<A>( "axjsonString", new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
    } );

Any help is appreciated. Thanks

Irshad Ali
  • 1,153
  • 1
  • 13
  • 39
  • How do you deserialize? Usually if you cast the resulting object to AX C# will automatically creates the property of the descendant type. Post your deserialization mechanism here – Emad Nov 13 '16 at 11:42
  • @Emad I am deserializing AX json to A object using NewtonSoft with ReadAHead setting. var a = result.Documents?.Select( x => x.ToObject( new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead } ) ); – Irshad Ali Nov 13 '16 at 12:34
  • 1) Can you modify the base class `A` in any way? 2) Do you both the `xyz.OrganizationAttribute` and `pqr.OrganizationAttribute` collections to appear in the file, or just the derived collection? 3) If your type `AX` ignores the `xyz` collection, that would seem to violate the [Liskov Substitution Principle](https://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle). Are you sure you want to do that? – dbc Nov 13 '16 at 20:08
  • Also, have you tested this? According to [this answer](https://stackoverflow.com/questions/9304096/trying-to-hide-base-class-member-during-serialization-with-json-net/9315113#9315113) it should just work as of Json.NET v4.0.6.0. – dbc Nov 13 '16 at 20:10
  • @dbc 1) No, 2) No, I don't have xyz.OrganizationAttribute in json or anywhere. but unfortunately, It is a property in class A. 3) I want to rename pqr.OrganizationAttribute property but can't for now. – Irshad Ali Nov 14 '16 at 06:55
  • Actually, this seems to work perfectly already. See this fiddle: https://dotnetfiddle.net/TR4sfS. Can you explain what the problem is? – dbc Nov 14 '16 at 07:07
  • @IrshadAli - I don't understand what you are doing. Are you serializing an object of type `AX`, then deserializing as type `A` -- I.e. trying to use Json.NET to map an instance of the derived type to an instance of the base type? – dbc Nov 14 '16 at 07:37
  • @dbc, thank you for your dotnetfiddle link, it helped me directing in a right direction. for your second comment, yes. The error was due to presence of "$id": 1 additional property created to find circular reference. – Irshad Ali Nov 14 '16 at 08:16

1 Answers1

0

Based on what you said in the comments there is no way to have 'pqr.OrganizationAttribute' in class A that's not related to JSon or deserialization, that's just how OOP works. Hiding a method or field in a class propagates down in inheritance hierarchy therefore when talking about A you can't talk about fields that it's children like AX has re-defined.

You can read more about this here

Edit

All right I got what you want to do now. Use this:

public static A FromJson(string input) 
{
    var json = JToken.Parse(input);
    json["OrganizationAttribute"].Remove();
    return JsonConvert.DeserializeObject<A>(json.ToString());
}
Emad
  • 3,809
  • 3
  • 32
  • 44
  • Yes you are right, A object cannot have 'pqr.OrganizaitonAttribute'. I just want to ignore OrganizationAttributes when i deserialize AX json to A object. I cannot modify class A. – Irshad Ali Nov 14 '16 at 06:52
  • @IrshadAli see the edit. What you want is to manipulate the JSon. For the record I have to say that it's not a good idea to use JSon created from one model to deserialize into another model. – Emad Nov 14 '16 at 07:15