diff --git a/src/app/demos/compound-parsers/page.tsx b/src/app/demos/compound-parsers/page.tsx new file mode 100644 index 00000000..4aab4f07 --- /dev/null +++ b/src/app/demos/compound-parsers/page.tsx @@ -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().withDefault({}) + ) + const [array, setArray] = useQueryState( + 'array', + parseAsArrayOf(parseAsJson()).withDefault([]) + ) + return ( + <> +

Compound parsers

+
+

JSON

+ + + + + +
+          {JSON.stringify(code, null, 2)}
+        
+
+
+

Arrays

+ + + + + +
+          {JSON.stringify(array, null, 2)}
+        
+
+ + ) +} diff --git a/src/app/page.tsx b/src/app/page.tsx index 2c4231e2..52b3259d 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -5,7 +5,8 @@ const demos = [ 'subscribeToQueryUpdates', 'batching', 'server-side-parsing', - 'hex-colors' + 'hex-colors', + 'compound-parsers' ] export default function IndexPage() { diff --git a/src/lib/parsers.ts b/src/lib/parsers.ts index c2cc2d0c..f4b7fba9 100644 --- a/src/lib/parsers.ts +++ b/src/lib/parsers.ts @@ -224,7 +224,7 @@ export function parseAsJson(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 { @@ -234,7 +234,7 @@ export function parseAsJson(parser?: (value: unknown) => T) { return null } }, - serialize: value => encodeURIComponent(JSON.stringify(value)) + serialize: value => JSON.stringify(value) }) }