NJsonSchema icon indicating copy to clipboard operation
NJsonSchema copied to clipboard

CSharp Generated class name

Open robsonj opened this issue 4 years ago • 5 comments

Hi There,

A question rather than an issue. When I generate C# from a json schema, the resulting class is called 'Anonymous', is there a way to specify the name of the generated class please?

Cheers Jonathan

robsonj avatar Mar 11 '20 13:03 robsonj

Set the “title” attribute of the given schema

RicoSuter avatar Mar 11 '20 18:03 RicoSuter

Set the “title” attribute of the given schema

That worked. We did have it set, but it contained spaces.

Thank you!

robsonj avatar Mar 11 '20 18:03 robsonj

And what if title contains dots (namespace.class)? Should the title be used as a class name? I suppose it should be but is not (#1131).

jirikanda avatar Mar 12 '20 12:03 jirikanda

I think the title is only used if it matches [a-zA-Z0-9] (or similar) - this is because the namespace should not be part of the schema (leaky abstraction).

RicoSuter avatar Mar 20 '20 15:03 RicoSuter

Regarding this point, I am using the NJsonSchema v10.6.10.0 (Newtonsoft.Json v13.0.0.0) but setting the Title is not changing the title of the class, still getting:

public partial class Anonymous

I am doing it like:

JsonSchema schema = JsonSchema.FromSampleJson(json);
schema.Title = className;
CSharpGeneratorSettings genSettings = new CSharpGeneratorSettings { Namespace = "Veronica.JsonClasses", ClassStyle = CSharpClassStyle.Poco };
CSharpGenerator generator = new CSharpGenerator(schema, genSettings);
var file = generator.GenerateFile();

gkapellmann avatar Mar 27 '22 22:03 gkapellmann