Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Show to Json #1673

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
102 changes: 66 additions & 36 deletions docs/modules/Json.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,20 @@ Added in v2.10.0

<h2 class="text-delta">Table of contents</h2>

- [utils](#utils)
- [constructors](#constructors)
- [parse](#parse)
- [destructors](#destructors)
- [stringify](#stringify)
- [instances](#instances)
- [Show](#show)
- [model](#model)
- [Json (type alias)](#json-type-alias)
- [JsonArray (interface)](#jsonarray-interface)
- [JsonRecord (interface)](#jsonrecord-interface)
- [parse](#parse)
- [stringify](#stringify)

---

# utils

## Json (type alias)

**Signature**

```ts
export type Json = boolean | number | string | null | JsonArray | JsonRecord
```

Added in v2.10.0

## JsonArray (interface)

**Signature**

```ts
export interface JsonArray extends ReadonlyArray<Json> {}
```

Added in v2.10.0

## JsonRecord (interface)

**Signature**

```ts
export interface JsonRecord {
readonly [key: string]: Json
}
```

Added in v2.10.0
# constructors

## parse

Expand All @@ -78,6 +50,8 @@ assert.deepStrictEqual(pipe('{"a":}', J.parse), E.left(new SyntaxError('Unexpect

Added in v2.10.0

# destructors

## stringify

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
Expand Down Expand Up @@ -108,3 +82,59 @@ assert.deepStrictEqual(
```

Added in v2.10.0

# instances

## Show

**Signature**

```ts
export declare const Show: Sh.Show<Json>
```

**Example**

```ts
import * as J from 'fp-ts/Json'

const circular: any = { b: 1, a: 0 }
circular.circular = circular
assert.deepStrictEqual(J.Show.show(circular), '{"a":0,"b":1,"circular":"[Circular]"}')
```

Added in v2.11.9

# model

## Json (type alias)

**Signature**

```ts
export type Json = boolean | number | string | null | JsonArray | JsonRecord
```

Added in v2.10.0

## JsonArray (interface)

**Signature**

```ts
export interface JsonArray extends ReadonlyArray<Json> {}
```

Added in v2.10.0

## JsonRecord (interface)

**Signature**

```ts
export interface JsonRecord {
readonly [key: string]: Json
}
```

Added in v2.10.0
15 changes: 15 additions & 0 deletions dtslint/ts3.5/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import * as E from '../../src/Either'
import { pipe } from '../../src/function'
import * as _ from '../../src/Json'

//
// Show
//

// $ExpectError
_.Show.show(undefined)
// $ExpectError
_.Show.show(() => {})
// $ExpectError
_.Show.show(Symbol())
// $ExpectError
_.Show.show({ a: undefined })
// $ExpectError
_.Show.show({ ...{ a: undefined } })

//
// stringify
//
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,8 @@
"typescript",
"algebraic-data-types",
"functional-programming"
]
],
"dependencies": {
"safe-stable-stringify": "^2.4.0"
}
}
40 changes: 39 additions & 1 deletion src/Json.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
/**
* @since 2.10.0
*/
import safeStringify from 'safe-stable-stringify'
import { Either, tryCatch } from './Either'
import { identity } from './function'
import * as Sh from './Show'

// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------

/**
* @category model
* @since 2.10.0
*/
export type Json = boolean | number | string | null | JsonArray | JsonRecord

/**
* @category model
* @since 2.10.0
*/
export interface JsonRecord {
readonly [key: string]: Json
}

/**
* @category model
* @since 2.10.0
*/
export interface JsonArray extends ReadonlyArray<Json> {}

// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------

/**
* @example
* import * as J from 'fp-ts/Json'
*
* const circular: any = { b: 1, a: 0 }
* circular.circular = circular
* assert.deepStrictEqual(J.Show.show(circular), '{"a":0,"b":1,"circular":"[Circular]"}')
*
* @category instances
* @since 2.11.9
*/
export const Show: Sh.Show<Json> = {
show: safeStringify
}

// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------

/**
* Converts a JavaScript Object Notation (JSON) string into a `Json` type.
*
Expand All @@ -32,10 +64,15 @@ export interface JsonArray extends ReadonlyArray<Json> {}
* assert.deepStrictEqual(pipe('{"a":1}', J.parse), E.right({ a: 1 }))
* assert.deepStrictEqual(pipe('{"a":}', J.parse), E.left(new SyntaxError('Unexpected token } in JSON at position 5')))
*
* @category constructors
* @since 2.10.0
*/
export const parse = (s: string): Either<unknown, Json> => tryCatch(() => JSON.parse(s), identity)

// -------------------------------------------------------------------------------------
// destructors
// -------------------------------------------------------------------------------------

/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
*
Expand All @@ -55,7 +92,8 @@ export const parse = (s: string): Either<unknown, Json> => tryCatch(() => JSON.p
* E.left(true)
* )
*
* @since 2.10.0
* @category destructors
* @since 2.10.0
*/
export const stringify = <A>(a: A): Either<unknown, string> =>
tryCatch(() => {
Expand Down
5 changes: 2 additions & 3 deletions src/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as BA from './BooleanAlgebra'
import * as E from './Eq'
import { Lazy } from './function'
import * as J from './Json'
import { Monoid } from './Monoid'
import * as O from './Ord'
import { Refinement } from './Refinement'
Expand Down Expand Up @@ -186,6 +187,4 @@ export const Ord: O.Ord<boolean> = {
* @category instances
* @since 2.10.0
*/
export const Show: S.Show<boolean> = {
show: (b) => JSON.stringify(b)
}
export const Show: S.Show<boolean> = J.Show
5 changes: 2 additions & 3 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as B from './Bounded'
import * as E from './Eq'
import * as F from './Field'
import * as J from './Json'
import { Magma } from './Magma'
import { Monoid } from './Monoid'
import * as O from './Ord'
Expand Down Expand Up @@ -57,9 +58,7 @@ export const Bounded: B.Bounded<number> = {
* @category instances
* @since 2.10.0
*/
export const Show: S.Show<number> = {
show: (n) => JSON.stringify(n)
}
export const Show: S.Show<number> = J.Show

/**
* @category instances
Expand Down
5 changes: 2 additions & 3 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @since 2.10.0
*/
import * as E from './Eq'
import * as J from './Json'
import * as M from './Monoid'
import * as S from './Semigroup'
import * as O from './Ord'
Expand Down Expand Up @@ -93,9 +94,7 @@ export const Ord: O.Ord<string> = {
* @category instances
* @since 2.10.0
*/
export const Show: Sh.Show<string> = {
show: (s) => JSON.stringify(s)
}
export const Show: Sh.Show<string> = J.Show

// -------------------------------------------------------------------------------------
// refinements
Expand Down
25 changes: 25 additions & 0 deletions test/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,36 @@ import * as _ from '../src/Json'
import * as U from './util'

describe('Json', () => {
// -------------------------------------------------------------------------------------
// instances
// -------------------------------------------------------------------------------------

it('Show', () => {
U.deepStrictEqual(_.Show.show({ a: 1 }), '{"a":1}')
const circular: any = { ref: null }
circular.ref = circular
U.deepStrictEqual(_.Show.show(circular), '{"ref":"[Circular]"}')
type Person = {
readonly name: string
readonly age: number
}
const person: Person = { name: 'Giulio', age: 45 }
U.deepStrictEqual(_.Show.show(person), '{"age":45,"name":"Giulio"}')
})

// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------

it('parse', () => {
U.deepStrictEqual(pipe('{"a":1}', _.parse), E.right({ a: 1 }))
U.deepStrictEqual(pipe('{"a":}', _.parse), E.left(new SyntaxError('Unexpected token } in JSON at position 5')))
})

// -------------------------------------------------------------------------------------
// destructors
// -------------------------------------------------------------------------------------

it('stringify', () => {
U.deepStrictEqual(pipe({ a: 1 }, _.stringify), E.right('{"a":1}'))
const circular: any = { ref: null }
Expand Down