1

The issue is with the following class:

[DebuggerDisplay("{Kind}: {Identifier}")]
public class SocialConnection
{
    public virtual Guid UniqueId
    {
        get { return Id; }
        set { Id = value; }
    }

    // SocialConnectionKind is an enumeration
    public virtual SocialConnectionKind Kind { get; set; }

    public virtual string Identifier { get; set; }
}

Kind property never gets serialized: when I request an object which has an associated SocialConnection I never get the whole property.

BTW, if I manually call JsonConvert.SerializeObject it gets serialized. It should be something with the default media-type formatter but I can't figure out the solution so far.

7
  • Please paste the expected and current result, to have more clarity. I have understood, you Json Serialize SocialConnection object and do not receive SocialConnectionKind enum, which otherwise get serialized on JsonConvert.SerializeObject Commented Apr 6, 2016 at 10:06
  • @MrinalKamboj You've understood correctly Commented Apr 6, 2016 at 10:06
  • @MrinalKamboj BTW I believe the problem is very stupid, I'm about to check the reason behind the issue Commented Apr 6, 2016 at 10:07
  • It indeed looks like a very strange issue :) Commented Apr 6, 2016 at 10:07
  • @MrinalKamboj I've found what was causing the issue, I'm going to answer myself... you'll laugh a lot ;P Commented Apr 6, 2016 at 10:08

1 Answer 1

4

The issue which was causing this serialization problem was very simple. Check SocialConnectionKind enumeration:

public enum SocialConnectionKind
{
    Skype,
    Facebook,
    Twitter,
    LinkedIn,
    Hangouts
}

Did you already notice what could be the problem? The issue wouldn't be reproduced if the value would be any excepting Skype!

Why? Enumerations start with 0 and see how I've configured my WebAPI's HttpConfiguration:

config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling =
                                                           DefaultValueHandling.Ignore;

Which is the default value of the Enum default underlying type int? Yes, it's 0.

So, what solved the issue?

public enum SocialConnectionKind
{
    Skype = 1, // <--- The issue was solved starting the enumeration from 1!
    Facebook,
    Twitter,
    LinkedIn,
    Hangouts
}

Another approach

As @Avner Shahar-Kashtan have pointed out in some comment, I could also solve this issue using [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)] attribute:

[DebuggerDisplay("{Kind}: {Identifier}")]
public class SocialConnection
{
    public virtual Guid UniqueId
    {
        get { return Id; }
        set { Id = value; }
    }

    // SocialConnectionKind is an enumeration
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
    public virtual SocialConnectionKind Kind { get; set; }

    public virtual string Identifier { get; set; }
}

...and this way there's no need of starting an enumeration from 1.

Anyway, in my particular case, I prefer to stay with the start from 1 approach, because I find cleaner avoid polluting my POCOs with serialization-specific attributes because SocialConnection class lives in a shared library and this serialization issue is an implementation issue in a concrete project.

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

11 Comments

Interesting scenario. But wouldn't the deserializer have similar settings, and initialize the property to 0/Skype if the value wasn't included? Or is the deserializing code not C#/.NET, so you don't actually have the SocialConnection class there.
@AvnerShahar-Kashtan Well, in that case, if I'm not mistaken, it would set the value, the whole setting is specific to serializing.
@AvnerShahar-Kashtan Ah, you're right. For now, I'm testing my controllers using C# integration tests (VSTest), BTW, the consumers will be Web browsers so it wouldn't receive 0 and, using JavaScript I would need to check if the property kind is there. I prefer to avoid this scenario and start from 1 to avoid that checking. The social connection must have a kind.
@MatíasFidemraizer Yes, but if the net effect of dropping the default value is 0 - that is, both serializer and deserializer can handle it and preserve data integrity - then it's not a problem but an implementation detail. A confusing and surprising one, true. :)
But if your client doesn't use the same deserializer and environment (i.e. javascript) and can't preserve data integrity with missing data (which it can't), then it certainly becomes a problem. Incidentally, wouldn't it be clearer to simply not Ignore default values, rather than changing your enum values?
|

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.