fix bug in test/json.nas

This commit is contained in:
ValKmjolnir 2022-03-11 15:26:38 +08:00
parent 3d86a32b12
commit 3b8a092f36
1 changed files with 12 additions and 12 deletions

View File

@ -228,6 +228,9 @@ var JSON=func(){
var member=func(hash) var member=func(hash)
{ {
var name=token.content; var name=token.content;
if(token.type==j_str)
match(j_str);
else
match(j_id); match(j_id);
match(j_colon); match(j_colon);
if(token.type==j_lbrace) if(token.type==j_lbrace)
@ -244,6 +247,8 @@ var JSON=func(){
return { return {
parse:func(str) parse:func(str)
{ {
if(typeof(str)!="str")
die("JSON.parse: must use string");
get(str); get(str);
next(); next();
@ -261,6 +266,8 @@ var JSON=func(){
return res; return res;
}, },
stringify:func(hash){ stringify:func(hash){
if(typeof(hash)!="hash")
die("JSON.stringify: must use hashmap");
var s=""; var s="";
var gen=func(elem){ var gen=func(elem){
var t=typeof(elem); var t=typeof(elem);
@ -304,21 +311,14 @@ var JSON=func(){
}(); }();
var ss=JSON.stringify({ var ss=JSON.stringify({
vec:[ vec:[0,1,2],
0,
1,
2
],
hash:{ hash:{
m1:0, m1:0,
m2:"str", m2:"str",
m3:[114514], m3:[114514],
m4:{ m4:{year:1919,month:8,date:10}
year:1919, },
month:8, function:func(){}
date:10
}
}
}); });
println(ss); println(ss);
println(JSON.parse(ss)); println(JSON.parse(ss));