j2m
j2m
is short for Json to Model
.
Generate model according to json data. Why? Once you have the model, you can deserialize json to model.
You can specify either a json file or multiple json files or a folder which contains one or more json files as input. Multiple json files will be deep-merged into one json file before processing.
todo
- auto test upon saving
- gulp
- best practice to write documentation
- TypeScript
- allow user to specify template
Installation
npm install json2model
Usage
Command line
Generate account
model in C#:
Specify a folder which contains json files for model account
:
j2m -l cs -n account -f test/fixtures/json/account/
Specify a json file which is the definition for model account
:
j2m -l cs -n account -j test/fixtures/json/account/130829004.json
Node.js
const JsonHelper = ;const json = JsonHelper;// Or read multiple json files: const json = JsonHelper.readFiles(['account1.json', 'account2.json']);// Or read from folder: const json = JsonHelper.readFolder('./account/');const LanguageController = ; // language is C#const model = LanguageController; // model name is "account"console;
Example
account.json
:
Generated C# model:
public partial class Account{ public string uri { get; set; } public int? id { get; set; } public ServiceInfo serviceInfo { get; set; } public Operator @operator { get; set; } public string mainNumber { get; set; } public string status { get; set; } public SignupInfo signupInfo { get; set; } public string setupWizardState { get; set; } public class ServiceInfo { public string uri { get; set; } public Brand brand { get; set; } public ServicePlan servicePlan { get; set; } public BillingPlan billingPlan { get; set; } public class Brand { public string id { get; set; } public string name { get; set; } public HomeCountry homeCountry { get; set; } public class HomeCountry { public string id { get; set; } public string uri { get; set; } public string name { get; set; } public string isoCode { get; set; } public string callingCode { get; set; } } } public class ServicePlan { public string id { get; set; } public string name { get; set; } public string edition { get; set; } } public class BillingPlan { public string id { get; set; } public string name { get; set; } public string durationUnit { get; set; } public int? duration { get; set; } public string type { get; set; } } } public class Operator { public string uri { get; set; } public int? id { get; set; } public string extensionNumber { get; set; } } public class SignupInfo { }}
Usage of generated model
using Newtonsoft.Json; var content = File.ReadAllText("account.json");var account = JsonConvert.DeserilizeObject<Account>(content);
Supported languages
- C#