JsonClassGen – A json2csharp Command Line

json2csharpcli

JsonClassGen

I needed a command line wrapper around the fantastic json2csharp library to generate C# code. So I wrote one.

It’s not 100% done yet, but does most things, and includes batch support for build pipelines.

Check it out on github!

Usage

Batch processing

Generates all the code based on the configuration file.

JsonClassGen.exe batch -f configfile.json

Options file is in the format below. Options are standard across all the classes to create consistency. If, for example, you want some files visible internally and other not, create a seperate config. That way you know what you’re exposing clearly.

{
"options": {
"useproperties": true,
"pascal": true,
"nested": true,
"singlefile": true,
"internalvisibility": false
},
"classes": [
{
"filename": "..\\some\\folder\\dataresponse.json",
"namespace": "Fantastic.Namespace.Model",
"mainclass": "DataResponse"
},
{
"filename": "..\\some\\folder\\autorecognizeresponse.json",
"namespace": "Audio.Recognition.Model",
"mainclass": "AudioRecognizeResponse"
}
]
}

Single file

Generates the code based for a single json file. The below example generates nested classes in a single file using Pascal case, with the main class for the json file “MainClassName” with the awesome namespace.

JsonClassGen.exe generate -f myjson.json -n Awesome.Namespaces.Are.Awesome -m MainClassName -p -s --pascal --nested

-f, --filename Required. Input JSON file to read.
-t, --targetfolder Target output folder.
-n, --namespace Required. Target Namespace.
-p, --useproperties (Default: true) Use Properties.
-i, --internal (Default: false) Internal visibility only.
-x, --explicit (Default: false) Explicit deserialization.
-h, --nohelp (Default: false) No helper class.
-m, --mainclass Main class.
--pascal (Default: false) Pascal Case.
--nested (Default: false) Nested classes.
-o, --obfuscation (Default: false) Apply obfuscation attributes.
-s, --singlefile (Default: false) Single file.
--nullable (Default: false) Always use nullable values.
--examples (Default: false) Examples in documentation.
-v, --verbose Print details during execution.
--help Display this help screen.
--version Display version information.

What’s the code look like?

Check out the config file classes used in batch processing. They are generated by JsonClassGen 🙂

namespace JsonClassGen
{

public class JsonClassGenConfig
{
public class Options2
{

[JsonProperty("useproperties")]
public bool Useproperties { get; set; }

[JsonProperty("pascal")]
public bool Pascal { get; set; }

[JsonProperty("nested")]
public bool Nested { get; set; }

[JsonProperty("singlefile")]
public bool Singlefile { get; set; }

[JsonProperty("internalvisibility")]
public bool Internalvisibility { get; set; }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s