Skip to content

Commit 4cdcbaf

Browse files
committed
Merge branch 'feat/json-to-csharp' into chore/all-my-stuffs
# Conflicts: # components.d.ts # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents efd30f9 + 748126f commit 4cdcbaf

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"js-base64": "^3.7.7",
132132
"json-editor-vue": "^0.16.0",
133133
"json-analyzer": "^1.2.2",
134+
"json2csharp": "^1.0.3",
134135
"json5": "^2.2.3",
135136
"jsonpath": "^1.1.1",
136137
"jsonar-mod": "^1.9.0",

pnpm-lock.yaml

Lines changed: 7 additions & 0 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
@@ -23,6 +23,7 @@ import { tool as jsUnobfuscator } from './js-unobfuscator';
2323
import { tool as jsonToPhpArray } from './json-to-php-array';
2424
import { tool as phpArrayToJson } from './php-array-to-json';
2525
import { tool as jsonSizeAnalyzer } from './json-size-analyzer';
26+
import { tool as jsonToCsharp } from './json-to-csharp';
2627

2728
import { tool as cssXpathConverter } from './css-xpath-converter';
2829
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -181,6 +182,7 @@ export const toolsByCategory: ToolCategory[] = [
181182
markdownToHtml,
182183
currencyConverter,
183184
hddCalculator,
185+
jsonToCsharp,
184186
],
185187
},
186188
{

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)