|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.5 KB
|
| Line | |
|---|
| 1 | //TODO: handle reviver/dehydrate function like normal
|
|---|
| 2 | //and handle indentation, like normal.
|
|---|
| 3 | //if anyone needs this... please send pull request.
|
|---|
| 4 |
|
|---|
| 5 | exports.stringify = function stringify (o) {
|
|---|
| 6 | if('undefined' == typeof o) return o
|
|---|
| 7 |
|
|---|
| 8 | if(o && Buffer.isBuffer(o))
|
|---|
| 9 | return JSON.stringify(':base64:' + o.toString('base64'))
|
|---|
| 10 |
|
|---|
| 11 | if(o && o.toJSON)
|
|---|
| 12 | o = o.toJSON()
|
|---|
| 13 |
|
|---|
| 14 | if(o && 'object' === typeof o) {
|
|---|
| 15 | var s = ''
|
|---|
| 16 | var array = Array.isArray(o)
|
|---|
| 17 | s = array ? '[' : '{'
|
|---|
| 18 | var first = true
|
|---|
| 19 |
|
|---|
| 20 | for(var k in o) {
|
|---|
| 21 | var ignore = 'function' == typeof o[k] || (!array && 'undefined' === typeof o[k])
|
|---|
| 22 | if(Object.hasOwnProperty.call(o, k) && !ignore) {
|
|---|
| 23 | if(!first)
|
|---|
| 24 | s += ','
|
|---|
| 25 | first = false
|
|---|
| 26 | if (array) {
|
|---|
| 27 | if(o[k] == undefined)
|
|---|
| 28 | s += 'null'
|
|---|
| 29 | else
|
|---|
| 30 | s += stringify(o[k])
|
|---|
| 31 | } else if (o[k] !== void(0)) {
|
|---|
| 32 | s += stringify(k) + ':' + stringify(o[k])
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | s += array ? ']' : '}'
|
|---|
| 38 |
|
|---|
| 39 | return s
|
|---|
| 40 | } else if ('string' === typeof o) {
|
|---|
| 41 | return JSON.stringify(/^:/.test(o) ? ':' + o : o)
|
|---|
| 42 | } else if ('undefined' === typeof o) {
|
|---|
| 43 | return 'null';
|
|---|
| 44 | } else
|
|---|
| 45 | return JSON.stringify(o)
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | exports.parse = function (s) {
|
|---|
| 49 | return JSON.parse(s, function (key, value) {
|
|---|
| 50 | if('string' === typeof value) {
|
|---|
| 51 | if(/^:base64:/.test(value))
|
|---|
| 52 | return Buffer.from(value.substring(8), 'base64')
|
|---|
| 53 | else
|
|---|
| 54 | return /^:/.test(value) ? value.substring(1) : value
|
|---|
| 55 | }
|
|---|
| 56 | return value
|
|---|
| 57 | })
|
|---|
| 58 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.