Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit 781c215

Browse files
author
blond
committed
docs(toJSON): add serialization section
1 parent 7ecaf14 commit 781c215

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Contents
2626
* [Install](#install)
2727
* [Usage](#usage)
2828
* [API](#api)
29+
* [Serialization](#serialization)
2930
* [TypeScript support](#typescript-support)
3031
* [Debuggability](#debuggability)
3132
* [Deprecation](#deprecation)
@@ -362,6 +363,31 @@ BemEntityName.isBemEntityName(entityName); // true
362363
BemEntityName.isBemEntityName({ block: 'button' }); // false
363364
```
364365

366+
Serialization
367+
-------------
368+
369+
The `BemEntityName` has `toJSON` method to support `JSON.stringify()` behaviour.
370+
371+
Use `JSON.stringify` to serialize an instance of `BemEntityName`.
372+
373+
```js
374+
const BemEntityName = require('@bem/entity-name');
375+
376+
const name = new BemEntityName({ block: 'input', mod: 'available' });
377+
378+
JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}
379+
```
380+
381+
Use `JSON.parse` to deserialize JSON string and create an instance of `BemEntityName`.
382+
383+
```js
384+
const BemEntityName = require('@bem/entity-name');
385+
386+
const str = '{"block":"input","mod":{"name":"available","val":true}}';
387+
388+
new BemEntityName(JSON.parse(str)); // BemEntityName({ block: 'input', mod: 'available' });
389+
```
390+
365391
TypeScript support
366392
------------------
367393

@@ -398,18 +424,6 @@ console.log(`name: ${name}`);
398424
// ➜ name: input_available
399425
```
400426

401-
Also `BemEntityName` has `toJSON` method to support `JSON.stringify()` behaviour.
402-
403-
```js
404-
const BemEntityName = require('@bem/entity-name');
405-
406-
const name = new BemEntityName({ block: 'input', mod: 'available' });
407-
408-
console.log(JSON.stringify(name));
409-
410-
// ➜ {"block":"input","mod":{"name":"available","val":true}}
411-
```
412-
413427
Deprecation
414428
-----------
415429

0 commit comments

Comments
 (0)