mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-01 18:35:35 +00:00
Allow [Parameter] to control JSON Field Names.
Previously, SimpleJsonSerializer.MapClrMemberNameToJsonFieldName special cased the name "Links" since while that was the name of the property in the object model, in JSON "_links" was used instead. It turns out that there was an additional problem, where GitReference wants to expose as Repository, but the name in JSON responses is "repo". Instead of simply adding another special case to MapClrMemberNameToJsonFieldName, we update the implementation of the serializer to allow [Parameter(Key = "some_name")] to denote what name we'd like to use for the field in the JSON object when we serialize.
This commit is contained in:
@@ -135,7 +135,7 @@ namespace Octokit.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IgnoresUnderscore()
|
||||
public void RespectsParameterKeyName()
|
||||
{
|
||||
const string json = "{\"_links\":\"blah\"}";
|
||||
|
||||
@@ -151,6 +151,7 @@ namespace Octokit.Tests
|
||||
public string FirstName { get; set; }
|
||||
public bool IsSomething { get; set; }
|
||||
public bool Private { get; set; }
|
||||
[Parameter(Key = "_links")]
|
||||
public string Links { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +26,17 @@ namespace Octokit.Internal
|
||||
readonly List<string> _membersWhichShouldPublishNull
|
||||
= new List<string>();
|
||||
|
||||
protected override string MapClrMemberNameToJsonFieldName(string clrPropertyName)
|
||||
protected override string MapClrMemberToJsonFieldName(MemberInfo member)
|
||||
{
|
||||
var rubyCased = clrPropertyName.ToRubyCase();
|
||||
if (rubyCased == "links") return "_links"; // Special case for GitHub API
|
||||
return rubyCased;
|
||||
var memberName = member.Name;
|
||||
var paramAttr = member.GetCustomAttribute<ParameterAttribute>();
|
||||
|
||||
if (paramAttr != null && !string.IsNullOrEmpty(paramAttr.Key))
|
||||
{
|
||||
memberName = paramAttr.Key;
|
||||
}
|
||||
|
||||
return memberName.ToRubyCase();
|
||||
}
|
||||
|
||||
internal override IDictionary<string, ReflectionUtils.GetDelegate> GetterValueFactory(Type type)
|
||||
@@ -53,7 +59,7 @@ namespace Octokit.Internal
|
||||
var attribute = propertyInfo.GetCustomAttribute<SerializeNullAttribute>();
|
||||
if (attribute == null)
|
||||
continue;
|
||||
_membersWhichShouldPublishNull.Add(fullName + MapClrMemberNameToJsonFieldName(propertyInfo.Name));
|
||||
_membersWhichShouldPublishNull.Add(fullName + MapClrMemberToJsonFieldName(propertyInfo));
|
||||
}
|
||||
foreach (var fieldInfo in ReflectionUtils.GetFields(type))
|
||||
{
|
||||
@@ -62,7 +68,7 @@ namespace Octokit.Internal
|
||||
var attribute = fieldInfo.GetCustomAttribute<SerializeNullAttribute>();
|
||||
if (attribute == null)
|
||||
continue;
|
||||
_membersWhichShouldPublishNull.Add(fullName + MapClrMemberNameToJsonFieldName(fieldInfo.Name));
|
||||
_membersWhichShouldPublishNull.Add(fullName + MapClrMemberToJsonFieldName(fieldInfo));
|
||||
}
|
||||
|
||||
return base.GetterValueFactory(type);
|
||||
@@ -89,7 +95,7 @@ namespace Octokit.Internal
|
||||
continue;
|
||||
}
|
||||
|
||||
jsonObject.Add(MapClrMemberNameToJsonFieldName(getter.Key), value);
|
||||
jsonObject.Add(getter.Key, value);
|
||||
}
|
||||
}
|
||||
output = jsonObject;
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using Octokit.Internal;
|
||||
|
||||
namespace Octokit
|
||||
{
|
||||
@@ -45,6 +46,7 @@ namespace Octokit
|
||||
/// <summary>
|
||||
/// List of feed urls including feed url and feed type, e.g. application/atom+xml
|
||||
/// </summary>
|
||||
[Parameter(Key = "_links")]
|
||||
public FeedLinks Links { get; set; }
|
||||
|
||||
internal string DebuggerDisplay
|
||||
|
||||
Reference in New Issue
Block a user