public override Result TrySerialize(object instance, out Data serialized, Type storageType) { long instanceValue = instance != null?Convert.ToInt64(instance) : 0; if (PortableReflection.GetAttribute <FlagsAttribute>(storageType) != null) { long resultInteger = 0; //var result = new StringBuilder(); //bool first = true; foreach (var value in Enum.GetValues(storageType)) { long integralValue = (long)value; bool isSet = (instanceValue & integralValue) != 0; if (isSet) { resultInteger |= integralValue; //if (first == false) result.Append(","); //first = false; //result.Append(value.ToString()); } } //serialized = new Data(result.ToString()); serialized = new Data(resultInteger); } else { //serialized = new Data(Enum.GetName(storageType, instance)); serialized = new Data(instanceValue); } return(Result.Success); }
/// <summary> /// Returns the name the given member wants to use for JSON serialization. /// </summary> private static ReadOnlyCollection <string> GetFallbackNames(MemberInfo member) { var attr = PortableReflection.GetAttribute <SerializePropertyAttribute>(member); if (attr != null && attr.FallbackNames != null) { return(new ReadOnlyCollection <string>(attr.FallbackNames)); } return(new ReadOnlyCollection <string>(new List <string>())); }
public static Option <VersionedType> GetVersionedType(Type type) { Option <VersionedType> optionalVersionedType; if (_cache.TryGetValue(type, out optionalVersionedType) == false) { var attr = PortableReflection.GetAttribute <ObjectAttribute>(type); if (attr != null) { if (string.IsNullOrEmpty(attr.VersionString) == false || attr.PreviousModels != null) { // Version string must be provided if (attr.PreviousModels != null && string.IsNullOrEmpty(attr.VersionString)) { throw new Exception("Object attribute on " + type + " contains a PreviousModels specifier - it must also include a VersionString modifier"); } // Map the ancestor types into versioned types VersionedType[] ancestors = new VersionedType[attr.PreviousModels != null ? attr.PreviousModels.Length : 0]; for (int i = 0; i < ancestors.Length; ++i) { Option <VersionedType> ancestorType = GetVersionedType(attr.PreviousModels[i]); if (ancestorType.IsEmpty) { throw new Exception("Unable to create versioned type for ancestor " + ancestorType + "; please add an [Object(VersionString=\"...\")] attribute"); } ancestors[i] = ancestorType.Value; } // construct the actual versioned type instance VersionedType versionedType = new VersionedType { Ancestors = ancestors, VersionString = attr.VersionString, ModelType = type }; // finally, verify that the versioned type passes some sanity checks VerifyUniqueVersionStrings(versionedType); VerifyConstructors(versionedType); optionalVersionedType = Option.Just(versionedType); } } _cache[type] = optionalVersionedType; } return(optionalVersionedType); }
/// <summary> /// Returns the name the given member wants to use for JSON serialization. /// </summary> private static string GetJsonName(MemberInfo member) { var attr = PortableReflection.GetAttribute <SerializePropertyAttribute>(member); if (attr != null) { if (!string.IsNullOrEmpty(attr.Name)) { return(attr.Name); } } else { var xmlAttr = PortableReflection.GetAttribute <System.Xml.Serialization.XmlAttributeAttribute>(member); if (xmlAttr != null && !string.IsNullOrEmpty(xmlAttr.AttributeName)) { return(xmlAttr.AttributeName); } } return(member.Name); }
public TAttribute GetAttribute <TAttribute>() where TAttribute : Attribute { return(PortableReflection.GetAttribute <TAttribute>(_memberInfo)); }
public Attribute GetAttribute(System.Type p_type) { return(PortableReflection.GetAttribute(_memberInfo, p_type)); }