Reputation: 64933
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.
Upvotes: 1
Views: 115
Reputation: 64933
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
}
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.
Upvotes: 4