0

I'm using NJsonSchema to generate a C# model from a json sample. Environment: dotnet6 I's working fine but it is generate lots of comments and properties that I don't want.

I need a solution either with NJsonSchema or any other library.

Here is an example of what is produced and what is the expected result.

Json Sample

{
  "name": "iPhone 13",
  "brand": "Apple",
  "price": 999.99,
  "specs": {
  "storage": 256,
  "display": "Super Retina XDR",
  "camera": "Dual-Camera System"
  }
}

Schema produced

//----------------------
// <auto-generated>
//     Generated using the NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0) 
(http://NJsonSchema.org)
// </auto-generated>
//----------------------

namespace test
{
#pragma warning disable // Disable all warnings

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.9.0.0 (Newtonsoft.Json v13.0.0.0)")]
public partial class Specs
{
    [Newtonsoft.Json.JsonProperty("storage", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public int Storage { get; set; }

    [Newtonsoft.Json.JsonProperty("display", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public string Display { get; set; }

    [Newtonsoft.Json.JsonProperty("camera", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public string Camera { get; set; }



    private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

    [Newtonsoft.Json.JsonExtensionData]
    public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
    {
        get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
        set { _additionalProperties = value; }
    }

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.9.0.0 (Newtonsoft.Json v13.0.0.0)")]
public partial class Anonymous
{
    [Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public string Name { get; set; }

    [Newtonsoft.Json.JsonProperty("brand", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public string Brand { get; set; }

    [Newtonsoft.Json.JsonProperty("price", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public double Price { get; set; }

    [Newtonsoft.Json.JsonProperty("specs", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
    public Specs Specs { get; set; }



    private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

    [Newtonsoft.Json.JsonExtensionData]
    public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
    {
        get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
        set { _additionalProperties = value; }
    }

}
}

Expected result

namespace myNamespace
{
    public partial class Specs
    {
        public int Storage { get; set; }
        public string Display { get; set; }
        public string Camera { get; set; }
    }
    public partial class Anonymous
    {
        public string Name { get; set; }
        public string Brand{ get; set; }
        public double Price{ get; set; }
        public Specs Specs{ get; set; }
    }
}

I'm trying to cleanup the result with the code beloz but it is very painfull and does not produce yet what I need.

 var schema = JsonSchema.FromSampleJson(jsonData);
        // Configure the C# code generator settings
        var settings = new CSharpGeneratorSettings
        {
            Namespace = defaultNamespace,
            ClassStyle = CSharpClassStyle.Poco,
            GenerateDataAnnotations = false,
            RequiredPropertiesMustBeDefined  = false
        };
         // Generate the C# code
        var csharpGenerator = new CSharpGenerator(schema, settings);
        string generatedCode = csharpGenerator.GenerateFile();
        int pos = generatedCode.IndexOf("namespace");
        generatedCode = generatedCode.Substring(pos);
        string[] lines = generatedCode.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
        lines = lines.Where(line => !line.Contains("[Newtonsoft.Json.JsonProperty")
            && !line.Contains("#pragma warning disable")
            && !line.Contains("System.CodeDom.Compiler.GeneratedCode")).ToArray();
        string cleanedCode = string.Join(Environment.NewLine, lines);
        string outputString = string.Join("\n", cleanedCode.Split('\n').Where(x => !string.IsNullOrWhiteSpace(x)));
        return Task.FromResult(outputString);
CloudAnywhere
  • 669
  • 1
  • 11
  • 27
  • 1) why do you care if it generates extra stuff? 2) personally I use either VS "paste json as classes" or https://app.quicktype.io/ with "just types" setting. – Guru Stron May 08 '23 at 09:16

0 Answers0