mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(new tool): JSON to Python
This commit is contained in:
1 parent
0e9c33ddd3
commit
bcf93f3824
5 files changed
+229
-2
No files matched your search
@@ -237,6 +237,7 @@
|
||||
"qrcode": "^1.5.4",
|
||||
"qrcode-parser": "^2.1.3",
|
||||
"qrcode-terminal-nooctal": "^0.12.1",
|
||||
"quicktype-core": "^23.1.3",
|
||||
"randexp": "^0.5.3",
|
||||
"regex": "^4.4.0",
|
||||
"riimut": "^0.9.1",
|
||||
|
||||
Generated
+85
-2
@@ -608,6 +608,9 @@ dependencies:
|
||||
qrcode-terminal-nooctal:
|
||||
specifier: ^0.12.1
|
||||
version: 0.12.1
|
||||
quicktype-core:
|
||||
specifier: ^23.1.3
|
||||
version: 23.1.3
|
||||
randexp:
|
||||
specifier: ^0.5.3
|
||||
version: 0.5.3
|
||||
@@ -3539,6 +3542,10 @@ packages:
|
||||
resolution: {integrity: sha512-PzZ1b3PaqBzYqbT4fwKEhiORf38h2OcGp2+JdXNNM7inZ7egaSmfmhyNkQILpqWfS0AYtRS3CDq6z03eZ8yOMQ==}
|
||||
dev: false
|
||||
|
||||
/@glideapps/ts-necessities@2.2.3:
|
||||
resolution: {integrity: sha512-gXi0awOZLHk3TbW55GZLCPP6O+y/b5X1pBXKBVckFONSwF1z1E5ND2BGJsghQFah+pW7pkkyFb2VhUQI2qhL5w==}
|
||||
dev: false
|
||||
|
||||
/@gulpjs/messages@1.1.0:
|
||||
resolution: {integrity: sha512-Ys9sazDatyTgZVb4xPlDufLweJ/Os2uHWOv+Caxvy2O85JcnT4M3vc73bi8pdLWlv3fdWQz3pdI9tVwo8rQQSg==}
|
||||
engines: {node: '>=10.13.0'}
|
||||
@@ -8084,6 +8091,10 @@ packages:
|
||||
resolution: {integrity: sha512-cKV8tMCEpQs4hK/ik71d6LrPOnpkpGBR0wzxqr68g2m/LB2GxVYQroAjMJZRVM1Y4BCjCKc3vAamxSzOY2RP+w==}
|
||||
dev: true
|
||||
|
||||
/browser-or-node@3.0.0:
|
||||
resolution: {integrity: sha512-iczIdVJzGEYhP5DqQxYM9Hh7Ztpqqi+CXZpSmX8ALFs9ecXkQIeqRyM6TfxEfMVpwhl3dSuDvxdzzo9sUOIVBQ==}
|
||||
dev: false
|
||||
|
||||
/browser-resolve@2.0.0:
|
||||
resolution: {integrity: sha512-7sWsQlYL2rGLy2IWm8WL8DCTJvYLc/qlOnsakDac87SOoCd16WLsaAMdCiAqsTNHIe+SXfaqyxyo6THoWqs8WQ==}
|
||||
dependencies:
|
||||
@@ -8627,6 +8638,10 @@ packages:
|
||||
engines: {node: '>=0.8'}
|
||||
dev: false
|
||||
|
||||
/collection-utils@1.0.1:
|
||||
resolution: {integrity: sha512-LA2YTIlR7biSpXkKYwwuzGjwL5rjWEZVOSnvdUc7gObvWe4WkjxOpfrdhoP7Hs09YWDVfg0Mal9BpAqLfVEzQg==}
|
||||
dev: false
|
||||
|
||||
/color-convert@1.9.3:
|
||||
resolution: {integrity: sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==}
|
||||
dependencies:
|
||||
@@ -8965,6 +8980,14 @@ packages:
|
||||
- encoding
|
||||
dev: false
|
||||
|
||||
/cross-fetch@4.1.0:
|
||||
resolution: {integrity: sha512-uKm5PU+MHTootlWEY+mZ4vvXoCn4fLQxT9dSc1sXVMSFkINTJVN8cAQROpwcKm8bJ/c7rgZVIBWzH5T78sNZZw==}
|
||||
dependencies:
|
||||
node-fetch: 2.7.0
|
||||
transitivePeerDependencies:
|
||||
- encoding
|
||||
dev: false
|
||||
|
||||
/cross-spawn@7.0.6:
|
||||
resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==}
|
||||
engines: {node: '>= 8'}
|
||||
@@ -10734,7 +10757,6 @@ packages:
|
||||
/events@3.3.0:
|
||||
resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==}
|
||||
engines: {node: '>=0.8.x'}
|
||||
dev: true
|
||||
|
||||
/evp_bytestokey@1.0.3:
|
||||
resolution: {integrity: sha512-/f2Go4TognH/KvCISP7OUsHn85hT9nUkxxA9BEWxFn+Oj9o8ZNLm/40hdlgSLyuOimsrTKLUMEorQexp/aPQeA==}
|
||||
@@ -14467,6 +14489,10 @@ packages:
|
||||
/package-manager-detector@0.2.8:
|
||||
resolution: {integrity: sha512-ts9KSdroZisdvKMWVAVCXiKqnqNfXz4+IbrBG8/BWx/TR5le+jfenvoBuIZ6UWM9nz47W7AbD9qYfAwfWMIwzA==}
|
||||
|
||||
/pako@0.2.9:
|
||||
resolution: {integrity: sha512-NUcwaKxUxWrZLpDG+z/xZaCgQITkA/Dv4V/T6bw7VON6l1Xz/VnrBqrYjZQ12TamKHzITTfOEIYUj48y2KXImA==}
|
||||
dev: false
|
||||
|
||||
/pako@1.0.11:
|
||||
resolution: {integrity: sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==}
|
||||
|
||||
@@ -14862,7 +14888,6 @@ packages:
|
||||
/pluralize@8.0.0:
|
||||
resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==}
|
||||
engines: {node: '>=4'}
|
||||
dev: true
|
||||
|
||||
/pngjs@3.4.0:
|
||||
resolution: {integrity: sha512-NCrCHhWmnQklfH4MtJMRjZ2a8c80qXeMlQMv2uVp9ISJMTt562SbGd6n2oq0PaPgKm7Z6pL9E2UlLIhC+SHL3w==}
|
||||
@@ -15345,6 +15370,27 @@ packages:
|
||||
engines: {node: '>=10'}
|
||||
dev: false
|
||||
|
||||
/quicktype-core@23.1.3:
|
||||
resolution: {integrity: sha512-0wL9YYx6SvfI3sd7e+mr1Ha514CXnOKdNJgHSiccMqoHfGumrChmVkQhD81HQHztdLQq1k+HMmACzFgfDJJLAA==}
|
||||
dependencies:
|
||||
'@glideapps/ts-necessities': 2.2.3
|
||||
browser-or-node: 3.0.0
|
||||
collection-utils: 1.0.1
|
||||
cross-fetch: 4.1.0
|
||||
is-url: 1.2.4
|
||||
js-base64: 3.7.7
|
||||
lodash: 4.17.21
|
||||
pako: 1.0.11
|
||||
pluralize: 8.0.0
|
||||
readable-stream: 4.5.2
|
||||
unicode-properties: 1.4.1
|
||||
urijs: 1.19.11
|
||||
wordwrap: 1.0.0
|
||||
yaml: 2.7.0
|
||||
transitivePeerDependencies:
|
||||
- encoding
|
||||
dev: false
|
||||
|
||||
/railroad-diagrams@1.0.0:
|
||||
resolution: {integrity: sha512-cz93DjNeLY0idrCNOH6PviZGRN9GJhsdm9hpn1YCS879fj4W+x5IFJhhkRZcwVgMmFF7R82UA/7Oh+R8lLZg6A==}
|
||||
dev: false
|
||||
@@ -15465,6 +15511,17 @@ packages:
|
||||
string_decoder: 1.3.0
|
||||
util-deprecate: 1.0.2
|
||||
|
||||
/readable-stream@4.5.2:
|
||||
resolution: {integrity: sha512-yjavECdqeZ3GLXNgRXgeQEdz9fvDDkNKyHnbHRFtOr7/LcfgBcmct7t/ET+HaCTqfh06OzoAxrkN/IfjJBVe+g==}
|
||||
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
|
||||
dependencies:
|
||||
abort-controller: 3.0.0
|
||||
buffer: 6.0.3
|
||||
events: 3.3.0
|
||||
process: 0.11.10
|
||||
string_decoder: 1.3.0
|
||||
dev: false
|
||||
|
||||
/readable-web-to-node-stream@3.0.2:
|
||||
resolution: {integrity: sha512-ePeK6cc1EcKLEhJFt/AebMCLL+GgSKhuygrZ/GLaKZYEecIgIECf4UaUuaByiGtzckwR4ain9VzUh95T1exYGw==}
|
||||
engines: {node: '>=8'}
|
||||
@@ -16874,6 +16931,10 @@ packages:
|
||||
globrex: 0.1.2
|
||||
dev: true
|
||||
|
||||
/tiny-inflate@1.0.3:
|
||||
resolution: {integrity: sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw==}
|
||||
dev: false
|
||||
|
||||
/tinybench@2.5.0:
|
||||
resolution: {integrity: sha512-kRwSG8Zx4tjF9ZiyH4bhaebu+EDz1BOx9hOigYHlUW4xxI/wKIUQUqo018UlU4ar6ATPBsaMrdbKZ+tmPdohFA==}
|
||||
dev: true
|
||||
@@ -17372,11 +17433,25 @@ packages:
|
||||
engines: {node: '>=4'}
|
||||
dev: true
|
||||
|
||||
/unicode-properties@1.4.1:
|
||||
resolution: {integrity: sha512-CLjCCLQ6UuMxWnbIylkisbRj31qxHPAurvena/0iwSVbQ2G1VY5/HjV0IRabOEbDHlzZlRdCrD4NhB0JtU40Pg==}
|
||||
dependencies:
|
||||
base64-js: 1.5.1
|
||||
unicode-trie: 2.0.0
|
||||
dev: false
|
||||
|
||||
/unicode-property-aliases-ecmascript@2.1.0:
|
||||
resolution: {integrity: sha512-6t3foTQI9qne+OZoVQB/8x8rk2k1eVy1gRXhV3oFQ5T6R1dqQ1xtin3XqSlx3+ATBkliTaR/hHyJBm+LVPNM8w==}
|
||||
engines: {node: '>=4'}
|
||||
dev: true
|
||||
|
||||
/unicode-trie@2.0.0:
|
||||
resolution: {integrity: sha512-x7bc76x0bm4prf1VLg79uhAzKw8DVboClSN5VxJuQ+LKDOVEW9CdH+VY7SP+vX7xCYQqzzgQpFqz15zeLvAtZQ==}
|
||||
dependencies:
|
||||
pako: 0.2.9
|
||||
tiny-inflate: 1.0.3
|
||||
dev: false
|
||||
|
||||
/unimport@3.0.8(rollup@2.79.2):
|
||||
resolution: {integrity: sha512-AOt6xj3QMwqcTZRPB+NhFkyVEjCKnpTVoPm5x6424zz2NYYtCfym2bpJofzPHIJKPNIh5ko2/t2q46ZIMgdmbw==}
|
||||
dependencies:
|
||||
@@ -17660,6 +17735,10 @@ packages:
|
||||
punycode: 2.3.1
|
||||
dev: true
|
||||
|
||||
/urijs@1.19.11:
|
||||
resolution: {integrity: sha512-HXgFDgDommxn5/bIv0cnQZsPhHDA90NPHD6+c/v21U5+Sx5hoP8+dP9IZXBU1gIfvdRfhG8cel9QNPeionfcCQ==}
|
||||
dev: false
|
||||
|
||||
/url-parse@1.5.10:
|
||||
resolution: {integrity: sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==}
|
||||
dependencies:
|
||||
@@ -18540,6 +18619,10 @@ packages:
|
||||
engines: {node: '>=0.4.0'}
|
||||
dev: false
|
||||
|
||||
/wordwrap@1.0.0:
|
||||
resolution: {integrity: sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==}
|
||||
dev: false
|
||||
|
||||
/workbox-background-sync@7.3.0:
|
||||
resolution: {integrity: sha512-PCSk3eK7Mxeuyatb22pcSx9dlgWNv3+M8PqPaYDokks8Y5/FX4soaOqj3yhAZr5k6Q5JWTOMYgaJBpbw11G9Eg==}
|
||||
dependencies:
|
||||
|
||||
@@ -13,6 +13,7 @@ import markdownHljs from 'highlight.js/lib/languages/markdown';
|
||||
import jsHljs from 'highlight.js/lib/languages/javascript';
|
||||
import cssHljs from 'highlight.js/lib/languages/css';
|
||||
import goHljs from 'highlight.js/lib/languages/go';
|
||||
import pythonHljs from 'highlight.js/lib/languages/python';
|
||||
import csharpHljs from 'highlight.js/lib/languages/csharp';
|
||||
import { Base64 } from 'js-base64';
|
||||
import { useCopy } from '@/composable/copy';
|
||||
@@ -51,6 +52,7 @@ hljs.registerLanguage('css', cssHljs);
|
||||
hljs.registerLanguage('javascript', jsHljs);
|
||||
hljs.registerLanguage('go', goHljs);
|
||||
hljs.registerLanguage('csharp', csharpHljs);
|
||||
hljs.registerLanguage('python', pythonHljs);
|
||||
|
||||
const { value, language, followHeightOf, copyPlacement, copyMessage, downloadFileName, downloadButtonText } = toRefs(props);
|
||||
const { height } = followHeightOf.value ? useElementSize(followHeightOf) : { height: ref(null) };
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { BrandPython } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'Json to Python',
|
||||
path: '/json-to-python',
|
||||
description: 'Convert JSON to Python classes',
|
||||
keywords: ['json', 'to', 'python'],
|
||||
component: () => import('./json-to-python.vue'),
|
||||
icon: BrandPython,
|
||||
createdAt: new Date('2025-03-09'),
|
||||
});
|
||||
@@ -0,0 +1,129 @@
|
||||
<script setup lang="ts">
|
||||
import JSON5 from 'json5';
|
||||
import { InputData, jsonInputForTargetLanguage, quicktype } from 'quicktype-core';
|
||||
import type { UseValidationRule } from '@/composable/validation';
|
||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const defaultValue = `{
|
||||
a:"n",
|
||||
arr: [1, 2],
|
||||
nested: {
|
||||
a:1,
|
||||
b:"2"
|
||||
}
|
||||
}`;
|
||||
const jsonInput = ref(defaultValue);
|
||||
const rootName = useQueryParamOrStorage({ name: 'root', storageName: 'json-python:r', defaultValue: 'GeneratedClass' });
|
||||
const pythonVersion = useQueryParamOrStorage({ name: 'version', storageName: 'json-python:pv', defaultValue: '3.6' });
|
||||
const classesOnly = useQueryParamOrStorage({ name: 'classes', storageName: 'json-python:cl', defaultValue: false });
|
||||
const pythonicNames = useQueryParamOrStorage({ name: 'pythonic', storageName: 'json-python:pn', defaultValue: true });
|
||||
const pydanticBaseModel = useQueryParamOrStorage({ name: 'basemodel', storageName: 'json-python:bm', defaultValue: false });
|
||||
const optionalProperties = useQueryParamOrStorage({ name: 'optional', storageName: 'json-python:op', defaultValue: false });
|
||||
|
||||
async function convertJsonToPython(json: string,
|
||||
{
|
||||
pythonVersion = '3.9',
|
||||
classesOnly = true,
|
||||
pythonicNames = true,
|
||||
optionalProperties = true,
|
||||
pydanticBaseModel = false,
|
||||
rootName = 'GeneratedClass',
|
||||
}: {
|
||||
pythonVersion?: string
|
||||
classesOnly?: boolean
|
||||
pythonicNames?: boolean
|
||||
optionalProperties?: boolean
|
||||
rootName?: string
|
||||
pydanticBaseModel?: boolean
|
||||
} = {},
|
||||
) {
|
||||
const jsonInput = jsonInputForTargetLanguage('python');
|
||||
await jsonInput.addSource({ name: rootName, samples: [JSON.stringify(JSON5.parse(json))] });
|
||||
|
||||
const inputData = new InputData();
|
||||
inputData.addInput(jsonInput);
|
||||
|
||||
const result = await quicktype({
|
||||
inputData,
|
||||
lang: 'python',
|
||||
allPropertiesOptional: optionalProperties,
|
||||
topLevel: rootName,
|
||||
rendererOptions: {
|
||||
'python-version': pythonVersion,
|
||||
'just-types': classesOnly,
|
||||
'nice-property-names': pythonicNames,
|
||||
'pydantic-base-model': pydanticBaseModel,
|
||||
},
|
||||
} as any);
|
||||
|
||||
return result.lines.join('\n');
|
||||
}
|
||||
const pythonOutput = computedAsync(async () => {
|
||||
try {
|
||||
return await convertJsonToPython(jsonInput.value,
|
||||
{
|
||||
rootName: rootName.value,
|
||||
pythonVersion: pythonVersion.value,
|
||||
classesOnly: classesOnly.value,
|
||||
pythonicNames: pythonicNames.value,
|
||||
optionalProperties: optionalProperties.value,
|
||||
pydanticBaseModel: pydanticBaseModel.value,
|
||||
},
|
||||
);
|
||||
}
|
||||
catch (e: any) {
|
||||
return e.toString();
|
||||
}
|
||||
});
|
||||
|
||||
const rules: UseValidationRule<string>[] = [
|
||||
{
|
||||
validator: (v: string) => v === '' || JSON5.parse(v),
|
||||
message: 'Provided JSON is not valid.',
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<c-card title="JSON to Python">
|
||||
<c-input-text
|
||||
v-model:value="jsonInput"
|
||||
multiline
|
||||
placeholder="Put your json string here..."
|
||||
rows="20"
|
||||
label="JSON Object:"
|
||||
:validation-rules="rules"
|
||||
raw-text
|
||||
mb-5
|
||||
/>
|
||||
<c-input-text v-model:value="rootName" label="Root Name:" label-position="left" mb-2 />
|
||||
<n-space justify="center" items-center>
|
||||
<c-select
|
||||
v-model:value="pythonVersion"
|
||||
:options="['3.5', '3.6', '3.7']"
|
||||
label-position="left"
|
||||
label="Python version:"
|
||||
placeholder="Python version"
|
||||
/>
|
||||
<n-checkbox v-model:checked="classesOnly">
|
||||
Classes only
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="pythonicNames">
|
||||
Transform property names to be Pythonic
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="pydanticBaseModel">
|
||||
Uses pydantic BaseModel
|
||||
</n-checkbox>
|
||||
<n-checkbox v-model:checked="optionalProperties">
|
||||
Make all properties optional
|
||||
</n-checkbox>
|
||||
</n-space>
|
||||
</c-card>
|
||||
<c-card title="Your Python code">
|
||||
<TextareaCopyable
|
||||
:value="pythonOutput"
|
||||
language="python"
|
||||
/>
|
||||
</c-card>
|
||||
</template>
|
||||
Reference in new issue
Block a user