@@ -106,7 +106,7 @@ public override Task WriteToStreamAsync(System.Type type, object value, Stream w
106106
107107 //writer.Formatting = Formatting.Indented;
108108
109- var root = GetPropertyName ( type , value ) ;
109+ var root = GetJsonKeyForType ( type , value ) ;
110110
111111 writer . WriteStartObject ( ) ;
112112 writer . WritePropertyName ( root ) ;
@@ -155,7 +155,9 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
155155 var idProp = ModelManager . Instance . GetIdProperty ( value . GetType ( ) ) ;
156156 writer . WriteValue ( GetValueForIdProperty ( idProp , value ) ) ;
157157
158- PropertyInfo [ ] props = value . GetType ( ) . GetProperties ( ) ;
158+ // Leverage the cached map to avoid another costly call to GetProperties()
159+ PropertyInfo [ ] props = ModelManager . Instance . GetPropertyMap ( value . GetType ( ) ) . Values . ToArray ( ) ;
160+
159161 // Do non-model properties first, everything else goes in "links"
160162 //TODO: Unless embedded???
161163 IList < PropertyInfo > modelProps = new List < PropertyInfo > ( ) ;
@@ -293,8 +295,9 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
293295 if ( lt == null )
294296 throw new JsonSerializationException (
295297 "A property was decorated with SerializeAs(SerializeAsOptions.Link) but no LinkTemplate attribute was provided." ) ;
296- string link = String . Format ( lt , objId ,
297- value . GetType ( ) . GetProperty ( "Id" ) . GetValue ( value , null ) ) ;
298+ string link = String . Format ( lt , objId ,
299+ GetIdFor ( value ) ) ; //value.GetType().GetProperty("Id").GetValue(value, null));
300+
298301 //writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
299302 writer . WriteStartObject ( ) ;
300303 writer . WritePropertyName ( "href" ) ;
@@ -399,7 +402,7 @@ protected void SerializeLinkedResources(Stream writeStream, JsonWriter writer, J
399402 foreach ( KeyValuePair < Type , KeyValuePair < JsonWriter , StringWriter > > apair in writers )
400403 {
401404 apair . Value . Key . WriteEnd ( ) ; // close off the array
402- writer . WritePropertyName ( GetPropertyName ( apair . Key ) ) ;
405+ writer . WritePropertyName ( GetJsonKeyForType ( apair . Key ) ) ;
403406 writer . WriteRawValue ( apair . Value . Value . ToString ( ) ) ; // write the contents of the type JsonWriter's StringWriter to the main JsonWriter
404407 }
405408
@@ -422,7 +425,7 @@ private object ReadFromStream(Type type, Stream readStream, HttpContent content,
422425 {
423426 object retval = null ;
424427 Type singleType = GetSingleType ( type ) ;
425- var pripropname = GetPropertyName ( type ) ;
428+ var pripropname = GetJsonKeyForType ( type ) ;
426429 var contentHeaders = content == null ? null : content . Headers ;
427430
428431 // If content length is 0 then return default value for this type
@@ -749,22 +752,10 @@ private object DeserializePrimitive(Type type, JsonReader reader)
749752
750753 #endregion
751754
752- private string GetPropertyName ( Type type , dynamic value = null )
755+ //TODO: Could be expensive, and is called often...move to ModelManager and cache result?
756+ private string GetJsonKeyForType ( Type type , dynamic value = null )
753757 {
754- if ( IsMany ( type ) )
755- type = GetSingleType ( type ) ;
756-
757- var attrs = type . CustomAttributes . Where ( x => x . AttributeType == typeof ( Newtonsoft . Json . JsonObjectAttribute ) ) . ToList ( ) ;
758-
759- string title = type . Name ;
760- if ( attrs . Any ( ) )
761- {
762- var titles = attrs . First ( ) . NamedArguments . Where ( arg => arg . MemberName == "Title" )
763- . Select ( arg => arg . TypedValue . Value . ToString ( ) ) . ToList ( ) ;
764- if ( titles . Any ( ) ) title = titles . First ( ) ;
765- }
766-
767- return FormatPropertyName ( this . PluralizationService . Pluralize ( title ) ) ;
758+ return ModelManager . Instance . GetJsonKeyForType ( type , this . PluralizationService ) ;
768759 }
769760
770761 //private string GetPropertyName(Type type)
@@ -780,14 +771,14 @@ private bool IsMany(dynamic value = null)
780771 return false ;
781772 }
782773
783- private bool IsMany ( Type type )
774+ internal static bool IsMany ( Type type )
784775 {
785776 return
786777 type . IsArray ||
787778 ( type . GetInterfaces ( ) . Contains ( typeof ( IEnumerable ) ) && type . IsGenericType ) ;
788779 }
789780
790- private Type GetSingleType ( Type type ) //dynamic value = null)
781+ internal static Type GetSingleType ( Type type ) //dynamic value = null)
791782 {
792783 if ( IsMany ( type ) )
793784 if ( type . IsGenericType )
0 commit comments