public static string ToJson(
     this object value,
     JsonProfile profile,
     Type memberType = null,
     int indentLevel = 1)
 {
     if (value.IsSimpleValue(profile))
     {
         return(value.ToSimpleValue(profile));
     }
     return(!value.IsArray(profile) ? value.ToObject(profile, memberType, indentLevel) : value.ToArray(profile, indentLevel));
 }
        private static string ToArray(this object value, JsonProfile profile, int indentLevel)
        {
            if (!(value is ICollection items) || items.Count == 0)
            {
                return(profile.EmptyArray);
            }
            string        emptyDelimiter = profile.NewLine + JsonSerializer.GetIndent(indentLevel - 1, profile.IndentChars);
            string        indent         = profile.NewLine + JsonSerializer.GetIndent(indentLevel, profile.IndentChars);
            StringBuilder jsonBuilder    = new StringBuilder();

            jsonBuilder.Append(items, profile, profile.Delimiter, emptyDelimiter, indent, indentLevel);
            return("[" + (object)jsonBuilder + "]");
        }
        private static string ToSimpleValue(this object value, JsonProfile profile)
        {
            if (value == null)
            {
                return(profile.NullLiteral);
            }
            if (value is string || value is Guid || (value as Uri) is object)
            {
                return("\"" + JsonSerializer.Escape(value.ToString()) + "\"");
            }
            switch (value)
            {
            case Decimal _:
                return(value.Of <Decimal>().ToString("G", (IFormatProvider)CultureInfo.InvariantCulture));

            case double _:
                return(value.Of <double>().ToString("G", (IFormatProvider)CultureInfo.InvariantCulture));

            case float _:
                return(value.Of <float>().ToString("G", (IFormatProvider)CultureInfo.InvariantCulture));

            case Enum _:
                return(value.Of <int>().ToString());

            default:
                if (profile.SimpleDictionaryFormat && value is DictionaryEntry dictionaryEntry)
                {
                    return(string.Format(profile.DictionaryEntryPattern, (object)JsonSerializer.Escape(dictionaryEntry.Key.ToString()), (object)dictionaryEntry.Value.ToJson(profile, typeof(object), 1)));
                }
                if (value is DateTime)
                {
                    DateTime dateTime = value.Of <DateTime>();
                    if (profile.DateTimeFormat != null)
                    {
                        return(dateTime.ToString(profile.DateTimeFormat.FormatProvider));
                    }
                    return("\"\\/Date(" + (object)((dateTime.ToUniversalTime().Ticks - JsonSerializer.DatetimeMinTimeTicks) / 10000L) + "+" + DateTimeOffset.Now.Offset.ToString("hhmm") + ")\\/\"");
                }
                if (object.Equals(value, (object)true))
                {
                    return(profile.TrueLiteral);
                }
                return(!object.Equals(value, (object)false) ? JsonSerializer.Escape(value.ToString()) : profile.FalseLiteral);
            }
        }
        private static string ToObject(
            this object value,
            JsonProfile profile,
            Type memberType,
            int indentLevel)
        {
            StringBuilder jsonBuilder    = new StringBuilder();
            string        indent         = profile.NewLine + JsonSerializer.GetIndent(indentLevel, profile.IndentChars);
            string        emptyDelimiter = profile.NewLine + JsonSerializer.GetIndent(indentLevel - 1, profile.IndentChars);

            if (value is IDictionary items && profile.SimpleDictionaryFormat)
            {
                if (items.Count == 0)
                {
                    return(profile.EmptyObject);
                }
                jsonBuilder.Append(items, profile, profile.Delimiter, emptyDelimiter, indent);
            }
        private static bool IsSimpleValue(this object value, JsonProfile profile)
        {
            switch (value)
            {
            case null:
            case string _:
            case Guid _:
label_9:
                return(true);

            default:
                if ((value as Uri) is null)
                {
                    switch (value)
                    {
                    case DateTime _:
                    case Decimal _:
                    case Enum _:
                        goto label_9;

                    default:
                        if (!value.GetType().GetTypeInfo().IsPrimitive)
                        {
                            if (!profile.SimpleDictionaryFormat)
                            {
                                return(false);
                            }
                            return(value.GetType().Name.StartsWith(profile.KeyValuePairName) || value is DictionaryEntry);
                        }
                        goto label_9;
                    }
                }
                else
                {
                    goto case null;
                }
            }
        }
 private static bool IsArray(this object value, JsonProfile profile)
 {
     return((!profile.SimpleDictionaryFormat || !(value is IDictionary)) && value is ICollection collection && !((IEnumerable <object>)collection.GetType().GetCustomAttributes(typeof(DataContractAttribute), true)).Any <object>());
 }
 public static string ToJson(this object value, Type memberType = null)
 {
     return(value.ToJson(JsonProfile.GetFormatted(), memberType, 1));
 }