예제 #1
0
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                JsonProperty property = base.CreateProperty(member, memberSerialization);

                property.Ignored         = IgnoredDataTypes.Any(p => p == property.PropertyType);
                property.ShouldSerialize = instance => !property.Ignored;

                if (PRETTY_NAMES)
                {
                    property.PropertyName = StringTransform.PrettyMsSqlIdent(property.PropertyName);
                }

                return(property);
            }
예제 #2
0
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                JsonProperty property = base.CreateProperty(member, memberSerialization);

                property.ShouldSerialize = instance =>
                                           property.PropertyType != typeof(string) &&
                                           (!property.PropertyType.IsValueType || property.PropertyName == "Id");
                property.Ignored = false;
                if (PRETTY_NAMES)
                {
                    property.PropertyName = StringTransform.PrettyMsSqlIdent(property.PropertyName);
                }

                return(property);
            }
예제 #3
0
 private static string pretty(string name) =>
 _prettyDictionary.ContainsKey(name)
                                                  ? _prettyDictionary[name]
                                                  : (_prettyDictionary[name] = StringTransform.PrettyMsSqlIdent(name));
예제 #4
0
 private static string pretty(string Name)
 {
     return(StringTransform.PrettyMsSqlIdent(Name));
 }
예제 #5
0
        /// <summary>
        ///     PropertyType(s) are merged when there DeclaringType(s) have the same names & the properties between those
        ///     DeclaringType(s) match also. This will yield a class that given any of the sourceTypes serialized with json; can be
        ///     de-serialized with a class defining the properties return.
        /// </summary>
        /// <param name="nameSpace"></param>
        /// <param name="sourceTypes"></param>
        /// <param name="propertyNameExclutions"></param>
        /// <param name="BaseType"></param>
        /// <param name="prettyNames"></param>
        /// <param name="ClassProperties"></param>
        /// <returns></returns>
        private ClassProperty[] ConsolidatePropertyNames(string nameSpace, Type[] sourceTypes = null, string[] propertyNameExclutions = null, Type BaseType = null, bool prettyNames = true, ClassProperty[] ClassProperties = null)
        {
            if (ClassProperties == null)
            {
                ClassProperties = new ClassProperty[] { }
            }
            ;

            if (propertyNameExclutions == null)
            {
                propertyNameExclutions = new string[] { }
            }
            ;

            if (sourceTypes == null)
            {
                sourceTypes = new Type[] { }
            }
            ;
            else
            {
                if (BaseType == null)
                {
                    BaseType = DefaultBaseType(sourceTypes);
                }

                if (BaseType != null)
                {
                    propertyNameExclutions =
                        BaseType
                        .GetProperties()
                        .Select(p => p.Name)
                        .Union(propertyNameExclutions)
                        .ToArray();
                }
            }

            Dictionary <string, Type> dic = new Dictionary <string, Type>();

            // do simple value/quazi-value type properties first
            foreach (var _ClassProperty in
                     FilterInvalidProperties(sourceTypes, propertyNameExclutions, prettyNames)
                     .Select(p => new ClassProperty(p.Name, p.PropertyType))
                     .Union(ClassProperties))
            {
                string propName = prettyNames
                                      ? StringTransform.PrettyMsSqlIdent(_ClassProperty.Name)
                                      : _ClassProperty.Name;

                if (_ClassProperty.PropertyType.hasConvert())
                {
                    // handle normal numeric, byte, string & datetime types
                    Type typeA = _ClassProperty.PropertyType;
                    Type typeB = dic.ContainsKey(propName)
                                     ? dic[propName]
                                     : _ClassProperty.PropertyType;
                    Type typeC = ImplicitTypeConversionExtension.TypeLcd(typeA.GetPrincipleType(), typeB.GetPrincipleType());

                    // there are many situations that will yield a nullable
                    if (Nullable.GetUnderlyingType(typeA) != null || Nullable.GetUnderlyingType(typeB) != null)
                    {
                        dic[propName] = typeof(Nullable <>).MakeGenericType(typeC);
                    }
                    else if (typeC.IsNullable() && sourceTypes.Count() > 0 && FilterInvalidProperties(sourceTypes, propertyNameExclutions, prettyNames).Count() != sourceTypes.Count())
                    {
                        dic[propName] = typeof(Nullable <>).MakeGenericType(typeC);
                    }
                    else
                    {
                        dic[propName] = typeC;
                    }
                }
                else if (dic.ContainsKey(propName) && dic[propName].hasConvert() != _ClassProperty.PropertyType.hasConvert())
                {
                    throw new Exception(string.Format("Property {0} is defined as both a primitive value data type & complex reference type amount properties defined in parent types {1}; automatic union of these property types can't be performed.", propName, string.Join(", ", sourceTypes.Select(t => t.FullName).ToArray())));
                }
                else if (dic.ContainsKey(propName) && dic[propName].isEnumeratedType() != _ClassProperty.PropertyType.isEnumeratedType())
                {
                    throw new Exception(string.Format("Property {0} is defined as both a Enumerable & Non-Enumerable properties defined in parent types {1}; automatic union of these property types can't be performed.", propName, string.Join(", ", sourceTypes.Select(t => t.FullName).ToArray())));
                }
                else if (_ClassProperty.PropertyType == typeof(byte[]) || _ClassProperty.PropertyType == typeof(string))
                {
                    dic[propName] = _ClassProperty.PropertyType;
                }
                else if (!dic.ContainsKey(propName))
                {
                    //TODO:stop recursive compiling off child objects as the names are the only thing we are looking for, not the types. far to many needless compiles are occurring when calling upon UnionOnName
                    ClassProperty[] dstTypes = FilterInvalidProperties(sourceTypes, propertyNameExclutions, prettyNames, new[] { _ClassProperty.Name }, ClassProperties);

                    if (dstTypes.Length > 0)
                    {
                        Type _UnionOnNameType = MergeOnPropertyNames(
                            nameSpace,
                            dstTypes.Select(prop => prop.PropertyType.GetPrincipleType()).ToArray(),
                            null,
                            propertyNameExclutions.Union(
                                dstTypes
                                .Where(prop => !sourceTypes.Select(t => t.Name).Contains(prop.PropertyType.Name))
                                .Select(prop => prop.Name)
                                ).ToArray(),
                            prettyNames);

                        // properties that are collections will yield as generic lists only
                        dic[propName] =
                            _ClassProperty.PropertyType.GetEnumeratedType() != null &&
                            _ClassProperty.PropertyType != typeof(string)
                                ? typeof(List <>).MakeGenericType(_UnionOnNameType)
                                : _UnionOnNameType;
                    }
                }
            }

            return(dic.Select(m => new ClassProperty(m.Key, m.Value)).ToArray());
        }