public static bool IsIgnorable <T>(JsonFieldIgnorableAttribute f, T o) { if (f == null) { return(false); } // Value if (Object.Equals(o, f.WhenValueIs)) { return(true); } // Length var a = o as Array; if (a != null) { return(a.Length == f.WhenLengthIs); } var l = o as IList; if (l != null) { return(l.Count == f.WhenLengthIs); } // Others return(false); }
private static IEnumerable <KeyValuePair <RankedKey, object> > ToRankedKeyValuesUnordered(object o) { var ty = o.GetType(); if (TypeWrap(ty).IsGenericType&& ty.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { var keyType = TypeWrap(ty).GetGenericArguments()[0]; if (keyType != typeof(string)) { // TODO: Should allow them and call `ToString`? throw new NotImplementedException(); } foreach (DictionaryEntry elem in (IDictionary)o) { yield return(new KeyValuePair <RankedKey, object>( new RankedKey { Order = 0, // Dictionary has no order infomation Key = (string)elem.Key, }, elem.Value)); } } else { var fields = TypeWrap(ty).GetFields(); foreach (var field in fields) { var fieldAttr = GetCustomAttribute <JsonFieldAttribute>(field); // TODO: duplication check var elemName = JsonFieldAttribute.FieldName(fieldAttr, field); var elemValue = field.GetValue(o); var fieldIgnoreAttr = GetCustomAttribute <JsonFieldIgnorableAttribute>(field); if (JsonFieldIgnorableAttribute.IsIgnorable(fieldIgnoreAttr, elemValue)) { continue; } yield return(new KeyValuePair <RankedKey, object>( new RankedKey { Order = JsonFieldAttribute.FieldOrder(fieldAttr), Key = elemName, }, elemValue)); } } }