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 json #638

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions docs/modules/Codec.ts.md
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ Added in v2.2.3
- [fromSum](#fromsum)
- [fromTuple](#fromtuple)
- [intersect](#intersect)
- [json](#json)
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nullable](#nullable)
@@ -194,6 +195,16 @@ export declare const intersect: <IB, OB, B>(

Added in v2.2.3

## json

**Signature**

```ts
export declare const json: Codec<unknown, string, Json>
```

Added in v2.2.17

## lazy

**Signature**
11 changes: 11 additions & 0 deletions docs/modules/Decoder.ts.md
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ Added in v2.2.7
- [fromSum](#fromsum)
- [fromTuple](#fromtuple)
- [intersect](#intersect)
- [json](#json)
- [lazy](#lazy)
- [mapLeftWithInput](#mapleftwithinput)
- [nullable](#nullable)
@@ -275,6 +276,16 @@ export declare const intersect: <IB, B>(

Added in v2.2.7

## json

**Signature**

```ts
export declare const json: Decoder<unknown, J.Json>
```

Added in v2.2.17

## lazy

**Signature**
11 changes: 11 additions & 0 deletions docs/modules/Encoder.ts.md
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ Added in v2.2.3
- [combinators](#combinators)
- [array](#array)
- [intersect](#intersect)
- [json](#json)
- [lazy](#lazy)
- [nullable](#nullable)
- [partial](#partial)
@@ -108,6 +109,16 @@ export declare const intersect: <P, B>(right: Encoder<P, B>) => <O, A>(left: Enc

Added in v2.2.3

## json

**Signature**

```ts
export declare const json: Encoder<string, Json>
```

Added in v2.2.17

## lazy

**Signature**
11 changes: 8 additions & 3 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -37,9 +37,11 @@
"url": "https://github.com/gcanti/io-ts/issues"
},
"homepage": "https://github.com/gcanti/io-ts",
"dependencies": {},
"dependencies": {
"safe-stable-stringify": "^2.3.1"
},
"peerDependencies": {
"fp-ts": "^2.5.0"
"fp-ts": "^2.10.0"
},
"devDependencies": {
"@types/benchmark": "1.0.31",
@@ -54,7 +56,7 @@
"dtslint": "github:gcanti/dtslint",
"eslint": "^7.18.0",
"fast-check": "^1.24.2",
"fp-ts": "^2.5.0",
"fp-ts": "^2.10.5",
"import-path-rewrite": "github:gcanti/import-path-rewrite",
"jest": "25.2.7",
"mocha": "7.1.1",
7 changes: 7 additions & 0 deletions src/Codec.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
*
* @since 2.2.3
*/
import { Json } from 'fp-ts/lib/Json'
import { identity, Refinement } from 'fp-ts/lib/function'
import { Invariant3 } from 'fp-ts/lib/Invariant'
import { pipe } from 'fp-ts/lib/pipeable'
@@ -142,6 +143,12 @@ export function nullable<I, O, A>(or: Codec<I, O, A>): Codec<null | I, null | O,
return make(D.nullable(or), E.nullable(or))
}

/**
* @category combinators
* @since 2.2.17
*/
export const json: Codec<unknown, string, Json> = make(D.json, E.json)

/**
* @category combinators
* @since 2.2.15
21 changes: 20 additions & 1 deletion src/Decoder.ts
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@ import { Alt2, Alt2C } from 'fp-ts/lib/Alt'
import { Bifunctor2 } from 'fp-ts/lib/Bifunctor'
import { Category2 } from 'fp-ts/lib/Category'
import * as E from 'fp-ts/lib/Either'
import { identity, Refinement } from 'fp-ts/lib/function'
import { flow, identity, Refinement } from 'fp-ts/lib/function'
import * as J from 'fp-ts/lib/Json'
import { Functor2 } from 'fp-ts/lib/Functor'
import { MonadThrow2C } from 'fp-ts/lib/MonadThrow'
import { pipe } from 'fp-ts/lib/pipeable'
@@ -21,6 +22,7 @@ import * as FS from './FreeSemigroup'
import * as G from './Guard'
import * as K from './Kleisli'
import * as S from './Schemable'
import Json = J.Json

// -------------------------------------------------------------------------------------
// Kleisli config
@@ -226,6 +228,23 @@ export const nullable: <I, A>(or: Decoder<I, A>) => Decoder<null | I, null | A>
/*#__PURE__*/
K.nullable(M)((u, e) => FS.concat(FS.of(DE.member(0, error(u, 'null'))), FS.of(DE.member(1, e))))

/**
* @category combinators
* @since 2.2.17
*/
export const json: Decoder<unknown, Json> = {
decode: (i) =>
pipe(
string.decode(i),
E.chain(
flow(
J.parse,
E.altW(() => failure(i, 'JSON'))
)
)
)
}

/**
* @category combinators
* @since 2.2.15
10 changes: 10 additions & 0 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
@@ -10,8 +10,10 @@
*/
import { Contravariant2 } from 'fp-ts/lib/Contravariant'
import { Category2 } from 'fp-ts/lib/Category'
import { Json } from 'fp-ts/lib/Json'
import { memoize, intersect_ } from './Schemable'
import { identity } from 'fp-ts/lib/function'
import safeStringify from 'safe-stable-stringify'

// -------------------------------------------------------------------------------------
// model
@@ -39,6 +41,14 @@ export function nullable<O, A>(or: Encoder<O, A>): Encoder<null | O, null | A> {
}
}

/**
* @category combinators
* @since 2.2.17
*/
export const json: Encoder<string, Json> = {
encode: safeStringify
}

/**
* @category combinators
* @since 2.2.15
27 changes: 27 additions & 0 deletions test/Codec.ts
Original file line number Diff line number Diff line change
@@ -205,6 +205,33 @@ describe('Codec', () => {
})
})

describe('json', () => {
describe('decode', () => {
it('should decode valid JSON', () => {
assert.deepStrictEqual(_.json.decode('null'), D.success(null))
assert.deepStrictEqual(_.json.decode('"a"'), D.success('a'))
assert.deepStrictEqual(_.json.decode('{"a":1}'), D.success({ a: 1 }))
})

it('should reject invalid JSON', () => {
assert.deepStrictEqual(_.json.decode(undefined), D.failure(undefined, 'string'))
assert.deepStrictEqual(_.json.decode('{"a":}'), D.failure('{"a":}', 'JSON'))
})
})

describe('encode', () => {
it('should encode a value', () => {
const circular: any = { ref: null }
circular.ref = circular

assert.deepStrictEqual(_.json.encode(null), 'null')
assert.deepStrictEqual(_.json.encode('a'), '"a"')
assert.deepStrictEqual(_.json.encode({ a: 1 }), '{"a":1}')
assert.deepStrictEqual(_.json.encode(circular), '{"ref":"[Circular]"}')
})
})
})

describe('struct', () => {
describe('decode', () => {
it('should decode a valid input', () => {
13 changes: 13 additions & 0 deletions test/Decoder.ts
Original file line number Diff line number Diff line change
@@ -153,6 +153,19 @@ describe('Decoder', () => {
})
})

describe('json', () => {
it('should decode valid JSON', () => {
assert.deepStrictEqual(_.json.decode('null'), _.success(null))
assert.deepStrictEqual(_.json.decode('"a"'), _.success('a'))
assert.deepStrictEqual(_.json.decode('{"a":1}'), _.success({ a: 1 }))
})

it('should reject invalid JSON', () => {
assert.deepStrictEqual(_.json.decode(undefined), _.failure(undefined, 'string'))
assert.deepStrictEqual(_.json.decode('{"a":}'), _.failure('{"a":}', 'JSON'))
})
})

describe('struct', () => {
it('should decode a valid input', async () => {
const decoder = _.struct({
10 changes: 10 additions & 0 deletions test/Encoder.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,16 @@ describe('Encoder', () => {
assert.deepStrictEqual(encoder.encode(null), null)
})

it('json', () => {
const circular: any = { ref: null }
circular.ref = circular

assert.deepStrictEqual(E.json.encode(null), 'null')
assert.deepStrictEqual(E.json.encode('a'), '"a"')
assert.deepStrictEqual(E.json.encode({ a: 1 }), '{"a":1}')
assert.deepStrictEqual(E.json.encode(circular), '{"ref":"[Circular]"}')
})

it('struct', () => {
const encoder = E.struct({ a: H.encoderNumberToString, b: H.encoderBooleanToNumber })
assert.deepStrictEqual(encoder.encode({ a: 1, b: true }), { a: '1', b: 1 })
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -6,6 +6,6 @@
"variable-name": false,
"ter-indent": false,
"strict-type-predicates": false,
"deprecation": true
"deprecation": false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}