Skip to content

Commit

Permalink
✨ Add json and cbor conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jun 28, 2024
1 parent eaa08de commit f8b1299
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 4 deletions.
77 changes: 75 additions & 2 deletions app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
<template>
<div>
<NuxtRouteAnnouncer />
<NuxtWelcome />
<h1>CBOR to JSON Online Converter</h1>
<p>Convert CBOR from/to JSON string</p>
<section style="display: flex; flex-wrap: wrap;">
<div style="flex: 1">
<div style="display: flex; gap: 25%;">
<label for="cbor-value">CBOR</label>
<div>
<label for="cbor-encoding">Encoding</label>
<select id="cbor-encoding" v-model="cborEncoding">
<option value="base64">base64</option>
<option value="hex">hex</option>
</select>
</div>
</div>
<textarea id="cbor-value" @input="cborToJson" v-model="cborValue" />
</div>
<div style="flex: 1">
<label for="json-value">JSON</label>
<br />
<textarea id="json-value" @input="jsonToCbor" v-model="jsonValue" />
</div>
</section>
</div>
</template>

<script setup lang="ts">
import { decode, encode } from 'cbor-x';
import { Buffer } from 'node:buffer'
const cborValue = ref('')
const jsonValue = ref(JSON.stringify({
hello: 'world',
array: [1, 2, 3],
nested: {
key: 'value'
}
}))
const cborEncoding = ref('base64' as BufferEncoding)
onMounted(() => {
jsonToCbor()
})
function stringToBuffer(input: string) {
if (cborEncoding.value === 'base64') {
return Buffer.from(input, 'base64')
} else {
return Buffer.from(input, 'hex')
}
}
function cborToJson() {
try {
const cbor = decode(stringToBuffer(cborValue.value))
jsonValue.value = JSON.stringify(cbor, null, 2)
} catch (e) {
jsonValue.value = (e as Error).message
}
}
function jsonToCbor() {
try {
const cbor = encode(JSON.parse(jsonValue.value))
cborValue.value = Buffer.from(cbor).toString(cborEncoding.value)
} catch (e) {
cborValue.value = (e as Error).message
}
}
watch(cborEncoding, () => jsonToCbor())
</script>

<style scoped>
textarea {
width: 100%;
min-height: 300px;
}
</style>
5 changes: 4 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true }
devtools: { enabled: true },
experimental: {
clientNodeCompat: true,
},
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"cbor-x": "^1.5.9",
"nuxt": "^3.12.2",
"vue": "latest"
}
Expand Down
60 changes: 59 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,36 @@
"@babel/helper-validator-identifier" "^7.24.7"
to-fast-properties "^2.0.0"

"@cbor-extract/[email protected]":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@cbor-extract/cbor-extract-darwin-arm64/-/cbor-extract-darwin-arm64-2.2.0.tgz#8d65cb861a99622e1b4a268e2d522d2ec6137338"
integrity sha512-P7swiOAdF7aSi0H+tHtHtr6zrpF3aAq/W9FXx5HektRvLTM2O89xCyXF3pk7pLc7QpaY7AoaE8UowVf9QBdh3w==

"@cbor-extract/[email protected]":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@cbor-extract/cbor-extract-darwin-x64/-/cbor-extract-darwin-x64-2.2.0.tgz#9fbec199c888c5ec485a1839f4fad0485ab6c40a"
integrity sha512-1liF6fgowph0JxBbYnAS7ZlqNYLf000Qnj4KjqPNW4GViKrEql2MgZnAsExhY9LSy8dnvA4C0qHEBgPrll0z0w==

"@cbor-extract/[email protected]":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@cbor-extract/cbor-extract-linux-arm64/-/cbor-extract-linux-arm64-2.2.0.tgz#bf77e0db4a1d2200a5aa072e02210d5043e953ae"
integrity sha512-rQvhNmDuhjTVXSPFLolmQ47/ydGOFXtbR7+wgkSY0bdOxCFept1hvg59uiLPT2fVDuJFuEy16EImo5tE2x3RsQ==

"@cbor-extract/[email protected]":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@cbor-extract/cbor-extract-linux-arm/-/cbor-extract-linux-arm-2.2.0.tgz#491335037eb8533ed8e21b139c59f6df04e39709"
integrity sha512-QeBcBXk964zOytiedMPQNZr7sg0TNavZeuUCD6ON4vEOU/25+pLhNN6EDIKJ9VLTKaZ7K7EaAriyYQ1NQ05s/Q==

"@cbor-extract/[email protected]":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@cbor-extract/cbor-extract-linux-x64/-/cbor-extract-linux-x64-2.2.0.tgz#672574485ccd24759bf8fb8eab9dbca517d35b97"
integrity sha512-cWLAWtT3kNLHSvP4RKDzSTX9o0wvQEEAj4SKvhWuOVZxiDAeQazr9A+PSiRILK1VYMLeDml89ohxCnUNQNQNCw==

"@cbor-extract/[email protected]":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@cbor-extract/cbor-extract-win32-x64/-/cbor-extract-win32-x64-2.2.0.tgz#4b3f07af047f984c082de34b116e765cb9af975f"
integrity sha512-l2M+Z8DO2vbvADOBNLbbh9y5ST1RY5sqkWOg/58GkUPBYou/cuNZ68SGQ644f1CvZ8kcOxyZtw06+dxWHIoN/w==

"@cloudflare/kv-asset-handler@^0.3.4":
version "0.3.4"
resolved "https://registry.yarnpkg.com/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.3.4.tgz#5cc152847c8ae4d280ec5d7f4f6ba8c976b585c3"
Expand Down Expand Up @@ -2069,6 +2099,27 @@ caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001599, caniuse-lite@^1.0.30001629:
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001638.tgz#598e1f0c2ac36f37ebc3f5b8887a32ca558e5d56"
integrity sha512-5SuJUJ7cZnhPpeLHaH0c/HPAnAHZvS6ElWyHK9GSIbVOQABLzowiI2pjmpvZ1WEbkyz46iFd4UXlOHR5SqgfMQ==

cbor-extract@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/cbor-extract/-/cbor-extract-2.2.0.tgz#cee78e630cbeae3918d1e2e58e0cebaf3a3be840"
integrity sha512-Ig1zM66BjLfTXpNgKpvBePq271BPOvu8MR0Jl080yG7Jsl+wAZunfrwiwA+9ruzm/WEdIV5QF/bjDZTqyAIVHA==
dependencies:
node-gyp-build-optional-packages "5.1.1"
optionalDependencies:
"@cbor-extract/cbor-extract-darwin-arm64" "2.2.0"
"@cbor-extract/cbor-extract-darwin-x64" "2.2.0"
"@cbor-extract/cbor-extract-linux-arm" "2.2.0"
"@cbor-extract/cbor-extract-linux-arm64" "2.2.0"
"@cbor-extract/cbor-extract-linux-x64" "2.2.0"
"@cbor-extract/cbor-extract-win32-x64" "2.2.0"

cbor-x@^1.5.9:
version "1.5.9"
resolved "https://registry.yarnpkg.com/cbor-x/-/cbor-x-1.5.9.tgz#ed6b2afcd7884bdd697674bfb7332c1473a13ecf"
integrity sha512-OEI5rEu3MeR0WWNUXuIGkxmbXVhABP+VtgAXzm48c9ulkrsvxshjjk94XSOGphyAKeNGLPfAxxzEtgQ6rEVpYQ==
optionalDependencies:
cbor-extract "^2.2.0"

chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
Expand Down Expand Up @@ -2500,7 +2551,7 @@ detect-libc@^1.0.3:
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==

detect-libc@^2.0.0:
detect-libc@^2.0.0, detect-libc@^2.0.1:
version "2.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.3.tgz#f0cd503b40f9939b894697d19ad50895e30cf700"
integrity sha512-bwy0MGW55bG41VqxxypOsdSdGqLwXPI/focwgTYCFMbdUiBAxLg9CFzG08sz2aqzknwiX7Hkl0bQENjg8iLByw==
Expand Down Expand Up @@ -3905,6 +3956,13 @@ node-forge@^1.3.1:
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.1.tgz#be8da2af243b2417d5f646a770663a92b7e9ded3"
integrity sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==

[email protected]:
version "5.1.1"
resolved "https://registry.yarnpkg.com/node-gyp-build-optional-packages/-/node-gyp-build-optional-packages-5.1.1.tgz#52b143b9dd77b7669073cbfe39e3f4118bfc603c"
integrity sha512-+P72GAjVAbTxjjwUmwjVrqrdZROD4nf8KgpBoDxqXXTiYZZt/ud60dE5yvCSr9lRO8e8yv6kgJIC0K0PfZFVQw==
dependencies:
detect-libc "^2.0.1"

node-gyp-build@^4.2.2:
version "4.8.1"
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.8.1.tgz#976d3ad905e71b76086f4f0b0d3637fe79b6cda5"
Expand Down

0 comments on commit f8b1299

Please sign in to comment.