mirror of
https://github.com/flynx/serialize.js.git
synced 2026-10-03 13:01:47 +00:00
Compare commits
6 Commits
1bdca62df9
...
c548a71aed
| Author | SHA1 | Date | |
|---|---|---|---|
| c548a71aed | |||
| 6e1ad92963 | |||
| 71d28aabc9 | |||
| 5d0beb3bc7 | |||
| 9bc96a9396 | |||
| 81839f8b24 |
18
README.md
18
README.md
@ -15,6 +15,12 @@ Data not stored:
|
|||||||
- Attributes on arrays, maps, sets, and functions,
|
- Attributes on arrays, maps, sets, and functions,
|
||||||
- Function closures.
|
- Function closures.
|
||||||
|
|
||||||
|
Relaxed JSON input format:
|
||||||
|
- Comments are treated as whitespace and are stripped from input
|
||||||
|
- Trailing commas are ignored
|
||||||
|
- All JavaScript string quotes are supported
|
||||||
|
- Object keys are optionally quoted if they confirm to JavaScript identifier spec
|
||||||
|
|
||||||
Note that this a strict superset of JSON, this if only JSON-supported
|
Note that this a strict superset of JSON, this if only JSON-supported
|
||||||
data is serialized the output will be strict JSON.
|
data is serialized the output will be strict JSON.
|
||||||
|
|
||||||
@ -193,6 +199,18 @@ eJSON.parse(<string>, {functions: <functions>})
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### `toVanilla(..)`
|
||||||
|
|
||||||
|
Convert eJSON formatted string to Vanilla JSON string.
|
||||||
|
|
||||||
|
```
|
||||||
|
toVanilla(<eJSON-string>)
|
||||||
|
-> <JSON-string>
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that this will fail on anny data not supported by the Vanilla `JOSN.stringify(..)`.
|
||||||
|
|
||||||
|
|
||||||
### `deepCopy(..)`
|
### `deepCopy(..)`
|
||||||
|
|
||||||
Deep-copy an object.
|
Deep-copy an object.
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-serialize",
|
"name": "ig-serialize",
|
||||||
"version": "1.3.2",
|
"version": "1.3.4",
|
||||||
"description": "experimental extended json serializaion...",
|
"description": "experimental extended json serializaion...",
|
||||||
"main": "serialize.js",
|
"main": "serialize.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
41
serialize.js
41
serialize.js
@ -53,7 +53,6 @@
|
|||||||
(function(require){ var module={} // make module AMD/node compatible...
|
(function(require){ var module={} // make module AMD/node compatible...
|
||||||
/*********************************************************************/
|
/*********************************************************************/
|
||||||
|
|
||||||
|
|
||||||
var NULL = 'null'
|
var NULL = 'null'
|
||||||
var UNDEFINED = 'undefined'
|
var UNDEFINED = 'undefined'
|
||||||
var NAN = 'NaN'
|
var NAN = 'NaN'
|
||||||
@ -333,10 +332,8 @@ module.eJSON = {
|
|||||||
undefined: undefined,
|
undefined: undefined,
|
||||||
NaN: NaN,
|
NaN: NaN,
|
||||||
|
|
||||||
//'<REF': 'reference',
|
|
||||||
[REFERENCE.split('%')[0]]: 'reference',
|
[REFERENCE.split('%')[0]]: 'reference',
|
||||||
|
|
||||||
//'<FUNC[': 'func',
|
|
||||||
[FUNCTION.split('%')[0]]: 'func',
|
[FUNCTION.split('%')[0]]: 'func',
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -445,19 +442,40 @@ module.eJSON = {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE: this treats comments as whitespace...
|
||||||
|
// XXX will we need to process comments in any way?
|
||||||
|
COMMENTS: true,
|
||||||
WHITESPACE: ' \t\n',
|
WHITESPACE: ' \t\n',
|
||||||
skipWhitespace: function(str, i, line){
|
skipWhitespace: function(str, i, line){
|
||||||
while(i < str.length
|
while(i < str.length){
|
||||||
&& this.WHITESPACE.includes(str[i])){
|
if(this.COMMENTS){
|
||||||
|
// comment: '// ... \n'
|
||||||
|
if(str[i] == '/' && str[i+1] == '/'){
|
||||||
|
while(i < str.length && str[i] != '\n'){
|
||||||
|
i++ } }
|
||||||
|
// comment: '/* ... */'
|
||||||
|
if(str[i] == '/' && str[i+1] == '*'){
|
||||||
|
while(i < str.length && str.slice(i, i+2) != '*/'){
|
||||||
|
if(str[i] == '\n'){
|
||||||
|
line++ }
|
||||||
|
i++ }
|
||||||
|
i += 2
|
||||||
|
if(i > str.length){
|
||||||
|
this.error('Unexpected end of input wile looking for "*/".', str, i-2, line) } } }
|
||||||
|
// non-whitespace...
|
||||||
|
if(!this.WHITESPACE.includes(str[i])){
|
||||||
|
break }
|
||||||
if(str[i] == '\n'){
|
if(str[i] == '\n'){
|
||||||
line++ }
|
line++ }
|
||||||
i++ }
|
i++ }
|
||||||
return [i, line] },
|
return [i, line] },
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// .handler(match, str, i, line)
|
// .handler(state, path, match, str, i, line)
|
||||||
// -> [value, i, line]
|
// -> [value, i, line]
|
||||||
//
|
//
|
||||||
|
|
||||||
number: function(state, path, match, str, i, line){
|
number: function(state, path, match, str, i, line){
|
||||||
debug.lex('number', str, i, line)
|
debug.lex('number', str, i, line)
|
||||||
// special cases..,
|
// special cases..,
|
||||||
@ -771,6 +789,17 @@ function(str, options){
|
|||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// utils...
|
// utils...
|
||||||
|
|
||||||
|
|
||||||
|
// Concert eJSON to vanilla JSON if possible...
|
||||||
|
//
|
||||||
|
// NOTE: this will fail on any non-vanilla data...
|
||||||
|
var toVanilla =
|
||||||
|
module.toVanilla =
|
||||||
|
function(str){
|
||||||
|
return JSON.stringify(
|
||||||
|
deserialize(str)) }
|
||||||
|
|
||||||
|
|
||||||
var deepCopy =
|
var deepCopy =
|
||||||
module.deepCopy =
|
module.deepCopy =
|
||||||
function(obj, funcs=true){
|
function(obj, funcs=true){
|
||||||
|
|||||||
22
test.js
22
test.js
@ -28,6 +28,10 @@ var ejson = false
|
|||||||
|
|
||||||
var pre_cycle = true
|
var pre_cycle = true
|
||||||
|
|
||||||
|
// XXX BUG: deserialize('1 2') -> 1
|
||||||
|
// should throw:
|
||||||
|
// 'SyntaxError: Unexpected non-whitespace character after JSON at position 2 (line 1 column 3)'
|
||||||
|
|
||||||
// XXX test whitespace handling...
|
// XXX test whitespace handling...
|
||||||
var setups = test.Setups({
|
var setups = test.Setups({
|
||||||
'true': function(assert){
|
'true': function(assert){
|
||||||
@ -228,6 +232,24 @@ test.Cases({
|
|||||||
|
|
||||||
// arrays...
|
// arrays...
|
||||||
['[1,2,]', '[1,2]'],
|
['[1,2,]', '[1,2]'],
|
||||||
|
|
||||||
|
// comments...
|
||||||
|
['123 // comment...', '123'],
|
||||||
|
['// comment...\n123', '123'],
|
||||||
|
['// comment...\n123// comment...', '123'],
|
||||||
|
['/* comment */ 123', '123'],
|
||||||
|
['123 /* comment */', '123'],
|
||||||
|
['/* comment */123/* comment */', '123'],
|
||||||
|
[`// comment...
|
||||||
|
[
|
||||||
|
// comment...
|
||||||
|
1, // comment...
|
||||||
|
2 /* comment */,
|
||||||
|
// comment...
|
||||||
|
/* comment */ 3 /* comment*/,
|
||||||
|
// comment...
|
||||||
|
]
|
||||||
|
// comment...`, '[1,2,3]']
|
||||||
],
|
],
|
||||||
'syntax-simplifications': function(assert){
|
'syntax-simplifications': function(assert){
|
||||||
var aa, bb
|
var aa, bb
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user