Skip to content

Commit

Permalink
init 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflake107 committed Feb 4, 2021
0 parents commit b3c5de1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Deno JSON5
**[JSON5](https://npmjs.com/package/json5)** for **[Deno](https://deno.land)**.

# Example

`main.ts`
```js
import JSON5 from "https://deno.land/x/json5";

const data = `{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',
],
"backwardsCompatible": "with JSON",
}`;
console.log(JSON5.parse(data));

/*
{
unquoted: "and you can quote me on that",
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! No \\n's!",
hexadecimal: 912559,
leadingDecimalPoint: 0.8675309,
andTrailing: 8675309,
positiveSign: 1,
trailingComma: "in objects",
andIn: [ "arrays" ],
backwardsCompatible: "with JSON"
}
*/
```

# Methods
- `JSON5.stringify`
- `JSON5.parse`
- `JSON5.require`
- `JSON5.requireAsync`
51 changes: 51 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import JSON5_MOD from "https://cdn.skypack.dev/json5";

/**
* Converts a JSON5 string into an object.
* @template T Type of return value.
* @param text A valid JSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
export function parse<T = any>(text: string, reviver?: ((this: any, key: string, value: any) => any | null)): T {
return JSON5_MOD.parse(text, reviver);
}

/**
* Converts a JavaScript value to a JSON5 string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
export function stringify(value: any, replacer?: ((this: any, key: string, value: any) => any) | null, space?: string | number | undefined): string {
// @ts-ignore
return JSON5_MOD.stringify(value, replacer, parseInt(space) || undefined);
}

/**
* Loads JSON5 from file synchronously
* @param path File path or url
*/
export function require(path: string | URL): any {
const data = Deno.readFileSync(path);
const decoder = new TextDecoder("utf8");
const raw = decoder.decode(data);

return JSON5_MOD.parse(raw, null);
}

/**
* Loads JSON5 from file asynchronously
* @param path File path or url
*/
export async function requireAsync(path: string | URL): Promise<any> {
const data = await Deno.readFileSync(path);
const decoder = new TextDecoder("utf8");
const raw = decoder.decode(data);

return JSON5_MOD.parse(raw, null);
}

// defaults
const JSON5 = { parse, stringify, require, requireAsync };

export default JSON5;

0 comments on commit b3c5de1

Please sign in to comment.