SubSonic Forums
All Your Database Are Belong To Us

Add ToJSON method to ActiveRecord and ActiveList

Latest post 08-08-2008 10:26 PM by ranomore. 1 replies.
  • 08-06-2008 3:44 AM

    Add ToJSON method to ActiveRecord and ActiveList

     Subsonic 2.1 Pakala has a new execute to json format but it is very simple and user can not control over toStringMethod

    If this class can have a virtual method for to string method it can be more power full. for example for converting and formating string in Dates or other object.

    also ActiveRecord and ActiveList (called Collection) does not have any toJson method, I add them, I can say it is very usefull if add to subsonic official release. A would like to share it and suggest to add to subsonic next version:

    ActiveRecord.cs (Add these methods and property)

     /// <summary>
    /// Get column value as string user can override it to do any formatting on object
    /// </summary>
    /// <param name="Name">Column name</param>
    /// <returns>value as string</returns>
    protected virtual string ColumnValueToString(string Name)
    {
    object val = GetColumnValue(Name);
    if (val != null)
    {
    return GetColumnValue(Name).ToString();
    }
    else
    {
    return "";
    }
    }

    private static string[] _colsArray;
    public static string[] ColsArray
    {
    get
    {
    if (_colsArray == null)
    {
    _colsArray = ColumnsAarray();
    }
    return _colsArray;
    }
    }


    public string JSON()
    {
    System.Collections.Generic.List<string> cls = new System.Collections.Generic.List<string>();
    for (int i = 0; i < ColsArray.Length; i++)
    {
    cls.Add("\"" + ColsArray + "\":\"" + ColumnValueToString(ColsArray) + "\"");
    }
    return "{" + string.Join(",", cls.ToArray()) + "}";
    }

    /// <summary>
    /// Get all columns array
    /// </summary>
    /// <returns>string array names of all columns</returns>
    public static string[] ColumnsAarray()
    {
    TableSchema.TableColumnCollection cols = table.Columns;
    System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>();
    foreach (TableSchema.TableColumn col in cols)
    {
    result.Add(col.ColumnName);
    }
    return result.ToArray();
    }

     

    ActiveList.cs

    /// <summary>
    /// Retrun json format of a collection
    /// </summary>
    /// <returns></returns>
    public string ToJson()
    {
    List<string> result = new List<string>();
    foreach (ItemType item in this)
    {
    result.Add(item.JSON());
    }
    return string.Join(",", result.ToArray());
    }

     

  • 08-08-2008 10:26 PM In reply to

    • ranomore
    • Top 10 Contributor
    • Joined on 11-05-2007
    • Salt Lake City
    • Posts 322

    Re: Add ToJSON method to ActiveRecord and ActiveList

     Any chance you can do a patch and post it on code plex?

    Find a bug? Create a Work Item for a fast response.. Want to help? Create a patch for us!

Page 1 of 1 (2 items) | RSS