Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit aed02c9

Browse files
committed
feat(json): add JSON serializer
1 parent 5175dd3 commit aed02c9

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
'apiaryb',
1212
'remote',
1313
'form',
14+
'json',
1415
'deps',
1516
'deps-dev',
1617
]],

packages/json-serializer/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# API Elements: JSON Serializer
2+
3+
## Usage
4+
5+
Takes an API Element data structure, and returns JSON serialized data
6+
structures, for example:
7+
8+
```javascript
9+
const { Fury } = require('@apielements/core');
10+
const jsonSerializer = require('@apielements/json-serializer');
11+
12+
const fury = new Fury();
13+
fury.use(jsonSerializer);
14+
15+
const name = new fury.minim.elements.String();
16+
name.attributes.set('default', 'Doe');
17+
18+
const api = new fury.minim.elements.Object({ name });
19+
const mediaType = 'application/json';
20+
fury.serialize({ api, mediaType }, (error, body) => {
21+
console.log(body);
22+
// { "name": "Doe" }
23+
});
24+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const serializeJSON = require('./serializeJSON');
2+
3+
const name = 'json';
4+
const mediaTypes = [
5+
'application/json',
6+
];
7+
8+
function serialize({ api }) {
9+
return new Promise(resolve => resolve(serializeJSON(api)));
10+
}
11+
12+
module.exports = { name, mediaTypes, serialize };
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function collectElementByIDs(element) {
2+
const dataStructures = {};
3+
4+
const { parents } = element;
5+
if (parents) {
6+
const rootElement = parents.get(0);
7+
8+
if (rootElement) {
9+
rootElement.recursiveChildren.forEach((element) => {
10+
if (element.id) {
11+
dataStructures[element.id.toValue()] = element;
12+
}
13+
});
14+
}
15+
}
16+
17+
return dataStructures;
18+
}
19+
20+
function serializeJSON(element) {
21+
const dataStructures = collectElementByIDs(element);
22+
const value = element.valueOf(undefined, dataStructures);
23+
return JSON.stringify(value);
24+
}
25+
26+
module.exports = serializeJSON;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@apielements/json-serializer",
3+
"version": "0.1.0",
4+
"description": "JSON serializer for API Elements",
5+
"author": "Apiary.io <support@apiary.io>",
6+
"license": "MIT",
7+
"main": "./lib/adapter.js",
8+
"files": [
9+
"lib/adapter.js",
10+
"lib/serializeJSON.js"
11+
],
12+
"homepage": "https://github.com/apiaryio/api-elements.js/tree/master/packages/json-serializer",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/apiaryio/api-elements.js.git",
16+
"directory": "packages/json-serializer"
17+
},
18+
"scripts": {
19+
"lint": "eslint .",
20+
"lint:fix": "eslint . --fix",
21+
"test": "mocha test"
22+
},
23+
"peerDependencies": {
24+
"@apielements/core": "^0.1.0"
25+
},
26+
"devDependencies": {
27+
"@apielements/core": "^0.1.0",
28+
"chai": "^4.2.0",
29+
"eslint": "^5.16.0",
30+
"mocha": "^7.1.1"
31+
},
32+
"engines": {
33+
"node": ">=8"
34+
}
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { expect } = require('chai');
2+
const { Fury } = require('@apielements/core');
3+
const adapter = require('../lib/adapter');
4+
5+
describe('JSON Serialiser Adapter', () => {
6+
let fury;
7+
8+
before(() => {
9+
fury = new Fury();
10+
fury.use(adapter);
11+
});
12+
13+
it('has a name', () => {
14+
expect(adapter.name).to.equal('json');
15+
});
16+
17+
it('has a JSON media type', () => {
18+
expect(adapter.mediaTypes).to.deep.equal(['application/json']);
19+
});
20+
21+
it('can serialize an element', (done) => {
22+
const element = new fury.minim.elements.String('hello world');
23+
24+
fury.serialize({ api: element, mediaType: 'application/json' }, (error, result) => {
25+
expect(error).to.be.null;
26+
expect(result).to.equal('"hello world"');
27+
done();
28+
});
29+
});
30+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { expect } = require('chai');
2+
const { Fury } = require('@apielements/core');
3+
const serializeJSON = require('../lib/serializeJSON');
4+
5+
const { minim: namespace } = new Fury();
6+
7+
describe('#serializeJSON', () => {
8+
it('can serialize a primitive element with value', () => {
9+
const element = new namespace.elements.String('hello world');
10+
11+
expect(serializeJSON(element)).to.equal('"hello world"');
12+
});
13+
14+
it('can serialize a primitive element with default value', () => {
15+
const element = new namespace.elements.String();
16+
element.attributes.set('default', 'hello world');
17+
18+
expect(serializeJSON(element)).to.equal('"hello world"');
19+
});
20+
21+
it('can serialize an element with references via parent tree', () => {
22+
const element = new namespace.elements.Element();
23+
element.element = 'message';
24+
25+
new namespace.elements.Category([
26+
new namespace.elements.Category([
27+
new namespace.elements.String('hello world', { id: 'message' }),
28+
], { classes: ['dataStructures'] }),
29+
element,
30+
]).freeze();
31+
32+
expect(serializeJSON(element)).to.equal('"hello world"');
33+
});
34+
});

0 commit comments

Comments
 (0)