RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.4k stars 535 forks source link

Is there any way to generate readonly property? #660

Open deepakmhr opened 6 years ago

deepakmhr commented 6 years ago

Here is class generation code:

        var schema = JsonSchema4.FromSampleJson(jsonData);
        var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()
        {
            Namespace = typeof(Program).Namespace,
            GenerateDataAnnotations = true,
            ClassStyle = CSharpClassStyle.Inpc,
            HandleReferences = true,
            SchemaType = SchemaType.JsonSchema
        });
        var file = generator.GenerateFile("DataSetHelper");

Here is the generated property:

    [Newtonsoft.Json.JsonProperty("Gender", Required = Newtonsoft.Json.Required.DisallowNull,         NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
     public string Gender
    {
        get { return _gender; }
        set
        {
            if (_gender != value)
            {
                _gender = value;
                RaisePropertyChanged();
            }
         }
    }

But my requirement is to generate readonly properties, something like this:

 [Newtonsoft.Json.JsonProperty("Gender", Required = Newtonsoft.Json.Required.DisallowNull,         NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
 public string Gender
 {
    get { return _gender; }
    private  set
    {
        if (_gender != value)
        {
            _gender = value;
            RaisePropertyChanged();
        }
     }
 }

Is this library support such configuration? please help

RicoSuter commented 6 years ago

I think there is no setting for that yet, but you could replace the whole Class.liquid template:

https://github.com/RSuter/NJsonSchema/wiki/Templates

deepakmhr commented 6 years ago

Thanks @RSuter for the help.