0

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
  • 2
    What do you mean by "best way?" Can you be more specific about how you want to optimize? Code-generated classes are not supposed to look nice, so if your optimizing for beauty, you'll have to write them yourself. If you have to write the EF classes anyway, maybe you can annotate them to work with JSON and optimize for development time. Commented May 13, 2015 at 15:18
  • this might help github.com/AutoMapper/AutoMapper/wiki/Projection
    – AK_
    Commented May 13, 2015 at 15:35

1 Answer 1

2

I think you have two options here.

  1. 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.

  2. 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.