Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/modules/Json.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Converts a JavaScript Object Notation (JSON) string into a `Json` type.
**Signature**

```ts
export declare const parse: (s: string) => Either<unknown, Json>
export declare const parse: (s: string) => Either<SyntaxError, Json>
```

**Example**
Expand All @@ -85,7 +85,7 @@ Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
**Signature**

```ts
export declare const stringify: <A>(a: A) => Either<unknown, string>
export declare const stringify: <A>(a: A) => Either<TypeError, string>
```

**Example**
Expand All @@ -101,7 +101,7 @@ circular.ref = circular
assert.deepStrictEqual(
pipe(
J.stringify(circular),
E.mapLeft((e) => e instanceof Error && e.message.includes('Converting circular structure to JSON'))
E.mapLeft((e) => e.message.includes('Converting circular structure to JSON'))
),
E.left(true)
)
Expand Down
11 changes: 10 additions & 1 deletion dtslint/ts3.5/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import * as E from '../../src/Either'
import { pipe } from '../../src/function'
import * as _ from '../../src/Json'

declare const s: string

//
// parse
//

// $ExpectType Either<SyntaxError, Json>
_.parse(s)

//
// stringify
//
Expand Down Expand Up @@ -34,5 +43,5 @@ _.stringify([{ a: 'a', b: 1 }])
_.stringify(abs)
_.stringify([...abs])

// $ExpectType Either<unknown, string>
// $ExpectType Either<TypeError, string>
pipe(E.right('a'), E.chainFirst(_.stringify))
10 changes: 5 additions & 5 deletions src/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface JsonArray extends ReadonlyArray<Json> {}
*
* @since 2.10.0
*/
export const parse = (s: string): Either<unknown, Json> => tryCatch(() => JSON.parse(s), identity)
export const parse = (s: string): Either<SyntaxError, Json> => tryCatch(() => JSON.parse(s), identity as any)

/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
Expand All @@ -50,18 +50,18 @@ export const parse = (s: string): Either<unknown, Json> => tryCatch(() => JSON.p
* assert.deepStrictEqual(
* pipe(
* J.stringify(circular),
* E.mapLeft(e => e instanceof Error && e.message.includes('Converting circular structure to JSON'))
* E.mapLeft(e => e.message.includes('Converting circular structure to JSON'))
* ),
* E.left(true)
* )
*
* @since 2.10.0
*/
export const stringify = <A>(a: A): Either<unknown, string> =>
export const stringify = <A>(a: A): Either<TypeError, string> =>
tryCatch(() => {
const s = JSON.stringify(a)
if (typeof s !== 'string') {
throw new Error('Converting unsupported structure to JSON')
throw new TypeError('Converting unsupported structure to JSON')
}
return s
}, identity)
}, identity as any)
5 changes: 3 additions & 2 deletions test/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ describe('Json', () => {
pipe(
circular,
_.stringify,
E.mapLeft((e) => (e as Error).message.includes('Converting circular structure to JSON'))
E.mapLeft((e) => e.message.includes('Converting circular structure to JSON'))
),
E.left(true)
)
U.deepStrictEqual(_.stringify({ a: BigInt(1) }), E.left(new TypeError('Do not know how to serialize a BigInt')))
type Person = {
readonly name: string
readonly age: number
}
const person: Person = { name: 'Giulio', age: 45 }
U.deepStrictEqual(pipe(person, _.stringify), E.right('{"name":"Giulio","age":45}'))

U.deepStrictEqual(_.stringify(undefined as any), E.left(new Error('Converting unsupported structure to JSON')))
U.deepStrictEqual(_.stringify(undefined), E.left(new TypeError('Converting unsupported structure to JSON')))
})
})