Skip to content

Commit 49d45e4

Browse files
committed
Initial implementation of GetPropertyMap, no tests yet.
1 parent b5a7813 commit 49d45e4

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

JSONAPI/Core/ModelManager.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using JSONAPI.Json;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Reflection;
@@ -32,6 +33,11 @@ private Lazy<Dictionary<Type, PropertyInfo>> _idProperties
3233
() => new Dictionary<Type, PropertyInfo>()
3334
);
3435

36+
private Lazy<Dictionary<Type, Dictionary<string, PropertyInfo>>> _propertyMaps
37+
= new Lazy<Dictionary<Type, Dictionary<string, PropertyInfo>>>(
38+
() => new Dictionary<Type, Dictionary<string, PropertyInfo>>()
39+
);
40+
3541
#endregion
3642

3743
#region Id property determination
@@ -57,5 +63,29 @@ public PropertyInfo GetIdProperty(Type type)
5763
}
5864

5965
#endregion
66+
67+
#region Property Maps
68+
69+
public Dictionary<string, PropertyInfo> GetPropertyMap(Type type)
70+
{
71+
Dictionary<string, PropertyInfo> propMap = null;
72+
73+
var propMapCache = _propertyMaps.Value;
74+
75+
if (propMapCache.TryGetValue(type, out propMap)) return propMap;
76+
77+
propMap = new Dictionary<string, PropertyInfo>();
78+
PropertyInfo[] props = type.GetProperties();
79+
foreach (PropertyInfo prop in props)
80+
{
81+
propMap[JsonApiFormatter.FormatPropertyName(prop.Name)] = prop;
82+
}
83+
84+
propMapCache.Add(type, propMap);
85+
86+
return propMap;
87+
}
88+
89+
#endregion
6090
}
6191
}

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,9 @@ private object ReadFromStream(Type type, Stream readStream, HttpContent content,
549549
public object Deserialize(Type objectType, Stream readStream, JsonReader reader, JsonSerializer serializer)
550550
{
551551
object retval = Activator.CreateInstance(objectType);
552-
PropertyInfo[] props = objectType.GetProperties();
553-
554-
//TODO: This could get expensive...cache these maps per type, so we only build the map once?
555-
IDictionary<string, PropertyInfo> propMap = new Dictionary<string, PropertyInfo>();
556-
foreach (PropertyInfo prop in props)
557-
{
558-
propMap[FormatPropertyName(prop.Name)] = prop;
559-
}
560552

553+
IDictionary<string, PropertyInfo> propMap = ModelManager.Instance.GetPropertyMap(objectType);
554+
561555
if (reader.TokenType != JsonToken.StartObject) throw new JsonReaderException(String.Format("Expected JsonToken.StartObject, got {0}", reader.TokenType.ToString()));
562556
reader.Read(); // Burn the StartObject token
563557
do
@@ -640,13 +634,7 @@ private void DeserializeLinkedResources(object obj, Stream readStream, JsonReade
640634
//reader.Read();
641635
if (reader.TokenType != JsonToken.StartObject) throw new JsonSerializationException("'links' property is not an object!");
642636

643-
//TODO: Redundant, already done in Deserialize method...optimize?
644-
PropertyInfo[] props = obj.GetType().GetProperties();
645-
IDictionary<string, PropertyInfo> propMap = new Dictionary<string, PropertyInfo>();
646-
foreach (PropertyInfo prop in props)
647-
{
648-
propMap[FormatPropertyName(prop.Name)] = prop;
649-
}
637+
IDictionary<string, PropertyInfo> propMap = ModelManager.Instance.GetPropertyMap(obj.GetType());
650638

651639
while (reader.Read())
652640
{
@@ -809,6 +797,7 @@ private Type GetSingleType(Type type)//dynamic value = null)
809797
return type;
810798
}
811799

800+
//TODO: Should this move to ModelManager? Could be cached there to improve performance?
812801
public static string FormatPropertyName(string propertyName)
813802
{
814803
string result = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);

0 commit comments

Comments
 (0)