Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET WCF Json deserializing a Dictionary<int, int>

Tags:

json

c#

wcf

I have a method:

[WebGet]
[OperationContract]
public int CollabSortFolder(int FolderId, Dictionary<int, int> Items)
{

    Console.Write(Items);
    return -1;
}

If I try this url:

http://server/CollabSortFolder?FolderId=12&Items={"1":3,"4":5,"6":7}

Items.Count is 0

What is the proper syntax to get my Dictionary filled?

like image 269
Joe Avatar asked Jan 19 '23 06:01

Joe


2 Answers

Modeling the key, value objects works:

http://server/CollabSortFolder?FolderId=12&Items=[{"Key":1,"Value":3}, {"Key":2,"Value":4}]
like image 87
Joe Avatar answered Jan 21 '23 20:01

Joe


You can customize the deserialization of a query string parameter by writing a custom QueryStringConverter for your service. The code below shows one which can understand the format you have in your question.

public class StackOverflow_6630425
{
    [ServiceContract]
    public class Service
    {
        [WebGet]
        public string CollabSortFolder(int FolderId, Dictionary<int, int> Items)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("FolderId=" + FolderId);
            foreach (var key in Items.Keys)
            {
                sb.AppendLine(string.Format("  Items[{0}] = {1}", key, Items[key]));
            }
            return sb.ToString();
        }
    }
    public class MyQueryStringConverter : QueryStringConverter
    {
        public override bool CanConvert(Type type)
        {
            return type == typeof(Dictionary<int, int>) || base.CanConvert(type);
        }

        public override object ConvertStringToValue(string parameter, Type parameterType)
        {
            if (parameterType == typeof(Dictionary<int, int>))
            {
                parameter = parameter.Trim().Substring(1, parameter.Length - 2); // trimming the begin and end '{' / '}'
                string[] pairs = parameter.Split(',');
                Dictionary<int, int> result = new Dictionary<int, int>();
                foreach (string pair in pairs)
                {
                    string[] parts = pair.Split(':');
                    string key = parts[0].Trim();
                    string value = parts[1].Trim();
                    if (key.StartsWith("\"")) key = key.Substring(1);
                    if (key.EndsWith("\"")) key = key.Substring(0, key.Length - 1);
                    result.Add(int.Parse(key), int.Parse(value));
                }

                return result;
            }

            return base.ConvertStringToValue(parameter, parameterType);
        }
    }
    class MyWebHttpBehavior : WebHttpBehavior
    {
        protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
        {
            return new MyQueryStringConverter();
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new MyWebHttpBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/CollabSortFolder?FolderId=12&Items={\"1\":3,\"4\":5,\"6\":7}"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
like image 28
carlosfigueira Avatar answered Jan 21 '23 19:01

carlosfigueira