From 1b369c27456ef0376ea220c0e8d3f6cb501483a5 Mon Sep 17 00:00:00 2001 From: Mukesh Date: Thu, 14 Nov 2019 11:09:39 +0530 Subject: [PATCH 1/9] Update SwiftCodable.json --- languages/SwiftCodable.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/languages/SwiftCodable.json b/languages/SwiftCodable.json index 13d594e..737e509 100644 --- a/languages/SwiftCodable.json +++ b/languages/SwiftCodable.json @@ -98,7 +98,7 @@ "getter": "", "setter": "", "fileExtension": "swift", - "instanceVarDefinition": "\tlet : ?\n", + "instanceVarDefinition": "\tlet : \n", "modelEnd": "\n}", "staticImports": "import Foundation", "supportsCamelcasing": true, @@ -115,7 +115,7 @@ "signature": "\tinit(from decoder: Decoder) throws ", "bodyStart": "{\n\t\tlet values = try decoder.container(keyedBy: CodingKeys.self)\n", "bodyEnd": "\t}\n", - "codeForEachProperty": "\t\t = try values.decodeIfPresent(.self, forKey: .)\n", + "codeForEachProperty": "\t\t = try values.decode(.self, forKey: .)\n", "codeForUndefinedProperty": "\t\t = nil // TODO: Add code for decoding ``, It was null at the time of model creation.\n", "codeForUndefinedArrayProperty": "\t\t = [] // TODO: Add code for decoding ``, It was empty at the time of model creation.\n", "comment": "" @@ -126,7 +126,7 @@ "signature": "\tfunc encode(to encoder: Encoder) throws ", "bodyStart": "{\n\t\tvar container = encoder.container(keyedBy: CodingKeys.self)\n", "bodyEnd": "\t}\n", - "codeForEachProperty": "\t\ttry container.encodeIfPresent(, forKey: .)\n", + "codeForEachProperty": "\t\ttry container.encode(, forKey: .)\n", "codeForUndefinedProperty": "\t\t// TODO: Add code for encoding ``, It was null at the time of model creation.\n", "codeForUndefinedArrayProperty": "\t\t// TODO: Add code for encoding ``, It was empty at the time of model creation.\n", "comment": "", @@ -135,4 +135,4 @@ } }, "exampleCode": "## Example\n\n\n#### Decoding:\nThe example below shows how to decode an instance of `` type from JSON object.\n\n```swift\nlet json = \"\"\"\n\n\"\"\".data(using: .utf8)!\n\nlet decoder = JSONDecoder()\nlet model = try decoder.decode(.self, json)\n```\n\n\n#### Encoding: \nThe example below shows how to encode an instance of a simple `` type into a JSON object.\n\n```swift\nlet model = ()\n// Assign value to properties.\n\nlet encoder = JSONEncoder()\nencoder.outputFormatting = .prettyPrinted\n\nlet data = try encoder.encode(model)\nprint(String(data: data, encoding: .utf8)!)\n```" -} \ No newline at end of file +} From 08718b4f19fbd44140e3bcbf02d94f5120ca6ab8 Mon Sep 17 00:00:00 2001 From: Mukesh Date: Wed, 17 Mar 2021 13:48:43 +0530 Subject: [PATCH 2/9] added variable type for swift and kotlin let/var, val/var --- index.html | 2 ++ languages/KotlinGson.json | 2 +- languages/KotlinMoshi.json | 2 +- languages/SwiftCodable.json | 2 +- languages/SwiftDictionary.json | 2 +- languages/SwiftyJSON.json | 2 +- script/parser.js | 18 +++++++---- script/script.js | 59 ++++++++++++++++++++++++++++++++-- 8 files changed, 76 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index 18d5c2c..8afe450 100644 --- a/index.html +++ b/index.html @@ -112,6 +112,8 @@
+
+
diff --git a/languages/KotlinGson.json b/languages/KotlinGson.json index 97cdee8..a3c7f9f 100644 --- a/languages/KotlinGson.json +++ b/languages/KotlinGson.json @@ -115,7 +115,7 @@ "modelDefinition": "\ndata class (\n) ", "fileExtension": "kt", - "codeForEachProperty": "\n\t\t@SerializedName(\"\") val : ", + "codeForEachProperty": "\n\t\t@SerializedName(\"\")\n\t\t : ", "modelEnd": "}", "staticImports": "\nimport com.google.gson.annotations.SerializedName\nimport com.google.gson.Gson\nimport com.google.gson.GsonBuilder", "supportsCamelcasing": true, diff --git a/languages/KotlinMoshi.json b/languages/KotlinMoshi.json index 35fd734..a48b62e 100644 --- a/languages/KotlinMoshi.json +++ b/languages/KotlinMoshi.json @@ -115,7 +115,7 @@ "modelDefinition": "\ndata class (\n) ", "fileExtension": "kt", - "codeForEachProperty": "\n\t\t@Json(\"\") val : ", + "codeForEachProperty": "\n\t\t@Json(\"\")\n\t\t : ", "modelEnd": "}", "staticImports": "\nimport com.squareup.moshi.Json\nimport com.squareup.moshi.Moshi\nimport com.squareup.moshi.JsonAdapter\nimport com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory", "supportsCamelcasing": true, diff --git a/languages/SwiftCodable.json b/languages/SwiftCodable.json index 737e509..af2f158 100644 --- a/languages/SwiftCodable.json +++ b/languages/SwiftCodable.json @@ -98,7 +98,7 @@ "getter": "", "setter": "", "fileExtension": "swift", - "instanceVarDefinition": "\tlet : \n", + "instanceVarDefinition": "\t : \n", "modelEnd": "\n}", "staticImports": "import Foundation", "supportsCamelcasing": true, diff --git a/languages/SwiftDictionary.json b/languages/SwiftDictionary.json index 62f174c..932c47e 100644 --- a/languages/SwiftDictionary.json +++ b/languages/SwiftDictionary.json @@ -114,7 +114,7 @@ "getter": "", "setter": "", "fileExtension": "swift", - "instanceVarDefinition": "\tlet : ?\n", + "instanceVarDefinition": "\t : ?\n", "modelEnd": "\n}", "staticImports": "import Foundation", "supportsCamelcasing": true, diff --git a/languages/SwiftyJSON.json b/languages/SwiftyJSON.json index d39af9f..3234598 100644 --- a/languages/SwiftyJSON.json +++ b/languages/SwiftyJSON.json @@ -114,7 +114,7 @@ "getter": "", "setter": "", "fileExtension": "swift", - "instanceVarDefinition": "\tlet : ?\n", + "instanceVarDefinition": "\t : ?\n", "modelEnd": "\n}", "staticImports": "import Foundation\nimport SwiftyJSON", "supportsCamelcasing": true, diff --git a/script/parser.js b/script/parser.js index 0b71011..599d250 100644 --- a/script/parser.js +++ b/script/parser.js @@ -13,7 +13,7 @@ let constKeyName = "" let additionalCustomTypeProperty = "" let modelIdentifier = "" let modelDefinitionProperties = "" - +let varNameKey = "" // FileBuilder function FileBuilder(language) { @@ -59,7 +59,7 @@ FileBuilder.prototype.getFiles = function(fileName, object, language) { var _this = this; keys.forEach(function(key) { var value = obj[key]; - var property = getProperty(key, value, language); + var property = getProperty(key, value, language, _this.varTypes); var prevIndex = properties.map(function(prop) { return prop.propertyName; @@ -82,12 +82,13 @@ FileBuilder.prototype.getFiles = function(fileName, object, language) { file.isInitializers = this.isInitializers; file.modelIdentifier = this.modelIdentifier; file.methods = this.methods; + file.varTypes = this.varTypes; files.splice(0, 0, file); return files; } -function getProperty(key, value, language) { +function getProperty(key, value, language, varTypes) { let propertyName = getLanguageName(key, language); let valueType = getPropertyTypeName(value, language); @@ -95,6 +96,7 @@ function getProperty(key, value, language) { property.jsonKeyName = key; property.propertyName = propertyName; property.sampleValue = value; + property.varTypes = varTypes if (value instanceof Array) { if (value[0] && (value[0] instanceof Object)) { @@ -225,6 +227,7 @@ Property.prototype = { toString: function() { var content = this.language.instanceVarDefinition; + content = content.replaceAll(varNameKey, this.varTypes); content = content.replaceAll(varType, this.propertyType); content = content.replaceAll(varName, this.propertyName); content = content.replaceAll(jsonKeyName, this.jsonKeyName); @@ -393,8 +396,13 @@ FileRepresenter.prototype = { if (this.language.codeForEachProperty) { var _language = this.language; + var _this = this; let propertyList = this.properties.map(function (property) { - return _language.codeForEachProperty.replaceAll(varName, property.propertyName).replaceAll(jsonKeyName, property.jsonKeyName).replaceAll(varType, property.propertyType); + return _language.codeForEachProperty + .replaceAll(varNameKey, _this.varTypes) + .replaceAll(varName, property.propertyName) + .replaceAll(jsonKeyName, property.jsonKeyName) + .replaceAll(varType, property.propertyType); }).join(","); modelDefinition = modelDefinition.replaceAll(modelDefinitionProperties, propertyList); @@ -427,8 +435,6 @@ FileRepresenter.prototype = { } } - - Date.prototype.dateString = function() { var monthNames = [ diff --git a/script/script.js b/script/script.js index 729e88e..627ce02 100644 --- a/script/script.js +++ b/script/script.js @@ -16,6 +16,15 @@ let languages = { key: "class", value: "Class", checked: "" + }], + varTypes: [{ + key: "let", + value: "let", + checked: "checked" + },{ + key: "var", + value: "var", + checked: "" }] }, swiftyjson: { @@ -30,6 +39,15 @@ let languages = { key: "class", value: "Class", checked: "" + }], + varTypes: [{ + key: "let", + value: "let", + checked: "checked" + },{ + key: "var", + value: "var", + checked: "" }] }, dictionary: { @@ -44,6 +62,15 @@ let languages = { key: "class", value: "Class", checked: "" + }], + varTypes: [{ + key: "let", + value: "let", + checked: "checked" + },{ + key: "var", + value: "var", + checked: "" }] } } @@ -74,12 +101,30 @@ let languages = { gson: { mode: "text/x-kotlin", name: "Gson", - file: "KotlinGson.json" + file: "KotlinGson.json", + varTypes: [{ + key: "val", + value: "val", + checked: "checked" + },{ + key: "var", + value: "var", + checked: "" + }] }, moshi: { mode: "text/x-kotlin", name: "Moshi", - file: "KotlinMoshi.json" + file: "KotlinMoshi.json", + varTypes: [{ + key: "val", + value: "val", + checked: "checked" + },{ + key: "var", + value: "var", + checked: "" + }] } } }, @@ -175,6 +220,7 @@ function getAllClasses(language) { builder.isInitializers = $('#initializerCheckbox').is(':checked'); builder.rootClassName = $('#rootClassName').val(); builder.modelIdentifier = $('[name="modelIdentifierRadios"]:checked').val(); + builder.varTypes = $('[name="modelVariableRadios"]:checked').val(); var methods = new Array(); let checkedMethods = $('.method-checkbox:checked'); @@ -247,6 +293,15 @@ function updateSelectedFramework() { }); } + $("#modelVariableTypes").empty(); + if (selectedFramework.varTypes) { + $("#modelVariableTypes").append(''); + selectedFramework.varTypes.forEach(function(identifier){ + let element = '
'; + $("#modelVariableTypes").append(element); + }); + } + $('.model-identifier-radio').click(function () { inputChanged(); }); From d13c9c7872c8d17d7b1b0cbdb080ebe130f52083 Mon Sep 17 00:00:00 2001 From: Aniket Patil Date: Wed, 25 Aug 2021 18:15:33 +0530 Subject: [PATCH 3/9] Add X++ Language --- languages/xppJSON.json | 115 +++++++++++++++++++++++++++++++++++++++++ script/script.js | 12 ++++- 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 languages/xppJSON.json diff --git a/languages/xppJSON.json b/languages/xppJSON.json new file mode 100644 index 0000000..2cdbce2 --- /dev/null +++ b/languages/xppJSON.json @@ -0,0 +1,115 @@ +{ + "modelStart": "\n{\n", + "reservedKeywords": [ + "abstract", + "as", + "base", + "boolean", + "break", + "byte", + "case", + "catch", + "char", + "checked", + "class", + "const", + "continue", + "real", + "default", + "delegate", + "do", + "double", + "else", + "enum", + "event", + "explicit", + "extern", + "false", + "finally", + "fixed", + "float", + "for", + "foreach", + "goto", + "if", + "implicit", + "in", + "int", + "interface", + "internal", + "is", + "lock", + "long", + "namespace", + "new", + "null", + "object", + "operator", + "out", + "override", + "params", + "private", + "protected", + "public", + "readonly", + "ref", + "return", + "sbyte", + "sealed", + "short", + "sizeof", + "stackalloc", + "static", + "str", + "struct", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "uint", + "ulong", + "unchecked", + "unsafe", + "ushort", + "using", + "virtual", + "void", + "volatile", + "while" + ], + + "dataTypes": { + "arrayType": "List", + "generic": "object", + "string": "str", + "boolean": "boolean", + "float": "real", + "doubleType": "real", + "characterType": "real", + "longType": "long", + "int": "int" + }, + + "modelDefinition": "\n[DataContract]\npublic class ", + "fileExtension": "cs", + "instanceVarDefinition": "\n\tprivate ;", + "modelEnd": "\n}", + "staticImports": "", + "supportsCamelcasing": true, + "methods": { + "others": { + "getterSetter": { + "name": "DataMembers", + "signature": "", + "bodyStart": "", + "bodyEnd": "", + "codeForEachProperty": "\n\t[DataMember]\n\tpublic void ( _ = ) {\n\t\t = _;\n\t\treturn ;\n\t}\n", + "comment": "", + "checked": "checked" + } + } + }, + "exampleCode": "" +} \ No newline at end of file diff --git a/script/script.js b/script/script.js index 627ce02..38f2643 100644 --- a/script/script.js +++ b/script/script.js @@ -140,9 +140,19 @@ let languages = { mode: "text/x-csharp", name: "C# Class", file: "CSharpClass.json" + } + } + }, + xpp: { + name: "X++", + frameworks: { + xppClass: { + mode: "text/x-csharp", + name: "X++ Contract Class", + file: "xppJSON.json" + } } } - } }; var selectedLanguage = languages.swift; From e74abe2a1946c0bc3972a381699e1c3e24ccbda5 Mon Sep 17 00:00:00 2001 From: Aniket Patil Date: Thu, 26 Aug 2021 10:18:03 +0530 Subject: [PATCH 4/9] X++ Fix for collection classes and example --- languages/xppJSON.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/languages/xppJSON.json b/languages/xppJSON.json index 2cdbce2..62000ef 100644 --- a/languages/xppJSON.json +++ b/languages/xppJSON.json @@ -1,5 +1,5 @@ { - "modelStart": "\n{\n", + "modelStart": "\n{", "reservedKeywords": [ "abstract", "as", @@ -97,7 +97,7 @@ "instanceVarDefinition": "\n\tprivate ;", "modelEnd": "\n}", "staticImports": "", - "supportsCamelcasing": true, + "supportsCamelcasing": false, "methods": { "others": { "getterSetter": { @@ -105,11 +105,11 @@ "signature": "", "bodyStart": "", "bodyEnd": "", - "codeForEachProperty": "\n\t[DataMember]\n\tpublic void ( _ = ) {\n\t\t = _;\n\t\treturn ;\n\t}\n", - "comment": "", + "codeForEachProperty": "\n\t[DataMember]\n\tpublic ( _ = )\n\t{\n\t\t = _;\n\t\treturn ;\n\t}\n", + "codeForEachArrayProperty": "\n\t[DataMember,\n\t AifCollectionTypeAttribute(\"_\",Types::Class), classstr(putClassName),\n\t AifCollectionTypeAttribute(\"_return\",Types::Class), classstr(putClassName)]\n\tpublic ( _ = )\n\t{\n\t\t = _;\n\t\treturn ;\n\t}", "checked": "checked" } } }, - "exampleCode": "" + "exampleCode": "## Example\n\n\n#### Decoding:\nThe example below shows how to decode an instance of `` type from JSON object.\n\n```csharp\nstring json = \"...\"; // Your JSON String.\n\n model = JsonConvert.DeserializeObject<>(json);\n```\n\n\n#### Encoding: \nThe example below shows how to encode an instance of a simple `` type into a JSON object.\n\n```java\n model = new ();\n// Assign value to variables.\n\nstring json = JsonConvert.SerializeObject(model);\n```" } \ No newline at end of file From 8a0ae6d677ba039a26e40c581a740bed31231fb4 Mon Sep 17 00:00:00 2001 From: Aniket Patil Date: Thu, 26 Aug 2021 15:40:43 +0530 Subject: [PATCH 5/9] Add informative details --- README.md | 3 +++ index.html | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cabe72e..4305542 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,6 @@ Visit: https://jsonmaster.github.io - [Newtonsoft](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) - C# Class +4. **[X++](https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-language-reference)** + - [Newtonsoft](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) + - C# Class \ No newline at end of file diff --git a/index.html b/index.html index 8afe450..8fa18b4 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - JSON Master: Generate Swift, Kotlin, Java, C# from JSON + JSON Master: Generate Swift, Kotlin, Java, C#, X++ from JSON From b032291cb10864e0db974d6f44826626d3d7e5e2 Mon Sep 17 00:00:00 2001 From: Aniket Patil Date: Thu, 26 Aug 2021 15:41:55 +0530 Subject: [PATCH 6/9] Update README.md --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 4305542..b102744 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,4 @@ Visit: https://jsonmaster.github.io - [Newtonsoft](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) - C# Class -4. **[X++](https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-language-reference)** - - [Newtonsoft](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm) - - C# Class \ No newline at end of file +4. **[X++](https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-language-reference)** \ No newline at end of file From e937623138c3205cb06846575a012db3b5557f0c Mon Sep 17 00:00:00 2001 From: Aniket Patil Date: Mon, 30 Aug 2021 17:33:21 +0530 Subject: [PATCH 7/9] Update CSS Remove white box for cleaner look --- css/style.css | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/css/style.css b/css/style.css index dfbb73d..5b03dc4 100644 --- a/css/style.css +++ b/css/style.css @@ -1,13 +1,28 @@ -.form-font { - font-size: small; -} - -.CodeMirror { - border: 0px solid #eee; - height: auto; - min-height: 400px; - font-size: small; - line-height: inherit; -} - +.form-font { + font-size: small; +} + +.CodeMirror { + border: 0px solid #eee; + height: auto; + min-height: 400px; + font-size: small; + line-height: inherit; +} + +.form-control { + display: block; + width: 100%; + height: calc(2.25rem + 2px); + padding: .375rem .75rem; + font-size: 1rem; + line-height: 1.5; + color: #495057; + background-color: #fff0; + background-clip: padding-box; + border: 1px solid #ced4da00; + border-radius: .25rem; + transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out; +} + pre {-moz-tab-size: 4; tab-size: 4; display: block; padding: 0.5em; color: #333; background: #f8f8ff; border: 1px solid #ccc; border-radius: 4px; } \ No newline at end of file From b8451bcc9cd33c86f2e1524f44e99820ebd7f4e5 Mon Sep 17 00:00:00 2001 From: Aniket Patil Date: Thu, 2 Sep 2021 12:38:19 +0530 Subject: [PATCH 8/9] Add X++ parm style --- css/style.css | 2 +- languages/xppParmJSON.json | 115 +++++++++++++++++++++++++++++++++++++ script/script.js | 5 ++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 languages/xppParmJSON.json diff --git a/css/style.css b/css/style.css index 5b03dc4..a4a5236 100644 --- a/css/style.css +++ b/css/style.css @@ -10,7 +10,7 @@ line-height: inherit; } -.form-control { +.form-control[class = 'form-control mr-sm-2']{ display: block; width: 100%; height: calc(2.25rem + 2px); diff --git a/languages/xppParmJSON.json b/languages/xppParmJSON.json new file mode 100644 index 0000000..03f3f4c --- /dev/null +++ b/languages/xppParmJSON.json @@ -0,0 +1,115 @@ +{ + "modelStart": "\n{", + "reservedKeywords": [ + "abstract", + "as", + "base", + "boolean", + "break", + "byte", + "case", + "catch", + "char", + "checked", + "class", + "const", + "continue", + "real", + "default", + "delegate", + "do", + "double", + "else", + "enum", + "event", + "explicit", + "extern", + "false", + "finally", + "fixed", + "float", + "for", + "foreach", + "goto", + "if", + "implicit", + "in", + "int", + "interface", + "internal", + "is", + "lock", + "long", + "namespace", + "new", + "null", + "object", + "operator", + "out", + "override", + "params", + "private", + "protected", + "public", + "readonly", + "ref", + "return", + "sbyte", + "sealed", + "short", + "sizeof", + "stackalloc", + "static", + "str", + "struct", + "switch", + "this", + "throw", + "true", + "try", + "typeof", + "uint", + "ulong", + "unchecked", + "unsafe", + "ushort", + "using", + "virtual", + "void", + "volatile", + "while" + ], + + "dataTypes": { + "arrayType": "List", + "generic": "object", + "string": "str", + "boolean": "boolean", + "float": "real", + "doubleType": "real", + "characterType": "real", + "longType": "long", + "int": "int" + }, + + "modelDefinition": "\n[DataContract]\npublic class ", + "fileExtension": "cs", + "instanceVarDefinition": "\n\tprivate ;", + "modelEnd": "\n}", + "staticImports": "", + "supportsCamelcasing": false, + "methods": { + "others": { + "getterSetter": { + "name": "DataMembers", + "signature": "", + "bodyStart": "", + "bodyEnd": "", + "codeForEachProperty": "\n\t[DataMember('')]\n\tpublic parm( _ = )\n\t{\n\t\t = _;\n\t\treturn ;\n\t}\n", + "codeForEachArrayProperty": "\n\t[DataMember(''),\n\t AifCollectionTypeAttribute(\"_\",Types::Class), classstr(putClassName),\n\t AifCollectionTypeAttribute(\"_return\",Types::Class), classstr(putClassName)]\n\tpublic parm( _ = )\n\t{\n\t\t = _;\n\t\treturn ;\n\t}", + "checked": "checked" + } + } + }, + "exampleCode": "## Example\n\n\n#### Decoding:\nThe example below shows how to decode an instance of `` type from JSON object.\n\n```csharp\nstring json = \"...\"; // Your JSON String.\n\n model = JsonConvert.DeserializeObject<>(json);\n```\n\n\n#### Encoding: \nThe example below shows how to encode an instance of a simple `` type into a JSON object.\n\n```java\n model = new ();\n// Assign value to variables.\n\nstring json = JsonConvert.SerializeObject(model);\n```" +} \ No newline at end of file diff --git a/script/script.js b/script/script.js index 38f2643..d5aa658 100644 --- a/script/script.js +++ b/script/script.js @@ -150,6 +150,11 @@ let languages = { mode: "text/x-csharp", name: "X++ Contract Class", file: "xppJSON.json" + }, + xppParmClass: { + mode: "text/x-csharp", + name: "X++ Parm Contract Class", + file: "xppParmJSON.json" } } } From bffc9ddb70038eed382735d7f94f5162a4c19209 Mon Sep 17 00:00:00 2001 From: Mukesh Yadav Date: Sat, 10 Sep 2022 15:10:12 +0530 Subject: [PATCH 9/9] added optional support --- index.html | 2 ++ languages/KotlinGson.json | 1 + languages/KotlinMoshi.json | 1 + languages/SwiftCodable.json | 41 ++++++++++++++++++++----------------- script/parser.js | 16 ++++++++++++--- script/script.js | 37 +++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index 8fa18b4..68b3e2c 100644 --- a/index.html +++ b/index.html @@ -114,6 +114,8 @@
+
+
diff --git a/languages/KotlinGson.json b/languages/KotlinGson.json index a3c7f9f..5acbc7a 100644 --- a/languages/KotlinGson.json +++ b/languages/KotlinGson.json @@ -116,6 +116,7 @@ "modelDefinition": "\ndata class (\n) ", "fileExtension": "kt", "codeForEachProperty": "\n\t\t@SerializedName(\"\")\n\t\t : ", + "codeForEachPropertyOptional": "\n\t\t@SerializedName(\"\")\n\t\t : ?", "modelEnd": "}", "staticImports": "\nimport com.google.gson.annotations.SerializedName\nimport com.google.gson.Gson\nimport com.google.gson.GsonBuilder", "supportsCamelcasing": true, diff --git a/languages/KotlinMoshi.json b/languages/KotlinMoshi.json index a48b62e..f1b8ebe 100644 --- a/languages/KotlinMoshi.json +++ b/languages/KotlinMoshi.json @@ -116,6 +116,7 @@ "modelDefinition": "\ndata class (\n) ", "fileExtension": "kt", "codeForEachProperty": "\n\t\t@Json(\"\")\n\t\t : ", + "codeForEachPropertyOptional": "\n\t\t@Json(\"\")\n\t\t : ?", "modelEnd": "}", "staticImports": "\nimport com.squareup.moshi.Json\nimport com.squareup.moshi.Moshi\nimport com.squareup.moshi.JsonAdapter\nimport com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory", "supportsCamelcasing": true, diff --git a/languages/SwiftCodable.json b/languages/SwiftCodable.json index af2f158..e4bfef7 100644 --- a/languages/SwiftCodable.json +++ b/languages/SwiftCodable.json @@ -99,38 +99,41 @@ "setter": "", "fileExtension": "swift", "instanceVarDefinition": "\t : \n", + "instanceVarDefinitionOptional": "\t : ?\n", "modelEnd": "\n}", "staticImports": "import Foundation", "supportsCamelcasing": true, "methods": { - "constructorName": "Initializer", - "constructorChecked": "checked", - "constructors": [{ - "signature": "\tprivate enum CodingKeys: String, CodingKey ", - "bodyStart": "{\n", - "bodyEnd": "\t}\n", - "codeForEachProperty": "\t\tcase = \"\"\n", - "comment": "" - },{ - "signature": "\tinit(from decoder: Decoder) throws ", - "bodyStart": "{\n\t\tlet values = try decoder.container(keyedBy: CodingKeys.self)\n", - "bodyEnd": "\t}\n", - "codeForEachProperty": "\t\t = try values.decode(.self, forKey: .)\n", - "codeForUndefinedProperty": "\t\t = nil // TODO: Add code for decoding ``, It was null at the time of model creation.\n", - "codeForUndefinedArrayProperty": "\t\t = [] // TODO: Add code for decoding ``, It was empty at the time of model creation.\n", - "comment": "" - }], "others": { + "condingKeys": { + "name": "CodingKeys", + "signature": "\tprivate enum CodingKeys: String, CodingKey ", + "bodyStart": "{\n", + "bodyEnd": "\t}\n", + "codeForEachProperty": "\t\tcase = \"\"\n", + "comment": "" + }, + "initializer": { + "name": "Initializer", + "signature": "\tinit(from decoder: Decoder) throws ", + "bodyStart": "{\n\t\tlet values = try decoder.container(keyedBy: CodingKeys.self)\n", + "bodyEnd": "\t}\n", + "codeForEachProperty": "\t\t = try values.decode(.self, forKey: .)\n", + "codeForEachPropertyOptional": "\t\t = try values.decodeIfPresent(.self, forKey: .)\n", + "codeForUndefinedProperty": "\t\t? = nil // TODO: Add code for decoding ``, It was null at the time of model creation.\n", + "codeForUndefinedArrayProperty": "\t\t = [] // TODO: Add code for decoding ``, It was empty at the time of model creation.\n", + "comment": "" + }, "encoder": { "name": "Encoding", "signature": "\tfunc encode(to encoder: Encoder) throws ", "bodyStart": "{\n\t\tvar container = encoder.container(keyedBy: CodingKeys.self)\n", "bodyEnd": "\t}\n", "codeForEachProperty": "\t\ttry container.encode(, forKey: .)\n", + "codeForEachPropertyOptional": "\t\ttry container.encodeIfPresent(, forKey: .)\n", "codeForUndefinedProperty": "\t\t// TODO: Add code for encoding ``, It was null at the time of model creation.\n", "codeForUndefinedArrayProperty": "\t\t// TODO: Add code for encoding ``, It was empty at the time of model creation.\n", - "comment": "", - "checked": "checked" + "comment": "" } } }, diff --git a/script/parser.js b/script/parser.js index 599d250..40ce2ee 100644 --- a/script/parser.js +++ b/script/parser.js @@ -60,6 +60,7 @@ FileBuilder.prototype.getFiles = function(fileName, object, language) { keys.forEach(function(key) { var value = obj[key]; var property = getProperty(key, value, language, _this.varTypes); + property.isOptional = _this.isOptional var prevIndex = properties.map(function(prop) { return prop.propertyName; @@ -226,6 +227,9 @@ Property.prototype = { constructor: Property, toString: function() { var content = this.language.instanceVarDefinition; + if (this.isOptional) { + content = this.language.instanceVarDefinitionOptional; + } content = content.replaceAll(varNameKey, this.varTypes); content = content.replaceAll(varType, this.propertyType); @@ -323,6 +327,9 @@ FileRepresenter.prototype = { } var propertyString = method.codeForEachProperty; + if (property.isOptional && method.codeForEachPropertyOptional) { + propertyString = method.codeForEachPropertyOptional; + } if (property.isCustomClass) { if (method.codeForEachCustomProperty) { @@ -393,12 +400,15 @@ FileRepresenter.prototype = { var modelDefinition = this.language.modelDefinition.replaceAll(modelName, this.className).replaceAll(modelIdentifier, this.modelIdentifier); + var codeForEachProperty = this.language.codeForEachProperty + if (this.language.isOptional && this.language.codeForEachPropertyOptional) { + codeForEachProperty = this.language.codeForEachPropertyOptional + } - if (this.language.codeForEachProperty) { - var _language = this.language; + if (codeForEachProperty) { var _this = this; let propertyList = this.properties.map(function (property) { - return _language.codeForEachProperty + return codeForEachProperty .replaceAll(varNameKey, _this.varTypes) .replaceAll(varName, property.propertyName) .replaceAll(jsonKeyName, property.jsonKeyName) diff --git a/script/script.js b/script/script.js index d5aa658..a279203 100644 --- a/script/script.js +++ b/script/script.js @@ -25,6 +25,15 @@ let languages = { key: "var", value: "var", checked: "" + }], + optionalTypes: [{ + key: "optional", + value: "Optional", + checked: "" + },{ + key: "non-optional", + value: "Non Optional", + checked: "checked" }] }, swiftyjson: { @@ -110,6 +119,15 @@ let languages = { key: "var", value: "var", checked: "" + }], + optionalTypes: [{ + key: "optional", + value: "Optional", + checked: "" + },{ + key: "non-optional", + value: "Non Optional", + checked: "checked" }] }, moshi: { @@ -124,6 +142,15 @@ let languages = { key: "var", value: "var", checked: "" + }], + optionalTypes: [{ + key: "optional", + value: "Optional", + checked: "" + },{ + key: "non-optional", + value: "Non Optional", + checked: "checked" }] } } @@ -236,6 +263,7 @@ function getAllClasses(language) { builder.rootClassName = $('#rootClassName').val(); builder.modelIdentifier = $('[name="modelIdentifierRadios"]:checked').val(); builder.varTypes = $('[name="modelVariableRadios"]:checked').val(); + builder.isOptional = $('[name="modelOptionalRadios"]:checked').val() == "optional"; var methods = new Array(); let checkedMethods = $('.method-checkbox:checked'); @@ -317,6 +345,15 @@ function updateSelectedFramework() { }); } + $("#optionalTypes").empty(); + if (selectedFramework.optionalTypes) { + $("#optionalTypes").append(''); + selectedFramework.optionalTypes.forEach(function(identifier){ + let element = '
'; + $("#optionalTypes").append(element); + }); + } + $('.model-identifier-radio').click(function () { inputChanged(); });