Member naming when SerializableAttribute is used

Apr 12, 2012 at 12:20 AM

Hello, I noticed that when trying to serialize an instance of a type decorated with the SerializableAttribute member naming is messed up and, specifically, automatic properties name is serialized as the name of the backing field rather than the property name.

Example, a class with a single string automatic property:

- without [Serializable]: {"Message":{"Commentary":"ciao"}

- with [Serializable]: {"Message":{"<Commentary>k__BackingField":"ciao"}

Coordinator
Apr 12, 2012 at 1:59 AM

One option is to place the [JsonObject] attribute on the type. The other option is to disable the serializable attribute globally.

private static readonly DefaultContractResolver Resolver = new DefaultContractResolver
    {
        IgnoreSerializableAttribute = true
    };

[Serializable]
public class SerializableTypeWithAutoProperty
{
    public string AutoProp { get; set; }
}

[Test]
public void Serializable()
{
    SerializableTypeWithAutoProperty value = new SerializableTypeWithAutoProperty();

    string json = JsonConvert.SerializeObject(value, new JsonSerializerSettings
        {
            ContractResolver = Resolver
        });

    Console.WriteLine(json);
}

Apr 12, 2012 at 7:28 AM

Hi James, thanks, really helpful. I had tried playing with the serialization settings but didn't find that one.
Out of curiosity why does the use of the Serializable attribute imply the behavior I described?

On Apr 12, 2012 3:59 AM, "JamesNK" <notifications@codeplex.com> wrote:

From: JamesNK

One option is to place the [JsonObject] attribute on the type. The other option is to disable the serializable attribute globally.

private static readonly DefaultContractResolver Resolver = new DefaultContractResolver
    {
        IgnoreSerializableAttribute = true
    };

[Serializable]
public class SerializableTypeWithAutoProperty
{
    public string AutoProp { get; set; }
}

[Test]
public void Serializable()
{
    SerializableTypeWithAutoProperty value = new SerializableTypeWithAutoProperty();

    string json = JsonConvert.SerializeObject(value, new JsonSerializerSettings
        {
            ContractResolver = Resolver
        });

    Console.WriteLine(json);
}

Read the full discussion online.

To add a post to this discussion, reply to this email (Json@discussions.codeplex.com)

To start a new discussion for this project, email Json@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Coordinator
Apr 12, 2012 at 9:49 AM

Serializable will serialize all public/private fields, i.e. the underlying data.

Apr 12, 2012 at 1:03 PM
Ok I see, maybe this should be documented?

On Thu, Apr 12, 2012 at 11:49, JamesNK <notifications@codeplex.com> wrote:

From: JamesNK

Serializable will serialize all public/private fields, i.e. the underlying data.

Read the full discussion online.

To add a post to this discussion, reply to this email (Json@discussions.codeplex.com)

To start a new discussion for this project, email Json@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Apr 13, 2012 at 1:18 PM

I'm surprised a breaking change was introduced in a bugfix release.

Coordinator
Apr 13, 2012 at 9:44 PM

I've changed IgnoreSerializableAttribute to true by default in release 3 which undoes the breaking change - http://json.codeplex.com/releases/view/85975

If you've already set it to true yourself you can either remove it or leave it in, there will be no difference for Json,NET.