-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Vehmloewff/json-overhaul
Json Overhaul
- Loading branch information
Showing
8 changed files
with
554 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { cborDecode, cborEncode } from './cbor.ts' | ||
import { asserts } from './deps.ts' | ||
|
||
Deno.test('cborDecode decodes what cborEncode encoded', () => { | ||
const value = { | ||
foo: { | ||
bar: true, | ||
bin: 'baz', | ||
fatty: 8123, | ||
acid: .3, | ||
}, | ||
data: null, | ||
} | ||
|
||
asserts.assertEquals(cborDecode(cborEncode(value)), value) | ||
|
||
// Can also encode/decode primitives | ||
asserts.assertEquals(cborDecode(cborEncode(true)), true) | ||
asserts.assertEquals(cborDecode(cborEncode('asd')), 'asd') | ||
asserts.assertEquals(cborDecode(cborEncode(123)), 123) | ||
asserts.assertEquals(cborDecode(cborEncode(.123)), .123) | ||
asserts.assertEquals(cborDecode(cborEncode(null)), null) | ||
}) | ||
|
||
Deno.test('cborDecode throws if it can\'t decode', () => { | ||
asserts.assertThrows(() => cborDecode(new Uint8Array([12, 2, 55, 90, 123]))) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { cbor } from './deps.ts' | ||
|
||
export function cborEncode(value: unknown): Uint8Array { | ||
return cbor.encode(value) | ||
} | ||
|
||
export function cborDecode(bytes: Uint8Array): unknown { | ||
return cbor.decode(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { asserts } from './deps.ts' | ||
import { jsonDecode, jsonEncode } from './json.ts' | ||
|
||
Deno.test('jsonDecode decodes what jsonEncode encodes', () => { | ||
const value = { | ||
foo: { | ||
bar: true, | ||
bin: 'baz', | ||
fatty: 8123, | ||
acid: .3, | ||
}, | ||
data: null, | ||
} | ||
|
||
asserts.assertEquals(jsonDecode(jsonEncode(value)), value) | ||
|
||
// Can also encode/decode primitives | ||
asserts.assertEquals(jsonDecode(jsonEncode(true)), true) | ||
asserts.assertEquals(jsonDecode(jsonEncode('asd')), 'asd') | ||
asserts.assertEquals(jsonDecode(jsonEncode(123)), 123) | ||
asserts.assertEquals(jsonDecode(jsonEncode(.123)), .123) | ||
asserts.assertEquals(jsonDecode(jsonEncode(null)), null) | ||
}) | ||
|
||
Deno.test('jsonDecode throws if it can\'t decode', () => { | ||
asserts.assertThrows(() => jsonDecode('duh')) | ||
asserts.assertThrows(() => jsonDecode('')) | ||
asserts.assertThrows(() => jsonDecode('{ new: }')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.