/// <summary>
 /// Initializes a new instance of the <see cref="JsonMemberCasingAttribute"/> class.
 /// </summary>
 /// <param name="casing">The casing of members when serializing into JSON.</param>
 public JsonMemberCasingAttribute(JsonMemberCasing casing)
 {
     _casing = casing;
 }
Ejemplo n.º 2
0
        private static string ToStringJson(object o, bool compact, int level)
        {
            if (o == null)
            {
                return("null");
            }
            else if (false.Equals(o))
            {
                return("false");
            }
            else if (true.Equals(o))
            {
                return("true");
            }
            else if (o is string || o is char || o is Enum)
            {
                return("\"" + JsonEscape(o.ToString()) + "\"");
            }
            else if (IsIDictionary(o))
            {
                return(IDictionaryToStringJson(ToIDictionary(o), compact, level));
            }

            Type oType = o.GetType();

            if (oType.IsPrimitive || oType == typeof(decimal))
            {
                return(Convert.ToString(o, CultureInfo.InvariantCulture));
            }

            JsonMemberCasing memberCasing = JsonMemberCasing.CamelCase;

            foreach (JsonMemberCasingAttribute attribute in oType.GetCustomAttributes <JsonMemberCasingAttribute>(true))
            {
                memberCasing = attribute.Casing;
            }

            StringBuilder sb = new StringBuilder();

            if (!compact && level > 0)
            {
                sb.AppendLine();
            }

            sb.Append(Indent(compact, level) + "{");
            ++level;

            bool firstItem = true;

            PropertyInfo[] properties = oType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo item = properties[i];
                if (item.GetIndexParameters().Length > 0)
                {
                    continue;
                }

                object value = item.GetValue(o, null);

                if (value == null || object.ReferenceEquals(value, o))
                {
                    continue;
                }

                if ((value is bool && false.Equals(value)) ||
                    (value is decimal && decimal.Zero.Equals(value)) ||
                    object.Equals(value, value.GetType().GetDefault()))
                {
                    if (item.IsDefined <JsonIgnoreDefaultAttribute>(true))
                    {
                        continue;
                    }
                }

                if (!firstItem)
                {
                    sb.Append(",");
                }
                else
                {
                    firstItem = false;
                }

                if (!compact)
                {
                    sb.AppendLine();
                }

                string itemName = item.Name;
                switch (memberCasing)
                {
                case JsonMemberCasing.LowerCase: itemName = itemName.ToLowerInvariant(); break;

                case JsonMemberCasing.UpperCase: itemName = itemName.ToUpperInvariant(); break;

                case JsonMemberCasing.CamelCase: itemName = itemName.ToCamelCaseInvariant(); break;
                }

                foreach (JsonMemberNameAttribute attribute in item.GetCustomAttributes <JsonMemberNameAttribute>(true))
                {
                    if (attribute.IsOutputName)
                    {
                        itemName = attribute.JsonName ?? itemName;
                    }
                }

                sb.Append(Indent(compact, level) + "\"" + itemName + "\":");

                if (!IsICollection(item.PropertyType) || IsIDictionary(item.PropertyType))
                {
                    if (!compact)
                    {
                        sb.Append(" ");
                    }

                    string valueJson = ToStringJson(value, compact, level);
                    sb.Append(valueJson);
                }
                else
                {
                    if (!compact)
                    {
                        sb.AppendLine();
                    }

                    sb.Append(Indent(compact, level) + "[");

                    if (!compact)
                    {
                        sb.AppendLine();
                    }

                    {
                        int         j = 0;
                        ICollection valueCollection = ToICollection(value);
                        foreach (object valueItem in valueCollection)
                        {
                            sb.Append(Indent(compact, level + 1) + ToStringJson(valueItem, compact, level + 1));

                            if (++j < valueCollection.Count)
                            {
                                if (!compact)
                                {
                                    sb.AppendLine(",");
                                }
                                else
                                {
                                    sb.Append(",");
                                }
                            }
                        }

                        if (!compact && valueCollection.Count > 0)
                        {
                            sb.AppendLine();
                        }
                    }

                    sb.Append(Indent(compact, level) + "]");
                }
            }

            --level;

            if (!compact)
            {
                sb.AppendLine();
            }

            sb.Append(Indent(compact, level) + "}");

            return(sb.ToString());
        }