Json type | number | string | true | false | array | object |
---|---|---|---|---|---|---|
Marker | one | 2 | 3 | 3 | four | five |
null -> []
value -> [1, value]
value -> [2, value]
value -> [3, ~~value] // [3, 1] true // [3, 0] false
array -> [4, p1, ..., pN] // p1, ..., pN -
name: value -> [name, valuePresentation] // valuePresentation - value
name: value -> [5, [name1, value1Presentation], ..., [nameN, valueNPresentation]] // value1Presentation, ..., valueNPresentation - value1, ..., valueN
npm install nsnjson-driver
{ "TYPE_MARKER_NUMBER": 1, "TYPE_MARKER_STRING": 2, "TYPE_MARKER_BOOLEAN": 3, "TYPE_MARKER_ARRAY": 4, "TYPE_MARKER_OBJECT": 5 }
{ "NULL": "null", "NUMBER": "number", "STRING": "string", "BOOLEAN": "boolean", "ARRAY": "array", "OBJECT": "object" }
var Maybe = require('data.maybe'); var nsnjson = require('nsnjson-driver'); var Types = require('./nsnjson.types'); var Format = require('./custom.format'); var encodingOptions = {}; // value JSON null encodingOptions[Types.NULL] = function() { return Maybe.Just([]); } // value JSON number encodingOptions[Types.NUMBER] = function(value) { return Maybe.Just([Format.TYPE_MARKER_NUMBER, value]); } // value JSON string encodingOptions[Types.STRING] = function(value) { return Maybe.Just([Format.TYPE_MARKER_STRING, value]); } // value JSON boolean encodingOptions[Types.BOOLEAN] = function(value) { return Maybe.Just([Format.TYPE_MARKER_BOOLEAN, ~~value]); } // array JSON array encodingOptions[Types.ARRAY] = function(array) { var presentation = [Format.TYPE_MARKER_ARRAY]; for (var i = 0, size = array.length; i < size; i++) { var itemPresentationMaybe = this.encode(array[i]); if (itemPresentationMaybe.isJust) { var itemPresentation = itemPresentationMaybe.get(); presentation.push(itemPresentation); } } return Maybe.Just(presentation); } // object JSON object encodingOptions[Types.OBJECT] = function(object) { var presentation = [Format.TYPE_MARKER_OBJECT]; for (var name in object) { if (object.hasOwnProperty(name)) { var valuePresentationMaybe = this.encode(object[name]); if (valuePresentationMaybe.isJust) { var valuePresentation = valuePresentationMaybe.get(); var fieldPresentation = [name, valuePresentation]; presentation.push(fieldPresentation); } } } return Maybe.Just(presentation); }
var Maybe = require('data.maybe'); var nsnjson = require('nsnjson-driver'); var Types = require('./nsnjson.types'); var Format = require('./custom.format') var decodingOptions = { // JSON . 'type': function(presentation) { if (presentation.length == 0) { return Maybe.Just(Types.NULL); } else { switch (presentation[0]) { case Format.TYPE_MARKER_NUMBER: return Maybe.Just(Types.NUMBER); case Format.TYPE_MARKER_STRING: return Maybe.Just(Types.STRING); case Format.TYPE_MARKER_BOOLEAN: return Maybe.Just(Types.BOOLEAN); case Format.TYPE_MARKER_ARRAY: return Maybe.Just(Types.ARRAY); case Format.TYPE_MARKER_OBJECT: return Maybe.Just(Types.OBJECT); } return Maybe.Nothing(); } } }; // JSON null decodingOptions[Types.NULL] = function() { return Maybe.Just(null); } // JSON number decodingOptions[Types.NUMBER] = function(presentation) { return Maybe.Just(presentation[1]); } // JSON string decodingOptions[Types.STRING] = function(presentation) { return Maybe.Just(presentation[1]); } // JSON boolean decodingOptions[Types.BOOLEAN] = function(presentation) { return Maybe.Just(presentation[1] != 0); } // JSON array decodingOptions[Types.ARRAY] = function(presentation) { var array = []; for (var i = 1, size = presentation.length; i < size; i++) { var itemPresentation = presentation[i]; var itemMaybe = this.decode(itemPresentation); if (itemMaybe.isJust) { var item = itemMaybe.get(); array.push(item); } } return Maybe.Just(array); } // JSON object decodingOptions[Types.OBJECT] = function(presentation) { var object = {}; for (var i = 1, size = presentation.length; i < size; i++) { var fieldPresentation = presentation[i]; var name = fieldPresentation[0]; var valueMaybe = this.decode(fieldPresentation[1]); if (valueMaybe.isJust) { var value = valueMaybe.get(); object[name] = value; } } return Maybe.Just(object); } module.exports = { decode: function(presentation) { return nsnjson.decode(presentation, decodingOptions); } };
// encoder JSON var customEncoder = require('./custom.encoder'); // decoder JSON var customDecoder = require('./custom.decoder'); var data = {message: ['I', 'love', 'brackets']}; console.log('Data:', JSON.stringify(data)); // Data: { "message": [ "I", "love", "brackets" ] } var presentationMaybe = customEncoder.encode(data); if (presentationMaybe.isJust) { var presentation = presentationMaybe.get(); console.log('Presentation:', JSON.stringify(presentation)); // Presentation: [ 5, [ "message", [ 4, [ 2, "I" ], [ 2, "love" ], [ 2, "brackets" ] ] ] ] var restoredDataMaybe = customDecoder.decode(presentation); if (restoredDataMaybe.isJust) { var restoredData = restoredDataMaybe.get(); console.log('Restored data:', JSON.stringify(restoredData)); // Restored data: { "message": [ "I", "love", "brackets" ] } } }
// JSON null // NSNJSON [ ]
// JSON 2015 // NSNJSON [ 1, 2015 ]
// JSON true // NSNJSON [ 3, 1 ]
// JSON "Habrahabr.ru" // NSNJSON [ 2, "Habrahabr.ru" ]
// JSON [ "Year", 2015 ] // NSNJSON [ 4, [ 2, "Year" ], [ 1, 2015 ] ]
// JSON { "message": [ "I", "love", "brackets" ] } // NSNJSON [ 5, [ "message", [ 4, [ 2, "I" ], [ 2, "love" ], [ 2, "brackets" ] ] ] ]
Source: https://habr.com/ru/post/269993/
All Articles