change json.JSON to json

This commit is contained in:
ValKmjolnir 2023-10-25 00:32:42 +08:00
parent 9f7484596a
commit d56e1bff2c
3 changed files with 50 additions and 51 deletions

View File

@ -19,7 +19,7 @@ var _j_content = [
"identifier" "identifier"
]; ];
var JSON = func() { var parse = func() {
var text = ""; var text = "";
var line = 1; var line = 1;
@ -66,7 +66,7 @@ var JSON = func() {
var get = func(str) { var get = func(str) {
init(); init();
if (!size(str)) { if (!size(str)) {
println("JSON.parse: empty string"); println("json::parse: empty string");
str = "[]"; str = "[]";
} }
text = str; text = str;
@ -147,7 +147,7 @@ var JSON = func() {
var match = func(type) { var match = func(type) {
if(token.type!=type) if(token.type!=type)
println("JSON.parse: line ",line,": expect ",_j_content[type]," but get `",token.content,"`."); println("json::parse: line ",line,": expect ",_j_content[type]," but get `",token.content,"`.");
next(); next();
return; return;
} }
@ -212,13 +212,13 @@ var JSON = func() {
return vec; return vec;
} }
return { return func(source) {
parse:func(str) { if(typeof(source)!="str") {
if(typeof(str)!="str") { println("json::parse: must use string but get", typeof(str));
println("JSON.parse: must use string");
return []; return [];
} }
get(str);
get(source);
next(); next();
if (token.type==_j_lbrkt) { if (token.type==_j_lbrkt) {
@ -230,12 +230,12 @@ var JSON = func() {
init(); init();
return res; return res;
} }
};
}(); }();
JSON.stringify = func(object) { var stringify = func(object) {
if(typeof(object)!="hash" and typeof(object)!="vec") { var object_type = typeof(object);
println("JSON.stringify: must use hashmap or vector"); if(object_type!="hash" and object_type!="vec" and object_type!="namespace") {
println("json::stringify: must use hashmap or vector, but get ", typeof(object));
return "[]"; return "[]";
} }

View File

@ -1,9 +1,7 @@
import.std.json; import.std.json;
import.std.process_bar; import.std.process_bar;
var JSON = json.JSON; var ss = json.stringify({
var ss=JSON.stringify({
vec:[0,1,2], vec:[0,1,2],
hash:{ hash:{
m1:0, m1:0,
@ -17,10 +15,11 @@ var ss=JSON.stringify({
empty_an:[[[[[[{}]]]]]], empty_an:[[[[[[{}]]]]]],
function:func(){} function:func(){}
}); });
println(ss,"\n");
println(JSON.parse(ss),"\n");
var ss=JSON.stringify([{ println(ss, "\n");
println(json.parse(ss), "\n");
var ss = json.stringify([{
vec:[0,1,2,3], vec:[0,1,2,3],
hash:{ hash:{
m1:0, m1:0,
@ -33,8 +32,9 @@ var ss=JSON.stringify([{
empty_an:[[[[[{}]]]]], empty_an:[[[[[{}]]]]],
function:func(){} function:func(){}
}]); }]);
println(ss, "\n"); println(ss, "\n");
println(JSON.parse(ss),"\n"); println(json.parse(ss), "\n");
func { func {
var bar = process_bar.high_resolution_bar(30); var bar = process_bar.high_resolution_bar(30);
@ -61,7 +61,7 @@ func {
print("\e[1000D", bar.bar(1), " executing...\n"); print("\e[1000D", bar.bar(1), " executing...\n");
} }
print("\e[1000D", "\e["~str(size(tmp))~"A"); print("\e[1000D", "\e["~str(size(tmp))~"A");
foreach(var h;JSON.parse(JSON.stringify(tmp))) { foreach(var h; json.parse(json.stringify(tmp))) {
println("\e[1000D", bar.bar(1), " parse done ", keys(h)[0], " ", size(h[keys(h)[0]])); println("\e[1000D", bar.bar(1), " parse done ", keys(h)[0], " ", size(h[keys(h)[0]]));
} }
}(); }();

View File

@ -2,7 +2,6 @@ import.module.libsock;
import.std.json; import.std.json;
import.std.runtime; import.std.runtime;
var JSON = json.JSON;
var socket = libsock.socket; var socket = libsock.socket;
var gettime=func(){ var gettime=func(){
@ -79,19 +78,19 @@ var server=func(ip,port) {
while(1) { while(1) {
var data=jsonRPC.recv(client); var data=jsonRPC.recv(client);
if (data!=nil) { if (data!=nil) {
data=JSON.parse(data); data=json.parse(data);
} else { } else {
break; break;
} }
if (contains(methods,data.method)) { if (contains(methods,data.method)) {
jsonRPC.send(client, JSON.stringify({ jsonRPC.send(client, json.stringify({
jsonrpc:2.0, jsonrpc:2.0,
id:data.id, id:data.id,
error:"null", error:"null",
result:methods[data.method](data.params) result:methods[data.method](data.params)
})); }));
} else { } else {
jsonRPC.send(client, JSON.stringify({ jsonRPC.send(client, json.stringify({
jsonrpc:2.0, jsonrpc:2.0,
id:data.id, id:data.id,
error:"no such method \\\""~data.method~"\\\"", error:"no such method \\\""~data.method~"\\\"",
@ -113,13 +112,13 @@ var client=func(ip,port) {
var server=jsonRPC.connect(ip,port); var server=jsonRPC.connect(ip,port);
while(1) { while(1) {
unix.sleep(5); unix.sleep(5);
var data=JSON.stringify({jsonrpc:2.0, id:call_id, method:methods[rand()*size(methods)],params:params[rand()*size(params)]}); var data=json.stringify({jsonrpc:2.0, id:call_id, method:methods[rand()*size(methods)],params:params[rand()*size(params)]});
jsonRPC.send(server, data); jsonRPC.send(server, data);
var respond=jsonRPC.recv(server); var respond=jsonRPC.recv(server);
if (respond==nil) { if (respond==nil) {
break; break;
} }
println("[",gettime(),"] result: ",JSON.parse(respond).result); println("[",gettime(),"] result: ",json.parse(respond).result);
call_id+=1; call_id+=1;
} }
jsonRPC.disconnect(server); jsonRPC.disconnect(server);