5

Is there any tool allowing to generate DataContract entity definitions based on a JSON sample? I'm really missing the old good WSDL metadata with code generation facilities.

dbc
  • 104,963
  • 20
  • 228
  • 340
Ivan G.
  • 5,027
  • 2
  • 37
  • 65
  • JSON doesn't specify any type information. JSON nested objects that declare the same set of fields *might* correspond to equivalent classes -- or they might not. When you sacrifice all that meta data by using JSON, you really are sacrificing quite a lot of info, and part of that sacrifice means that it is not possible to robustly generate C# types (i.e. statically-typed) based on JSON (i.e. not dynamically-typed). – Kirk Woll Oct 28 '11 at 18:28
  • Well, you can guess. At least you can differentiate between entities, arrays, integers, and strings. You can try to guess type inside string value sample i.e. TimeSpan looks like "00:00:00" etc. I don't mind fixing entity types after generator but I do mind generating all by hand. – Ivan G. Oct 28 '11 at 18:31

2 Answers2

9

I know this is a very old question but it's worth noting that if you're using Visual Studio you get that without any external tools - you can copy some JSON in your clipboard and then go to Edit > Edit Special > "Paste JSON As Classes".

charisk
  • 3,190
  • 2
  • 24
  • 18
6

Yes, you can find such a tool at http://jsontodatacontract.azurewebsites.net/. There's a post about it at http://blogs.msdn.com/b/carlosfigueira/archive/2011/01/11/inferring-schemas-for-json.aspx.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Perfect! That's exactly what I wanted. It's not perfect but theres source code to fix the dirty stuff. – Ivan G. Oct 28 '11 at 18:32