/// <summary> /// Write property utility /// </summary> public void WritePropertyUtil(String propertyName, Object instance, SerializationContext context, bool noSubContext = false) { if (instance == null) { return; } // first write the property if (!String.IsNullOrEmpty(propertyName) && context?.ShouldSerialize(propertyName) == false) { return; } // Instance data if (instance is IdentifiedData) { var identifiedData = instance as IdentifiedData; // Complex type .. allow the formatter to handle this INullTypeFormatter typeFormatter = this.GetFormatter(instance.GetType()); var subContext = noSubContext ? context as NullSerializationContext : new NullSerializationContext(propertyName, this, instance, context); typeFormatter.Serialize(instance as IdentifiedData, subContext); } else if (instance is IList && !instance.GetType().IsArray) { // Classifications? var classifier = this.GetClassifier(instance.GetType().StripNullable()); if (classifier == null) // no classifier { foreach (var itm in instance as IList) { this.WritePropertyUtil(null, itm, new NullSerializationContext(propertyName, this, instance, context as NullSerializationContext), noSubContext); } } else { foreach (var cls in classifier.Classify(instance as IList)) { Object value = new List <Object>(cls.Value as IEnumerable <Object>); if (cls.Value.Count == 1) { value = cls.Value[0]; } // Now write this.WritePropertyUtil(cls.Key, value, new NullSerializationContext(propertyName, this, instance, context as NullSerializationContext), value is IList); } } } }
/// <summary> /// Get the specified formatter /// </summary> /// <param name="type">The type to retrieve the formatter for</param> public INullTypeFormatter GetFormatter(Type type) { INullTypeFormatter typeFormatter = null; if (!this.m_formatters.TryGetValue(type, out typeFormatter)) { typeFormatter = new NullReflectionTypeFormatter(type); lock (this.m_syncLock) if (!this.m_formatters.ContainsKey(type)) { this.m_formatters.Add(type, typeFormatter); } } return(typeFormatter); }