Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions JSONAPI/Json/JsonApiFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js

// Do the Id now...
writer.WritePropertyName("id");
writer.WriteValue(GetIdFor(value));
var idProp = GetIdProperty(value.GetType());
writer.WriteValue(GetValueForIdProperty(idProp, value));

PropertyInfo[] props = value.GetType().GetProperties();
// Do non-model properties first, everything else goes in "links"
Expand All @@ -146,8 +147,7 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js

foreach (PropertyInfo prop in props)
{
//FIXME: The "id" property might not be named "Id"!
if (FormatPropertyName(prop.Name) == "id") continue; // We did the Id above, don't do it twice!
if (prop == idProp) continue;

if (this.CanWriteTypeAsPrimitive(prop.PropertyType))
{
Expand Down Expand Up @@ -811,10 +811,8 @@ protected PropertyInfo GetIdProperty(Type type)
return type.GetProperty("Id");
}

protected string GetIdFor(object obj)
protected string GetValueForIdProperty(PropertyInfo idprop, object obj)
{
Type type = obj.GetType();
PropertyInfo idprop = GetIdProperty(type);
if (idprop != null)
{
if (idprop.PropertyType == typeof(string))
Expand All @@ -827,6 +825,13 @@ protected string GetIdFor(object obj)
return "NOIDCOMPUTABLE!";
}

protected string GetIdFor(object obj)
{
Type type = obj.GetType();
PropertyInfo idprop = GetIdProperty(type);
return GetValueForIdProperty(idprop, obj);
}

private void WriteIdsArrayJson(Newtonsoft.Json.JsonWriter writer, IEnumerable<object> value, Newtonsoft.Json.JsonSerializer serializer)
{
IEnumerator<Object> collectionEnumerator = (value as IEnumerable<object>).GetEnumerator();
Expand Down