Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to a class representing JSON with variable parameters in C#

Tags:

json

c#

wcf

I have a webservice in WCF whose operations require requests and responses in JSON format. I know that I can just write C# objects with properties that I want represented in JSON, but my problem is that the JSON parameters may change. For example, my method contract is the following:

    [WebInvoke(Method = "PUT", 
        UriTemplate = "users", 
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    Response PutUserAccount(User user);

User's parameters may contain any number of parameters, so an instance of User may be sometimes:

{
    "Name" : "John",
    "LastName" : "Doe",
    "Email" : "[email protected]",
    "Username" : "jdoe",
    "Gender" : "M"
    "Phone" : "9999999"
}

or even:

{
    "Name" : "John",
    "LastName" : "Doe",
    "Email" : "[email protected]",
    "Username" : "jdoe",
    "FavoriteColor" : "Blue"
}

What is the best approach to have an object with a mutable number of properties to represent a JSON document?

EDIT This class allowed me to have a flexible JSON representation, since I can't use a JObject with WCF (Should I post this as answer?):

using System; 
using System.Collections.Generic; 
using System.Runtime.Serialization;

namespace MyNamespace {
    [Serializable]
    public class Data : ISerializable
    {
        internal Dictionary<string, object> Attributes { get; set; }

        public Data()
        {
            Attributes = new Dictionary<string, object>();
        }

        public Data(Dictionary<string, object> data)
        {
            Attributes = data;
        }

        protected Data(SerializationInfo info, StreamingContext context)
            : this()
        {
            SerializationInfoEnumerator e = info.GetEnumerator();
            while (e.MoveNext())
            {
                Attributes[e.Name] = e.Value;
            }
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            foreach (string key in Attributes.Keys)
            {
                info.AddValue(key, Attributes[key]);
            }
        }

        public void Add(string key, object value)
        {
            Attributes.Add(key, value);
        }

        public object this[string index]
        {
            set { Attributes[index] = value; }
            get
            {
                if (Attributes.ContainsKey(index))
                    return Attributes[index];
                else
                    return null;
            }
        }
    } 

}

like image 606
AntonioJunior Avatar asked Oct 22 '22 01:10

AntonioJunior


1 Answers

You can use JObject class from Json.NET. You can parse your json to a JObject property and manipulate it. JObject is more than just a Dictionary.

like image 65
S.M.Amin Avatar answered Oct 27 '22 10:10

S.M.Amin