-1

I have a Json schema and can use NJsonSchema.CodeGeneration.CSharp to create classes corresponding to it. So if I have json which conforms to the schema, I should be able to easily parse it into a collection of objects whose classes are the classes generated from the schema?

So how can I do this - parse json and get out C# class objects that correspond to the objects in the json (as define by the schema)?

As an example - if the schema defines a first object definition which is an array of a second object definition, then I would like to be able to parse it in such a way that the output is an instance of the class corresponding to the first object definition and it has a member which is a List of instances of the class corresponding to the second definition. It seems that the schema knows all the information required to do this, so it should be a single line - I appreciate I could do long-hand parsing (eg converting each item in the array) to achieve the same result.

I would think this would be a primary purpose of having C# classes generated from a schema, so what's the magic method I'm missing?

I'm also happy to write C# classes and generate a schema from that if it's a more workable solution.

I've used NJsonSchema but happy to use any other C# json schema and code generation technique that achieves the same end.

UPDATE: After discussion I've seen that if NJsonSchema is used to generate classes from the schema, the TypeScript version of those classes each have a fromJS method which sounds like what I want but they're missing from the C# version. I'm happy to use something other than NJsonSchema to generate classes from schema if it provides a solution.

Richard Hunt
  • 303
  • 2
  • 10
  • Your question is getting downvotes because it's not clear what you're asking. I can't see a question. You may find it useful to read the Stackoverflow guide on how to ask: https://stackoverflow.com/help/how-to-ask – Relequestual Oct 05 '20 at 13:24
  • Thanks for the clarification, I did wonder why I was getting downvotes... I'll try to rephrase in the form of a question – Richard Hunt Oct 05 '20 at 13:30
  • No problem. I think people assume if you have a small amount or reputation, you shouldn't need reminding, but it can be easy to do when you've come out of focus mode. This looks like ramblings rather than a question =/ – Relequestual Oct 05 '20 at 13:33
  • Have reworded to put an explicit question in there, let me know if that helps. – Richard Hunt Oct 05 '20 at 13:34
  • Yes appreciate it looks quite rambling, unfortunately that's the nature of the problem - a single sentence description has lots of words in it! – Richard Hunt Oct 05 '20 at 13:35
  • It looks like you're asking how to do something which is clearly show in the documentation of the library in question: https://github.com/RicoSuter/NJsonSchema#njsonschemacodegeneration-usage - "How to make instances from the generated classes" – Relequestual Oct 05 '20 at 13:38
  • Relequestual That link is about generating C# classes from schema, my question is about parsing json (not a json schema) and getting from it instances of those classes. I can understand you might think that's my question which is why I tried to be verbose in explaining it, but the distinction was probably lost in the detail. – Richard Hunt Oct 05 '20 at 13:50
  • The resulting class has a `fromJS` function, which given JSON, returns a new class based instance. Have you looked at the C# generated classes? Does it include any functions? – Relequestual Oct 05 '20 at 13:52
  • Odd, I have the generated classes but there's no `fromJS` method (and they don't inherit from any parent class). I did look specifically there for a solution. I can see `fromJS` is in the example `TypeScript` but the generated C# only have methods corresponding to the properties in the schema. I wonder why they're partial, it's like it expects a 2nd file of implementation but none comes from the generator. – Richard Hunt Oct 05 '20 at 14:03
  • Suggest you file an issue on the associated projects github repo. I don't know what it's like in C#, but for node, I pass data to constructor, job done. – Relequestual Oct 05 '20 at 14:06
  • Thanks, perhaps the c# version of the generated code is half-implemented... I shall look wider. Thanks for your help – Richard Hunt Oct 05 '20 at 14:09

1 Answers1

0

I think I found the answer, which was a lot simpler than I had anticipated. It's simply to use something like:

var ob=JsonConvert.DeserializeObject<MyNamespace.Anonymous>(jsonString);

...where MyNamespace is the namespace of the generated C# classes, MyNamespace.Anonymous is the class corresponding to the root of the schema (default names from NJsonSchema), and jsonString the string to be parsed.

I thought the solution would need to be schema-aware, since it would need to know about all the classes created from the schema, but I guess it 'works that out' from reflecting on the Anonymous class it's given and where the properties of it are classes, reflecting on those and so on.

I was over-thinking the problem!

Richard Hunt
  • 303
  • 2
  • 10