feat(new tool): JSON to C#

This commit is contained in:
sharevb 2024-05-18 15:26:05 +02:00 committed by ShareVB
parent e876d03608
commit 748126f207
7 changed files with 84 additions and 13 deletions

View file

@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as jsonToCsharp } from './json-to-csharp';
import { tool as asciiTextDrawer } from './ascii-text-drawer';
@ -107,6 +108,7 @@ export const toolsByCategory: ToolCategory[] = [
listConverter,
tomlToJson,
tomlToYaml,
jsonToCsharp,
],
},
{

View file

@ -0,0 +1,12 @@
import { CSharp } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'JSON to C#',
path: '/json-to-csharp',
description: 'Convert JSON data to C# type definition',
keywords: ['json', 'c#', 'csharp'],
component: () => import('./json-to-csharp.vue'),
icon: CSharp,
createdAt: new Date('2024-05-11'),
});

View file

@ -0,0 +1,50 @@
<script setup lang="ts">
import JSON5 from 'json5';
import json2csharp from 'json2csharp';
import type { UseValidationRule } from '@/composable/validation';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { withDefaultOnError } from '@/utils/defaults';
const defaultValue = `{
a:"n",
arr: [1, 2],
nested: {
a:1,
b:"2"
}
}`;
const jsonInput = ref(defaultValue);
const useNewtonsoft = useStorage('json-to-csharp:newtonsoft', false);
const csharpOutput = computed(() => withDefaultOnError(
() => json2csharp(JSON.stringify(JSON5.parse(jsonInput.value)), useNewtonsoft.value), ''));
const rules: UseValidationRule<string>[] = [
{
validator: (v: string) => v === '' || JSON5.parse(v),
message: 'Provided JSON is not valid.',
},
];
</script>
<template>
<c-card title="JSON to C#">
<c-input-text
v-model:value="jsonInput"
multiline
placeholder="Put your json string here..."
rows="20"
label="JSON to C#"
:validation-rules="rules"
raw-text
mb-5
/>
<n-checkbox v-model:checked="useNewtonsoft">
<span title="Use Newtonsoft Annotations">Use Newtonsoft Annotations</span>
</n-checkbox>
</c-card>
<c-card title="Your C# code">
<TextareaCopyable
:value="csharpOutput"
language="csharp"
/>
</c-card>
</template>

View file

@ -0,0 +1,3 @@
declare module 'json2csharp'{
export default function(json: string, useNewtonsoftAnnotations?: boolean): string;
}