-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDefaultNamingConventions.cs
More file actions
72 lines (64 loc) · 2.49 KB
/
Copy pathDefaultNamingConventions.cs
File metadata and controls
72 lines (64 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Linq;
using System.Reflection;
using JSONAPI.Extensions;
using Newtonsoft.Json;
namespace JSONAPI.Core
{
/// <summary>
/// Default implementation of INamingConventions
/// </summary>
public class DefaultNamingConventions : INamingConventions
{
private readonly IPluralizationService _pluralizationService;
/// <summary>
/// Creates a new DefaultNamingConventions
/// </summary>
/// <param name="pluralizationService"></param>
public DefaultNamingConventions(IPluralizationService pluralizationService)
{
_pluralizationService = pluralizationService;
}
/// <summary>
/// This method first checks if the property has a [JsonProperty] attribute. If so,
/// it uses the attribute's PropertyName. Otherwise, it falls back to taking the
/// property's name, and dasherizing it.
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
public virtual string GetFieldNameForProperty(PropertyInfo property)
{
var jsonPropertyAttribute = (JsonPropertyAttribute)property.GetCustomAttributes(typeof(JsonPropertyAttribute)).FirstOrDefault();
return jsonPropertyAttribute != null ? jsonPropertyAttribute.PropertyName : property.Name.Dasherize();
}
/// <summary>
/// This method first checks if the type has a [JsonObject] attribute. If so,
/// it uses the attribute's Title. Otherwise it falls back to pluralizing the
/// type's name using the given <see cref="IPluralizationService" /> and then
/// dasherizing that value.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public virtual string GetResourceTypeNameForType(Type type)
{
var jsonObjectAttribute = type.GetCustomAttributes().OfType<JsonObjectAttribute>().FirstOrDefault();
string title = null;
if (jsonObjectAttribute != null)
{
title = jsonObjectAttribute.Title;
}
if (string.IsNullOrEmpty(title))
{
title = GetNameForType(type);
}
return _pluralizationService.Pluralize(title).Dasherize();
}
/// <summary>
/// Gets the name for a CLR type.
/// </summary>
protected virtual string GetNameForType(Type type)
{
return type.Name;
}
}
}