Skip to content

Commit 748126f

Browse files
committed
feat(new tool): JSON to C#
1 parent e876d03 commit 748126f

File tree

7 files changed

+84
-13
lines changed

7 files changed

+84
-13
lines changed

components.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ declare module '@vue/runtime-core' {
108108
Ipv6UlaGenerator: typeof import('./src/tools/ipv6-ula-generator/ipv6-ula-generator.vue')['default']
109109
JsonDiff: typeof import('./src/tools/json-diff/json-diff.vue')['default']
110110
JsonMinify: typeof import('./src/tools/json-minify/json-minify.vue')['default']
111+
JsonToCsharp: typeof import('./src/tools/json-to-csharp/json-to-csharp.vue')['default']
111112
JsonToCsv: typeof import('./src/tools/json-to-csv/json-to-csv.vue')['default']
112113
JsonToToml: typeof import('./src/tools/json-to-toml/json-to-toml.vue')['default']
113114
JsonToYaml: typeof import('./src/tools/json-to-yaml-converter/json-to-yaml.vue')['default']
@@ -127,24 +128,18 @@ declare module '@vue/runtime-core' {
127128
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
128129
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
129130
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
131+
NCheckbox: typeof import('naive-ui')['NCheckbox']
130132
NCode: typeof import('naive-ui')['NCode']
131133
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
132134
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
133-
NDivider: typeof import('naive-ui')['NDivider']
134135
NEllipsis: typeof import('naive-ui')['NEllipsis']
135-
NFormItem: typeof import('naive-ui')['NFormItem']
136-
NGi: typeof import('naive-ui')['NGi']
137-
NGrid: typeof import('naive-ui')['NGrid']
138136
NH1: typeof import('naive-ui')['NH1']
139137
NH3: typeof import('naive-ui')['NH3']
140138
NIcon: typeof import('naive-ui')['NIcon']
141-
NInputNumber: typeof import('naive-ui')['NInputNumber']
142-
NLabel: typeof import('naive-ui')['NLabel']
143139
NLayout: typeof import('naive-ui')['NLayout']
144140
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
145141
NMenu: typeof import('naive-ui')['NMenu']
146142
NScrollbar: typeof import('naive-ui')['NScrollbar']
147-
NSpin: typeof import('naive-ui')['NSpin']
148143
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
149144
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
150145
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"highlight.js": "^11.7.0",
6565
"iarna-toml-esm": "^3.0.5",
6666
"ibantools": "^4.3.3",
67+
"json2csharp": "^1.0.3",
6768
"json5": "^2.2.3",
6869
"jwt-decode": "^3.1.2",
6970
"libphonenumber-js": "^1.10.28",

pnpm-lock.yaml

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { tool as base64FileConverter } from './base64-file-converter';
22
import { tool as base64StringConverter } from './base64-string-converter';
33
import { tool as basicAuthGenerator } from './basic-auth-generator';
4+
import { tool as jsonToCsharp } from './json-to-csharp';
45

56
import { tool as asciiTextDrawer } from './ascii-text-drawer';
67

@@ -107,6 +108,7 @@ export const toolsByCategory: ToolCategory[] = [
107108
listConverter,
108109
tomlToJson,
109110
tomlToYaml,
111+
jsonToCsharp,
110112
],
111113
},
112114
{

src/tools/json-to-csharp/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CSharp } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'JSON to C#',
6+
path: '/json-to-csharp',
7+
description: 'Convert JSON data to C# type definition',
8+
keywords: ['json', 'c#', 'csharp'],
9+
component: () => import('./json-to-csharp.vue'),
10+
icon: CSharp,
11+
createdAt: new Date('2024-05-11'),
12+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script setup lang="ts">
2+
import JSON5 from 'json5';
3+
import json2csharp from 'json2csharp';
4+
import type { UseValidationRule } from '@/composable/validation';
5+
import TextareaCopyable from '@/components/TextareaCopyable.vue';
6+
import { withDefaultOnError } from '@/utils/defaults';
7+
8+
const defaultValue = `{
9+
a:"n",
10+
arr: [1, 2],
11+
nested: {
12+
a:1,
13+
b:"2"
14+
}
15+
}`;
16+
const jsonInput = ref(defaultValue);
17+
const useNewtonsoft = useStorage('json-to-csharp:newtonsoft', false);
18+
const csharpOutput = computed(() => withDefaultOnError(
19+
() => json2csharp(JSON.stringify(JSON5.parse(jsonInput.value)), useNewtonsoft.value), ''));
20+
const rules: UseValidationRule<string>[] = [
21+
{
22+
validator: (v: string) => v === '' || JSON5.parse(v),
23+
message: 'Provided JSON is not valid.',
24+
},
25+
];
26+
</script>
27+
28+
<template>
29+
<c-card title="JSON to C#">
30+
<c-input-text
31+
v-model:value="jsonInput"
32+
multiline
33+
placeholder="Put your json string here..."
34+
rows="20"
35+
label="JSON to C#"
36+
:validation-rules="rules"
37+
raw-text
38+
mb-5
39+
/>
40+
<n-checkbox v-model:checked="useNewtonsoft">
41+
<span title="Use Newtonsoft Annotations">Use Newtonsoft Annotations</span>
42+
</n-checkbox>
43+
</c-card>
44+
<c-card title="Your C# code">
45+
<TextareaCopyable
46+
:value="csharpOutput"
47+
language="csharp"
48+
/>
49+
</c-card>
50+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'json2csharp'{
2+
export default function(json: string, useNewtonsoftAnnotations?: boolean): string;
3+
}

0 commit comments

Comments
 (0)