If you think in a generic way about all well implemented RESTful APIs you will find a pattern that can be easily described and stored in metadata. Most RESTful API is just a combination of the following elements:
2. By using a dynamic type
3. By using anonymous types
And we done! Was a pleasure. Next time will focus on a performance of each solution.
- Resource location (URL)
- HTTP method
- Header information
- Input parameters (required and optional)
- Content type
- Output parameters
- Business logic description
Today I would like to discuss an interesting problem that I came across recently. Imagine for a second that you need to implement a RESTful API client which uses a combination of metadata which describes API and a user input in order to make a HTTP calls.
In such a scenario you will quickly realize that for a subset of API calls you will need to develop a custom classes in order to have them later serialized (to JSON or XML) in runtime so that you can send it via POST or PUT requests. This rises a question. Do I really need to implement N - 1 classes that represent all types that some API(s) expect(s) as a part of HTTP request body?
Well, maybe. In my case I decided to use a more generic approach and leverage a simplicity of standard data formats like JSON. The 'hack' is very simple. From a serialization point of view any class is just a container for properties of specific type - methods and interfaces we can skip as its have nothing to do with serialization. Lets than simplify a class generic description. A class is a collection or key-value pairs. Sounds similar isn't it? Maybe it sounds like JSON format description? This is correct...milestone achieved.
We already know that we have a key-value pair type of collection. How to describe it from a data structure perspective? Very simple! Actually so simple that we have more than one option available.
We can use the followings:
We can use the followings:
- Dictionary<string,object>
- dynamic type
- Anonymous types and var
Other:
- Approach with List<Tuple<string,object>> does not work! A result JSON has a structure with a property names like Item1, Item2....expected.
[{"Item1":"Name","Item2":"Damian"},{"Item1":"Surname","Item2":"Damian"},{"Item1":"Age","Item2":12},{"Item1":"Books","Item2":["Book 1","Book 2"]}]
- If you know any other method (even crazy and geeky version) please let me know. Just don't send a one with reflection...
OK. It's time to serialize out classes.
1. By using Dictionary<string, object>
// Dictionary approach Dictionary<string, Object> userDict = new Dictionary<string, object>(); userDict.Add("Name", "Damian"); userDict.Add("Surname", "Zapart"); userDict.Add("Age", 12); userDict.Add("Books", new List<String> { "Book 1", "Book 2" }); JsonConvert.SerializeObject(userDict);
2. By using a dynamic type
// Dynamic type approach dynamic user = new { Name = "Damian", Surname = "Zapart", Age = 12, Books = new List<String> { "Book 1", "Book 2" }, }; JsonConvert.SerializeObject(user);
3. By using anonymous types
// Anonymous approach var userAnonymous = new { Name = "Damian", Surname = "Zapart", Age = 12, Books = new List<String> { "Book 1", "Book 2" } };
And we done! Was a pleasure. Next time will focus on a performance of each solution.
Result JSON. |