Skip to content

Commit a6b89c4

Browse files
authored
Merge pull request #13 from mukeshydv/development
Development
2 parents 02b076f + c3b493d commit a6b89c4

File tree

4 files changed

+165
-8
lines changed

4 files changed

+165
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Visit: https://jsonmaster.github.io
1111
2. **Java**
1212
- [Gson](https://github.com/google/gson)
1313
- [Android/JSONObject](https://developer.android.com/reference/org/json/JSONObject)
14-
3. **C#**
14+
3. **Kotlin**
15+
- [Gson](https://github.com/google/gson)
16+
4. **C#**
1517
- [Newtonsoft](https://www.newtonsoft.com/json/help/html/SerializingJSON.htm)
1618
- C# Class

languages/KotlinGson.json

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"modelStart": "",
3+
"reservedKeywords": [
4+
"field",
5+
"dynamic",
6+
"delegate",
7+
"constructor",
8+
"by",
9+
"when",
10+
"var",
11+
"val",
12+
"typealias",
13+
"object",
14+
"is",
15+
"in",
16+
"fun",
17+
"as",
18+
"abstract",
19+
"continue",
20+
"for",
21+
"new",
22+
"null",
23+
"switch",
24+
"assert",
25+
"default",
26+
"if",
27+
"package",
28+
"synchronized",
29+
"boolean",
30+
"do",
31+
"goto",
32+
"private",
33+
"this",
34+
"break",
35+
"double",
36+
"implements",
37+
"protected",
38+
"throw",
39+
"byte",
40+
"else",
41+
"import",
42+
"public",
43+
"throws",
44+
"case",
45+
"enum",
46+
"instanceof",
47+
"return",
48+
"transient",
49+
"catch",
50+
"extends",
51+
"int",
52+
"short",
53+
"try",
54+
"char",
55+
"final",
56+
"interface",
57+
"static",
58+
"void",
59+
"class",
60+
"finally",
61+
"long",
62+
"strictfp",
63+
"volatile",
64+
"const",
65+
"float",
66+
"native",
67+
"super",
68+
"while",
69+
"file",
70+
"get",
71+
"init",
72+
"param",
73+
"property",
74+
"receiver",
75+
"set",
76+
"setparam",
77+
"where",
78+
"actual",
79+
"annotation",
80+
"companion",
81+
"const",
82+
"crossinline",
83+
"data",
84+
"enum",
85+
"expect",
86+
"external",
87+
"infix",
88+
"inline",
89+
"inner",
90+
"internal",
91+
"lateinit",
92+
"noinline",
93+
"open",
94+
"operator",
95+
"out",
96+
"reified",
97+
"sealed",
98+
"suspend",
99+
"tailrec",
100+
"vararg",
101+
"it"
102+
],
103+
104+
"dataTypes": {
105+
"arrayType": "List<<!ElementType!>>",
106+
"generic": "Any",
107+
"string": "String",
108+
"boolean": "Boolean",
109+
"float": "Double",
110+
"doubleType": "Double",
111+
"characterType": "Int",
112+
"longType": "Long",
113+
"int": "Int"
114+
},
115+
116+
"modelDefinition": "\ndata class <!ModelName!> (<!ModelDefinitionProperty!>\n) ",
117+
"fileExtension": "kt",
118+
"codeForEachProperty": "\n\t\t@SerializedName(\"<!JsonKeyName!>\") val <!VarName!>: <!VarType!>",
119+
"modelEnd": "",
120+
"staticImports": "\nimport com.google.gson.annotations.SerializedName",
121+
"supportsCamelcasing": true,
122+
"methods": {
123+
}
124+
}

script/parser.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let jsonKeyName = "<!JsonKeyName!>"
1212
let constKeyName = "<!ConstKeyName!>"
1313
let additionalCustomTypeProperty = "<!AdditionalForCustomTypeProperty!>"
1414
let modelIdentifier = "<!ModelIdentifier!>"
15+
let modelDefinitionProperties = "<!ModelDefinitionProperty!>"
1516

1617

1718
// FileBuilder
@@ -255,13 +256,17 @@ FileRepresenter.prototype = {
255256
},
256257

257258
addProperties: function() {
258-
var propertyString = "\n";
259259

260-
this.properties.forEach(function(property) {
261-
propertyString += property.toString();
262-
});
260+
if (this.language.instanceVarDefinition) {
261+
var propertyString = "\n";
262+
263+
this.properties.forEach(function(property) {
264+
propertyString += property.toString();
265+
});
263266

264-
return propertyString;
267+
return propertyString;
268+
}
269+
return "";
265270
},
266271

267272
addSetterGetter: function() {
@@ -375,15 +380,31 @@ FileRepresenter.prototype = {
375380
return content;
376381
},
377382

383+
addModelDefinition: function() {
384+
385+
var modelDefinition = this.language.modelDefinition.replaceAll(modelName, this.className).replaceAll(modelIdentifier, this.modelIdentifier);
386+
387+
388+
if (this.language.codeForEachProperty) {
389+
var _language = this.language;
390+
let propertyList = this.properties.map(function (property) {
391+
return _language.codeForEachProperty.replaceAll(varName, property.propertyName).replaceAll(jsonKeyName, property.jsonKeyName).replaceAll(varType, property.propertyType);
392+
}).join(",");
393+
394+
modelDefinition = modelDefinition.replaceAll(modelDefinitionProperties, propertyList);
395+
}
396+
397+
return modelDefinition;
398+
},
399+
378400
toString: function() {
379401
var content = "";
380402

381403
content = content + this.addFirstLine();
382404
content = content + this.addCopyright();
383405
content = content + this.addImports();
384406

385-
let modelDefinition = this.language.modelDefinition.replaceAll(modelName, this.className).replaceAll(modelIdentifier, this.modelIdentifier);
386-
content = content + modelDefinition;
407+
content = content + this.addModelDefinition();
387408
content = content + this.language.modelStart;
388409

389410
content = content + this.addProperties();

script/script.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ let languages = {
6161
}
6262
}
6363
},
64+
kotlin: {
65+
name: "Kotlin",
66+
frameworks: {
67+
gson: {
68+
mode: "text/x-java",
69+
name: "Gson",
70+
file: "KotlinGson.json"
71+
}
72+
}
73+
},
6474
csharo: {
6575
name: "C#",
6676
frameworks: {

0 commit comments

Comments
 (0)