feat(new tool): JSON Patch Tools

Fix #1776
This commit is contained in:
ShareVB committed 2026-07-10 21:55:11 +02:00
1 parent 00584bd549
commit 6f93cd19c1
4 files changed
+197

No files matched your search

+1
View File
@@ -195,6 +195,7 @@
"exif-be-gone": "^1.5.1",
"exifreader": "^4.33.1",
"fanger": "0.3.1",
"fast-json-patch": "^3.1.1",
"fast-xml-parser": "^5.7.2",
"fast_array_intersect": "^1.1.0",
"fflate": "^0.8.2",
+8
View File
@@ -470,6 +470,9 @@ importers:
fanger:
specifier: 0.3.1
version: 0.3.1
fast-json-patch:
specifier: ^3.1.1
version: 3.1.1
fast-xml-parser:
specifier: ^5.7.2
version: 5.9.3
@@ -6872,6 +6875,9 @@ packages:
resolution: {integrity: sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg==}
engines: {node: '>=8.6.0'}
fast-json-patch@3.1.1:
resolution: {integrity: sha512-vf6IHUX2SBcA+5/+4883dsIjpBTqmfBjmYiWK1savxQmFk4JfBMLa7ynTYOs1Rolp/T1betJxHiGD3g1Mn8lUQ==}
fast-json-stable-stringify@2.1.0:
resolution: {integrity: sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==}
@@ -17498,6 +17504,8 @@ snapshots:
merge2: 1.4.1
micromatch: 4.0.8
fast-json-patch@3.1.1: {}
fast-json-stable-stringify@2.1.0: {}
fast-levenshtein@2.0.6: {}
+13
View File
@@ -0,0 +1,13 @@
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Json Patch Tester',
path: '/json-patch',
description:
'A simple tool for creating and testing JSON patches according to RFC 6902. And list paths of a JSON object.',
keywords: ['json', 'patch', 'rfc6902', 'path'],
component: () => import('./json-patch.vue'),
icon: defineAsyncComponent(() => import('@vicons/tabler/es/FileDiff')),
createdAt: new Date('2026-07-05'),
category: 'JSON',
});
+175
View File
@@ -0,0 +1,175 @@
<script setup lang="ts">
import { useCopy } from '@/composable/copy';
import { applyPatch, compare } from 'fast-json-patch';
const jsonPathsInput = ref('');
const pathRows = ref<{ op: string; path: string; value: string }[]>([]);
const paths = computed(() => pathRows.value.map((r) => r.path).join('\n'));
const { copy: copyPaths } = useCopy({ source: paths, text: 'JSON Paths copied to clipboard!' });
const pathColumns = [
{ title: 'Operation', key: 'op' },
{ title: 'Path', key: 'path' },
{ title: 'Value', key: 'value' },
];
function generatePaths() {
try {
const obj = JSON.parse(jsonPathsInput.value);
const acc: any[] = [];
walk('', obj, acc);
pathRows.value = acc;
} catch {
pathRows.value = [];
}
}
const isLeaf = (v: any) => v === null || typeof v !== 'object';
const preview = (v: any) => (typeof v === 'string' ? v : JSON.stringify(v));
const escapePtr = (s: string) => s.replace(/~/g, '~0').replace(/\//g, '~1');
function walk(base: string, value: any, acc: any[]) {
if (Array.isArray(value)) {
value.forEach((v, i) => {
const p = `${base}/${i}`;
if (isLeaf(v)) {
acc.push({ op: 'replace', path: p, value: preview(v) });
} else {
walk(p, v, acc);
}
});
} else if (value !== null && typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => {
const p = `${base}/${escapePtr(k)}`;
if (isLeaf(v)) {
acc.push({ op: 'replace', path: p, value: preview(v) });
} else {
walk(p, v, acc);
}
});
}
}
/* -------------------------------------------------------
TAB 2: GENERATE PATCH (using fast-json-patch)
------------------------------------------------------- */
const beforeJson = ref('');
const afterJson = ref('');
const generatedPatch = ref('');
function generatePatch() {
try {
const before = JSON.parse(beforeJson.value);
const after = JSON.parse(afterJson.value);
const patch = compare(before, after);
generatedPatch.value = JSON.stringify(patch, null, 2);
} catch {
generatedPatch.value = 'Invalid JSON';
}
}
/* -------------------------------------------------------
TAB 3: APPLY PATCH (using fast-json-patch)
------------------------------------------------------- */
const applyJson = ref('');
const applyPatchInput = ref('');
const applyResult = ref('');
function applyPatchToJson() {
try {
const obj = JSON.parse(applyJson.value);
const patch = JSON.parse(applyPatchInput.value);
const result = applyPatch(obj, patch).newDocument;
applyResult.value = JSON.stringify(result, null, 2);
} catch {
applyResult.value = 'Invalid JSON or patch';
}
}
</script>
<template>
<div>
<n-tabs type="line" animated>
<!-- TAB 1: PATHS -->
<n-tab-pane name="paths" tab="Paths">
<c-input-text
v-model:value="jsonPathsInput"
label="Input JSON"
multiline
rows="10"
placeholder="Paste JSON..."
mb-1
/>
<div mb-2 flex justify-center>
<c-button @click="generatePaths()"> Generate paths </c-button>
</div>
<n-data-table :columns="pathColumns" :data="pathRows" size="small" :bordered="false" mb-1 />
<div flex justify-center>
<c-button @click="copyPaths()"> Copy JSON paths </c-button>
</div>
</n-tab-pane>
<!-- TAB 2: GENERATE PATCH -->
<n-tab-pane name="generate" tab="Generate Patch">
<c-input-text
v-model:value="beforeJson"
label="Before JSON"
multiline
rows="10"
placeholder="Before JSON..."
mb-1
/>
<c-input-text
v-model:value="afterJson"
label="After JSON"
multiline
rows="10"
placeholder="After JSON..."
mb-1
/>
<div mb-2 flex justify-center>
<c-button @click="generatePatch()"> Generate patch </c-button>
</div>
<n-card v-if="generatedPatch" title="Generate JSON Patch">
<CodeBlockCopyable :value="generatedPatch" language="json" />
</n-card>
</n-tab-pane>
<!-- TAB 3: APPLY PATCH -->
<n-tab-pane name="apply" tab="Apply Patch">
<n-space vertical :size="16">
<c-input-text
v-model:value="applyJson"
label="JSON to patch"
multiline
rows="10"
placeholder="JSON to patch..."
mb-1
/>
<c-input-text
v-model:value="applyPatchInput"
label="JSON Patch array"
multiline
rows="10"
placeholder="JSON Patch array..."
mb-1
/>
<div mb-2 flex justify-center>
<c-button @click="applyPatchToJson()"> Apply patch </c-button>
</div>
<n-card v-if="applyResult" title="Patched JSON">
<CodeBlockCopyable :value="applyResult" language="json" />
</n-card>
</n-space>
</n-tab-pane>
</n-tabs>
</div>
</template>