Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map JSON string to the calling of C# method

Tags:

json

c#

I want to implement a framework to map JSON string to the calling of C# method. For example, I have a C# class Calculator defined as below.

// C# class
class Calculator
{
public:
    int add (int x, int y);
    int sub (int x, int y);
}

There is a JSON string as below. When the framework receives this string, it creates/new an object of class Calculator. Then call its function add. And pass the value 12 and 43 to the function as parameters.

// JSON string
"{
\"class\":\"Calculator\",
\"method\":\"add\",
\"parameters\": {
    \"x\" : \"12\", \"y\" : \"43\"
    }
}"

Is there any 3rd party library to implement this? Or how can I implement it by myself?

like image 937
Jeffrey Avatar asked Aug 16 '12 12:08

Jeffrey


People also ask

How to map JSON to a structure in C++?

To map JSON to a structure, it is necessary to register all data members of all structures that you want to mapped using for each field template<typename T, typename V, typename ... U, template<typename> typename ... Options> inline void reg (V T::* ptr, std::string const & name, Options<U>&& ... options);

What is JSON string in C?

Working With JSON String In C#. JSON (JavaScript Object Notation) is a lightweight data interchange format. It is language independent, easy to understand and self-describing. It is use as an alternative to XML.

How to get data from JSON string to dynamic object?

JObject class has parse method; it parses the JSON string and converts it into a Key-value dictionary object. In the following example, I have used “JObject.Parse” method and retrieved data using key. In the following code snippet, we just assign JObject.Parse method output to dynamic object and access value as properties of dynamic object.

What do you learn in a JSON mapper course?

You also learned to break down your target JSON and then create required JSON mapper objects with the names of the properties exactly according to the JSON structure with case sensitivity. You learned to serialize and deserialize the JSON string & object to pass it over the network or to share it via web API.


1 Answers

A small working sample. Of course many checks are missing. (Using Json.Net)

string jsonstring = "{\"class\":\"Calculator\",\"method\":\"add\",\"parameters\": { \"x\" : \"12\", \"y\" : \"43\" }}";

var json = (JObject)JsonConvert.DeserializeObject(jsonstring);

Type type = Assembly.GetExecutingAssembly()
                    .GetTypes()
                    .First(t => t.Name==(string)json["class"]);

object inst = Activator.CreateInstance(type);
var method =  type.GetMethod((string)json["method"]);
var parameters = method.GetParameters()
        .Select(p => Convert.ChangeType((string)json["parameters"][p.Name], p.ParameterType))
        .ToArray();
var result =  method.Invoke(inst, parameters);

var toReturn = JsonConvert.SerializeObject(new {status="OK",result=result });

-

class Calculator
{
    public int add(int x, int y)
    {
        return x + y;
    }
}
like image 78
L.B Avatar answered Sep 19 '22 12:09

L.B