object to json

mehedi hasan
mail@pastecode.io avatar
unknown
csharp
2 years ago
4.1 kB
4
Indexable
Never
private static string JsonFormatter<T>(T course)
    {
        string? result = null;
        if ((course.GetType().IsGenericType &&
             course.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
           || (course.GetType().IsArray)
           ||(course.GetType()==typeof(ArrayList)))
        {//for list,  array, ArrayList
            result += "[";
            foreach (var listItem in course as IEnumerable)
            {
                if (listItem.GetType() == typeof(string))
                    result += "\"" + JsonFormatter(listItem) + "\",";
                else
                    result += JsonFormatter(listItem) + ",";
            }
            result = result.Remove(result.Length - 1);
            result += "]";
        }
        else if (course.GetType().IsGenericType &&
           course.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(Dictionary<,>)))
        {//for dictionary
            result += $"{{";
            foreach (DictionaryEntry dictonaryItem in course as IDictionary)
            {

                if (dictonaryItem.Value.GetType() == typeof(string))
                    result += $"\"{dictonaryItem.Key}\":\"" + JsonFormatter(dictonaryItem.Value) + "\",";
                else
                    result += $"\"{dictonaryItem.Key}\":" + JsonFormatter(dictonaryItem.Value) + ",";

            }
            result = result.Remove(result.Length - 1);
            result += $"}}";
            return result;
        }
        else
        {//if object type is not iterateable 
            var type = course.GetType();
            if (type.ToString().ToLower().Contains("system.string"))
            {
                result += $"{course}";
            }
            else if (type.IsPrimitive)
                            result += course.ToString().ToLower();//in case of bool, cause lowercase boolean is acceptable for json
            else
            {
                result += $"{{";

                var properties = type.GetProperties().Where(x => x.CanWrite && x.CanRead);
                foreach (var property in properties)
                {
                    if (property.PropertyType.IsValueType || property.PropertyType == typeof(string))
                        if (property.PropertyType == typeof(string) || property.PropertyType == typeof(DateTime))
                            result += $"\"{property.Name}\":\"{property.GetValue(course)}\"";
                        else
                            result += $"\"{property.Name}\":{property.GetValue(course)}";
                    else
                    {
                        var instance = property.GetValue(course);

                        if (instance.GetType().ToString().ToLower().Contains("system.collections")
                            || instance.GetType().IsArray)
                        {
                            var items = instance as IEnumerable;
                            var index = 0;
                            result += $"\"{property.Name}\":";
                            foreach (var item in items)
                            {

                                if (index == 0)
                                    result += $"[{ JsonFormatter(item)}";
                                else
                                    result += $"{ JsonFormatter(item)}";
                                result += ",";
                                index++;
                            }
                            result = result.Remove(result.Length - 1);
                            result += "]";
                        }

                        else
                            result += $"\"{property.Name}\":{ JsonFormatter(instance)}";
                    }
                    if (property.Equals(properties.Last()))
                        result += $"}}";
                    else
                        result += $",";
                }

            }
        }

        return result;
    }