I want to consume part of the Steam WebAPI, it’s a simple Rest Service but it produces some complex json.
I thought about using the Newtonsoft Json.Net Library generate my c# objects.
Whats the best way to get the needed Classes to easily use the Library?
I thought about using http://json2csharp.com/ to generate the needed Classes, but of course these don’t follow any naming conventions. But as i save the data into a database anyway I have to convert it to the EF-Classes anyway.
I could write my own classes and use annotations, would result in nicer code but a little bit more work.
Whats the best practice creating the c# infrastructure for a external json service?
2
I think you have two options here.
-
Best practice. Create objects which match the json returned by/sent to the API and serialize/deserialze these objects when you make calls or receive responses. Hide the JSON implementation from the consumer of this ‘Client Library’
These objects could be created with a tool, or hand crafted. Either way you are probably going to have some custom code to deal with the edge cases of converting JSON to .net datatypes and classes
Additionally, you’ll need to spend alot of time creating and maintaining these objects.
-
Quick ‘n’ Dirty. rather than serializing to objects, simply parse the JSON and query the values out of the responses that you need to use with XPath or similar.
This will yield a partial implementation of a Client, but if you only want a sub set of the features may well be much faster to implement