private YamlMapping CreateMapping(string tag, object obj /*, bool by_content */ )
{
var type = obj.GetType();
/*
if ( type.IsClass && !by_content && type.GetConstructor(Type.EmptyTypes) == null )
throw new ArgumentException("Type {0} has no default constructor.".DoFormat(type.FullName));
*/
var mapping = map();
mapping.Tag = tag;
AppendToAppeared(obj, mapping);
// iterate props / fields
var accessor = ObjectMemberAccessor.FindFor(type);
foreach ( var entry in accessor ) {
var name = entry.Key;
var access = entry.Value;
if ( !access.ShouldSeriealize(obj) )
continue;
if ( access.SerializeMethod == YamlSerializeMethod.Binary ) {
var array = CreateBinaryArrayNode((Array)access.Get(obj));
AppendToAppeared(access.Get(obj), array);
array.Properties["expectedTag"] = TypeNameToYamlTag(access.Type);
mapping.Add(MapKey(entry.Key), array);
} else {
try {
var value = ObjectToNode(access.Get(obj), access.Type);
if( (access.SerializeMethod != YamlSerializeMethod.Content) ||
!(value is YamlMapping) || ((YamlMapping)value).Count>0 )
mapping.Add(MapKey(entry.Key), value);
} catch {
}
}
}
// if the object is IDictionary or IDictionary<,>
if ( accessor.IsDictionary && !accessor.IsReadOnly(obj) ) {
var dictionary = DictionaryToMap(obj);
if ( dictionary.Count > 0 )
mapping.Add(MapKey("IDictionary.Entries"), dictionary);
} else {
// if the object is ICollection<> or IList
if ( accessor.CollectionAdd != null && !accessor.IsReadOnly(obj)) {
var iter = ( (IEnumerable)obj ).GetEnumerator();
if ( iter.MoveNext() ) { // Count > 0
iter.Reset();
mapping.Add(MapKey("ICollection.Items"), CreateSequence("!!seq", iter, accessor.ValueType));
}
}
}
return mapping;
}