Skip to content

Commit

Permalink
add JsonFromString, closes #153
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 9, 2021
1 parent 478954c commit cfe8c9c
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 0.5.14

- **New Feature**
- add `JsonFromString`, closes #153 (@gcanti)

# 0.5.13

- **New Feature**
Expand Down
64 changes: 64 additions & 0 deletions docs/modules/JsonFromString.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: JsonFromString.ts
nav_order: 16
parent: Modules
---

# JsonFromString overview

Added in v0.5.14

---

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

- [JsonArray (interface)](#jsonarray-interface)
- [JsonRecord (interface)](#jsonrecord-interface)
- [Json (type alias)](#json-type-alias)
- [JSONFromString](#jsonfromstring)

---

# JsonArray (interface)

**Signature**

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

Added in v0.5.14

# JsonRecord (interface)

**Signature**

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

Added in v0.5.14

# Json (type alias)

Copied from `fp-ts/Either` module.

**Signature**

```ts
export type Json = boolean | number | string | null | JsonArray | JsonRecord
```
Added in v0.5.14
# JSONFromString
**Signature**
```ts
export const JSONFromString: t.Type<Json, string, string> = ...
```

Added in v0.5.14
2 changes: 1 addition & 1 deletion docs/modules/NonEmptyString.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: NonEmptyString.ts
nav_order: 18
nav_order: 19
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/NumberFromString.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: NumberFromString.ts
nav_order: 19
nav_order: 20
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/UUID.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: UUID.ts
nav_order: 26
nav_order: 27
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/mapOutput.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: mapOutput.ts
nav_order: 16
nav_order: 17
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/nonEmptyArray.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: nonEmptyArray.ts
nav_order: 17
nav_order: 18
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/option.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: option.ts
nav_order: 20
nav_order: 21
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/optionFromNullable.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: optionFromNullable.ts
nav_order: 21
nav_order: 22
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/readonlyNonEmptyArray.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: readonlyNonEmptyArray.ts
nav_order: 22
nav_order: 23
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/readonlySetFromArray.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: readonlySetFromArray.ts
nav_order: 23
nav_order: 24
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/regexp.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: regexp.ts
nav_order: 24
nav_order: 25
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/setFromArray.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: setFromArray.ts
nav_order: 25
nav_order: 26
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/withEncode.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: withEncode.ts
nav_order: 27
nav_order: 28
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/withFallback.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: withFallback.ts
nav_order: 28
nav_order: 29
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/withMessage.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: withMessage.ts
nav_order: 29
nav_order: 30
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/withValidate.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: withValidate.ts
nav_order: 30
nav_order: 31
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts-types",
"version": "0.5.13",
"version": "0.5.14",
"description": "A collection of codecs and combinators for use with io-ts",
"main": "lib/index.js",
"module": "es6/index.js",
Expand Down
43 changes: 43 additions & 0 deletions src/JsonFromString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @since 0.5.14
*/
import * as t from 'io-ts'

/**
* Copied from `fp-ts/Either` module.
*
* @since 0.5.14
*/
export type Json = boolean | number | string | null | JsonArray | JsonRecord

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

/**
* @since 0.5.14
*/
export interface JsonArray extends ReadonlyArray<Json> {}

const JsonArray: t.Type<JsonArray> = t.recursion('JsonArray', () => t.readonlyArray(Json))
const JsonRecord: t.Type<JsonRecord> = t.recursion('JsonRecord', () => t.record(t.string, Json))
const Json: t.Type<Json> = t.union([t.boolean, t.number, t.string, t.null, JsonArray, JsonRecord], 'Json')

/**
* @since 0.5.14
*/
export const JSONFromString = new t.Type<Json, string, string>(
'JSONFromString',
Json.is,
(s, c) => {
try {
return t.success(JSON.parse(s))
} catch (e) {
return t.failure(s, c)
}
},
json => JSON.stringify(json)
)
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ export * from './setFromArray'
*/
export * from './IntFromString'

/**
* @since 0.5.14
*/
export * from './JsonFromString'

/**
* @since 0.5.8
*/
Expand Down
36 changes: 36 additions & 0 deletions test/JsonFromString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as assert from 'assert'
import { JSONFromString } from '../src'
import { assertFailure, assertSuccess } from './helpers'

describe('JSONFromString', () => {
it('is', () => {
assert.deepStrictEqual(JSONFromString.is(null), true)
assert.deepStrictEqual(JSONFromString.is('a'), true)
assert.deepStrictEqual(JSONFromString.is(1), true)
assert.deepStrictEqual(JSONFromString.is(true), true)
assert.deepStrictEqual(JSONFromString.is(false), true)
assert.deepStrictEqual(JSONFromString.is([]), true)
assert.deepStrictEqual(JSONFromString.is([1]), true)
assert.deepStrictEqual(JSONFromString.is({}), true)
assert.deepStrictEqual(JSONFromString.is({ a: 1 }), true)
assert.deepStrictEqual(JSONFromString.is({ a: Date }), false)
})

it('decode', () => {
const T = JSONFromString
assertSuccess(T.decode('null'), null)
assertSuccess(T.decode('1'), 1)
assertSuccess(T.decode('"a"'), 'a')
assertSuccess(T.decode('true'), true)
assertSuccess(T.decode('false'), false)
assertSuccess(T.decode('[]'), [])
assertSuccess(T.decode('{}'), {})
assertFailure(T, '{', ['Invalid value "{" supplied to : JSONFromString'])
assertFailure(T, '{"a":undefined}', ['Invalid value "{\\"a\\":undefined}" supplied to : JSONFromString'])
})

it('encode', () => {
const T = JSONFromString
assert.deepEqual(T.encode({}), '{}')
})
})

0 comments on commit cfe8c9c

Please sign in to comment.