24

I'm using JSON.NET 6.0.1. When I use the SerializeObject method to serialize an object of my derived class, it serializes properties from base class only. Here is the code snippet:

string v = JsonConvert.SerializeObject(
                service, 
                Formatting.Indented, 
                new JsonSerializerSettings()
                {
                    TypeNameHandling = TypeNameHandling.All
                });

base class:

[DataContract]
public abstract partial class DataEntity : IDataEntity, INotifyPropertyChanging, INotifyPropertyChanged
{
    ...
}

derived class:

[Table(Name = "dbo.mytable")]
public sealed class mytable : DataEntity
{
    ...
}

Am I missing something?

3 Answers 3

32

Yes, you are missing the [DataContract] attribute on the derived class. You also need to add [DataMember] to any properties or fields that you want serialized, if you haven't already added them. Json.Net was changed in version 5.0 release 1 (April 2013) such that the [DataContract] attribute is not inherited.

Note that if you remove all instances of [DataContract] and [DataMemeber] from your classes, Json.Net behaves differently: in that case, the default behavior is for Json.Net to serialize all public properties, both in the base and derived classes.

Sign up to request clarification or add additional context in comments.

8 Comments

Is there any way to change this behavior?
@JoelFan You'll have to be more specific.
I want to be able to tell JsonConvert.SerializeObject to serialize all the properties, regardless of any attributes. I have a class that derives from a class marked [DataContract], but my derived class is not marked. As a result, only the base class's properties are being serialized.
@JoãoPortela I've removed the quote since it seems to be causing confusion for people. The answer is still useful without it.
|
4

Adding the attribute [JsonObject(MemberSerialization.OptOut)] to your derived class will include all its public members to be serialized.

[Table(Name = "dbo.mytable")]
[JsonObject(MemberSerialization.OptOut)]
public sealed class mytable : DataEntity
{
    ...
}

Alternatively, if you only want certain properties of your derived class to be serialized you can add the attribute [JsonProperty] to each one (This would be equivalent of adding [DataMember] to each property along with [DataContract] on the class).

1 Comment

Fantastic @Ray, thanks! I don't understand why did is not the accepted/most upvoted answer, as it is well commented on the web that TypeNameHandling.All brings security issues. Just a side note, if your derived class is Internal, just like mine, the [JsonObject(MemberSerialization.OptOut)] will not work. You'll have to use the [JsonProperty] on each base class member. It works just fine though!
-2

JsonConvert.SerializeObject was only return {} for me. I found that I needed to add a new constructor to the class before it serialized properly.

Comments

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.