Skip to content

Commit

Permalink
fix: No need to encode JSON
Browse files Browse the repository at this point in the history
It's done by URLSearchParams.toString() already.

Add a demo to test it out.

Fixes a double-encoding cosmetic issue found by @tacomanator in
#343 (comment)
  • Loading branch information
franky47 committed Sep 13, 2023
1 parent 6c4a5c6 commit 65ad3c9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/app/demos/compound-parsers/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client'

import { parseAsJson, useQueryState } from '../../../../dist'
import { parseAsArrayOf } from '../../../lib'

const escaped = '-_.!~*\'()?#/&,"`<>{}[]@$£%+=:;'

export default function CompoundParsersDemo() {
const [code, setCode] = useQueryState(
'code',
parseAsJson<any>().withDefault({})
)
const [array, setArray] = useQueryState(
'array',
parseAsArrayOf(parseAsJson<any>()).withDefault([])
)
return (
<>
<h1>Compound parsers</h1>
<section>
<h2>JSON</h2>
<button onClick={() => setCode({})}>Set to {'{}'}</button>
<button onClick={() => setCode([])}>Set to {'[]'}</button>
<button onClick={() => setCode([1, 2, 3])}>Set to {'[1,2,3]'}</button>
<button onClick={() => setCode({ hello: 'world' })}>
Set to {'{hello:"world"}'}
</button>
<button onClick={() => setCode({ escaped })}>
Set to escaped chars
</button>
<pre>
<code>{JSON.stringify(code, null, 2)}</code>
</pre>
</section>
<section>
<h2>Arrays</h2>
<button onClick={() => setArray(null)}>Clear</button>
<button onClick={() => setArray(a => [...a, {}])}>Push {'{}'}</button>
<button onClick={() => setArray(a => [...a, [1, 2, 3]])}>
Push {'[1,2,3]'}
</button>
<button onClick={() => setArray(a => [...a, { hello: 'world' }])}>
Push hello world
</button>
<button onClick={() => setArray(a => [...a, { escaped }])}>
Push escaped chars
</button>
<pre>
<code>{JSON.stringify(array, null, 2)}</code>
</pre>
</section>
</>
)
}
3 changes: 2 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const demos = [
'subscribeToQueryUpdates',
'batching',
'server-side-parsing',
'hex-colors'
'hex-colors',
'compound-parsers'
]

export default function IndexPage() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export function parseAsJson<T>(parser?: (value: unknown) => T) {
return createParser({
parse: query => {
try {
const obj = JSON.parse(decodeURIComponent(query))
const obj = JSON.parse(query)
if (typeof parser === 'function') {
return parser(obj)
} else {
Expand All @@ -234,7 +234,7 @@ export function parseAsJson<T>(parser?: (value: unknown) => T) {
return null
}
},
serialize: value => encodeURIComponent(JSON.stringify(value))
serialize: value => JSON.stringify(value)
})
}

Expand Down

0 comments on commit 65ad3c9

Please sign in to comment.