Example #1
0
 public static bool EqualsIgnoreCase(this Fixable <string> s1, string s2) => ReferenceEquals(s1.Value, s2) || true == s1.Value?.Equals(s2, StringComparison.OrdinalIgnoreCase);
Example #2
0
 public static bool Equals(this Fixable <string> s1, string s2, StringComparison comparison) => ReferenceEquals(s1.Value, s2) || s1.Value.Equals(s2, comparison);
Example #3
0
 public static bool Equals(this string s1, Fixable <string> s2, StringComparison comparison) => ReferenceEquals(s1, s2.Value) || s1.Equals(s2.Value, comparison);
Example #4
0
 public static string ToPascalCase(this Fixable <string> value) => CodeNamer.Instance.PascalCase(value.Value);
Example #5
0
 public static string EscapeXmlComment(this Fixable <string> comment) => EscapeXmlComment(comment.Value);
Example #6
0
 public static string Substring(this Fixable <string> str, int startIndex) => str.Value.Substring(startIndex);
        /// <summary>
        ///     Overriden to suppress serialization of IEnumerables that are empty.
        /// </summary>
        /// <param name="member"></param>
        /// <param name="memberSerialization"></param>
        /// <returns></returns>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            if (typeof(IModelType).IsAssignableFrom(property.PropertyType) || property.PropertyType.IsGenericOf(typeof(IEnumerableWithIndex <>)))
            {
                property.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
                property.IsReference           = true;
            }

            // if the property is marked as a JsonObject, it should never be treated as a collection
            // and hence, doesn't need our ShouldSerialize overload.
            if (property.PropertyType.CustomAttributes.Any(each => each.AttributeType == typeof(JsonObjectAttribute)))
            {
                return(property);
            }

            if (property.PropertyType.IsConstructedGenericType &&
                property.PropertyType.GetGenericTypeDefinition() == typeof(Fixable <>))
            {
                // gotta stick this in here for a bit.
                property.ShouldDeserialize = property.ShouldSerialize = instance =>
                {
                    Fixable f = null;
                    var     m = instance.GetType().GetMember(member.Name)[0];
                    if (m is PropertyInfo)
                    {
                        f = instance
                            .GetType()
                            .GetProperty(member.Name)
                            .GetValue(instance, null) as Fixable;
                    }
                    else
                    {
                        f = instance
                            .GetType()
                            .GetField(member.Name)
                            .GetValue(instance) as Fixable;
                    }

                    if (f == null)
                    {
                        return(false);
                    }

                    return(f.ShouldSerialize && !f.IsFixed);
                };

                property.ShouldSerialize = instance =>
                {
                    Fixable f = null;
                    var     m = instance.GetType().GetMember(member.Name)[0];
                    if (m is PropertyInfo)
                    {
                        f = instance
                            .GetType()
                            .GetProperty(member.Name)
                            .GetValue(instance, null) as Fixable;
                    }
                    else
                    {
                        f = instance
                            .GetType()
                            .GetField(member.Name)
                            .GetValue(instance) as Fixable;
                    }

                    if (f == null)
                    {
                        return(false);
                    }

                    return(f.ShouldSerialize && f.IsFixed);
                };
            }

            // if the property is an IEnumerable, put a ShouldSerialize delegate on it to check if it's empty before we bother serializing
            if ((property.PropertyType != typeof(string)) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                property.ShouldSerialize = instance =>
                {
                    IEnumerable enumerable = null;

                    // this value could be in a public field or public property
                    switch (member.MemberType)
                    {
                    case MemberTypes.Property:
                        enumerable = instance
                                     .GetType()
                                     .GetProperty(member.Name)
                                     .GetValue(instance, null) as IEnumerable;
                        break;

                    case MemberTypes.Field:
                        enumerable = instance
                                     .GetType()
                                     .GetField(member.Name)
                                     .GetValue(instance) as IEnumerable;
                        break;
                    }

                    return((enumerable == null) || enumerable.GetEnumerator().MoveNext());
                };
            }
            return(property);
        }
Example #8
0
 public static bool StartsWithIgnoreCase(this Fixable <string> str, string startsWith) => true == str?.Value?.StartsWith(startsWith, StringComparison.OrdinalIgnoreCase);
Example #9
0
 public static string EnsureEndsWith(this Fixable <string> str, string suffix) => str.IsNullOrEmpty() ? str.Value : str.Value.EnsureEndsWith(suffix);
Example #10
0
 public static int IndexOf(this Fixable <string> str, char chr) => str?.Value?.IndexOf(chr) ?? -1;
Example #11
0
 public static int IndexOfIgnoreCase(this Fixable <string> str, string text) => str?.Value?.IndexOf(text, StringComparison.OrdinalIgnoreCase) ?? -1;
Example #12
0
 public static bool Contains(this Fixable <string> str, char chr) => true == str?.Value?.Contains(chr);
Example #13
0
 public static bool Contains(this Fixable <string> str, string contained) => true == str?.Value?.Contains(contained);
Example #14
0
 public static bool IsNullOrEmpty(this Fixable <string> str) => string.IsNullOrWhiteSpace(str?.Value);
Example #15
0
 public static bool EqualsIgnoreCase(this Fixable <string> s1, Fixable <string> s2) => ReferenceEquals(s1.Value, s2.Value) || s1.Value.Equals(s2.Value, StringComparison.OrdinalIgnoreCase);
Example #16
0
 public static string Else(this Fixable <string> preferred, Fixable <string> fallback) => string.IsNullOrWhiteSpace(preferred.Value) ? fallback.Value : preferred.Value;
Example #17
0
 public static char CharAt(this Fixable <string> str, int index) => str.Value[index];
Example #18
0
 public static string TrimEnd(this Fixable <string> str, char ch) => str.Value.TrimEnd(ch);
Example #19
0
 public static string ToLower(this Fixable <string> str) => str?.Value.ToLowerInvariant();
Example #20
0
 public static void Null(Fixable<string> str)
 {
     Assert.Null(str.Value);
 }