jsonizeit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +9 -0
- package/package.json +16 -0
- package/src/jsonToCSharp.js +37 -0
- package/src/jsonToCsv.js +9 -0
- package/src/jsonToXml.js +13 -0
- package/test.js +11 -0
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jsonizeit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Convert JSON to C# Class, XML and CSV",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["json", "csharp", "xml", "csv", "converter"],
|
|
10
|
+
"author": "Olayinka Kareem",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"fast-xml-parser": "^5.4.1",
|
|
14
|
+
"json2csv": "^6.0.0-alpha.2"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
function toPascalCase(str) {
|
|
2
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function getCSharpType(value) {
|
|
6
|
+
if (Array.isArray(value)) return "List<object>";
|
|
7
|
+
switch (typeof value) {
|
|
8
|
+
case "string":
|
|
9
|
+
return "string";
|
|
10
|
+
case "number":
|
|
11
|
+
return Number.isInteger(value) ? "int" : "double";
|
|
12
|
+
case "boolean":
|
|
13
|
+
return "bool";
|
|
14
|
+
case "object":
|
|
15
|
+
return "object";
|
|
16
|
+
default:
|
|
17
|
+
return "string";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function jsonToCSharp(json, className = "Root") {
|
|
22
|
+
const obj = typeof json === "string" ? JSON.parse(json) : json;
|
|
23
|
+
|
|
24
|
+
let properties = Object.keys(obj)
|
|
25
|
+
.map(key => {
|
|
26
|
+
const type = getCSharpType(obj[key]);
|
|
27
|
+
return ` public ${type} ${toPascalCase(key)} { get; set; }`;
|
|
28
|
+
})
|
|
29
|
+
.join("\n");
|
|
30
|
+
|
|
31
|
+
return `public class ${className}
|
|
32
|
+
{
|
|
33
|
+
${properties}
|
|
34
|
+
}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = jsonToCSharp;
|
package/src/jsonToCsv.js
ADDED
package/src/jsonToXml.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const { XMLBuilder } = require("fast-xml-parser");
|
|
2
|
+
|
|
3
|
+
function jsonToXml(json) {
|
|
4
|
+
const obj = typeof json === "string" ? JSON.parse(json) : json;
|
|
5
|
+
const builder = new XMLBuilder({
|
|
6
|
+
ignoreAttributes: false,
|
|
7
|
+
format: true
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
return builder.build(obj);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = jsonToXml;
|
package/test.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const transformer = require("jsonizeit");
|
|
2
|
+
|
|
3
|
+
const sample = {
|
|
4
|
+
name: "John",
|
|
5
|
+
age: 30,
|
|
6
|
+
isActive: true
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
console.log(transformer.jsonToCSharp(sample, "Person"));
|
|
10
|
+
console.log(transformer.jsonToXml(sample));
|
|
11
|
+
console.log(transformer.jsonToCsv([sample]));
|