This project is read-only.
1

Closed

Error with Facebook.Utility.JSONHelper.ConvertToJSONArray

description

This function has an error if the list passed in is empty. It returns a value of "[" when it should either be returning an empty string or "[]" (not sure which, I haven't investigated much beyond this.
 
I found the problem when trying to call Facebook.Rest.Stream.Publish without any media attached to it. It doesn't publish properly unless you always have media included.
Closed Apr 2, 2010 at 5:03 PM by rpowers119
This fix has been checked in. Will be part of version 3.1

comments

charlesboyung wrote Feb 1, 2010 at 2:35 AM

I played around with the code and the following updated functions fix the problem:
    /// <summary>
    /// Converts a List collection of type string to a JSON Array.
    /// </summary>
    /// <param name="list">List of strings</param>
    /// <returns>string</returns>
    public static string ConvertToJSONArray(List<string> list)
    {
        if (list == null || list.Count == 0)
        {
            return "[]";
        }

        StringBuilder builder = new StringBuilder();
        builder.Append("[");
        foreach (var item in list)
        {
            builder.Append(string.Format("{0}{1}{0},", IsJSONArray(item) || IsBoolean(item) ? string.Empty : "\"", EscapeJSONString(item)));
        }
        builder.Replace(",", "]", builder.Length - 1, 1);
        return builder.ToString();
    }

    /// <summary>
    /// Converts a List collection of type long to a JSON Array.
    /// </summary>
    /// <param name="list">List of longs</param>
    /// <returns>string</returns>
    public static string ConvertToJSONArray(List<long> list)
    {
        if (list == null || list.Count == 0)
        {
            return "[]";
        }

        StringBuilder builder = new StringBuilder();
        builder.Append("[");
        foreach (var item in list)
        {
            builder.Append(string.Format("{0}{1}{0},", IsJSONArray(item.ToString()) || IsBoolean(item.ToString()) ? string.Empty : "\"", EscapeJSONString(item.ToString())));
        }
        builder.Replace(",", "]", builder.Length - 1, 1);
        return builder.ToString();
    }

wrote Apr 2, 2010 at 5:03 PM

wrote Feb 22, 2013 at 12:37 AM

wrote May 16, 2013 at 12:10 PM