migrate json module to global
This commit is contained in:
parent
3239d6ac95
commit
8b4a3b27c0
@ -1,4 +1,3 @@
|
|||||||
local json = require("json")
|
|
||||||
local http = {}
|
local http = {}
|
||||||
|
|
||||||
-- Global routing tables
|
-- Global routing tables
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
local json = {}
|
json = {}
|
||||||
|
|
||||||
function json.encode(value)
|
function json.encode(value)
|
||||||
local buffer = {}
|
local buffer = {}
|
||||||
@ -596,5 +596,3 @@ function json.validate(data, schema)
|
|||||||
|
|
||||||
return validate_value(data, schema)
|
return validate_value(data, schema)
|
||||||
end
|
end
|
||||||
|
|
||||||
return json
|
|
@ -1,6 +1,5 @@
|
|||||||
local kv = require("kv")
|
local kv = require("kv")
|
||||||
local crypto = require("crypto")
|
local crypto = require("crypto")
|
||||||
local json = require("json")
|
|
||||||
|
|
||||||
local sessions = {}
|
local sessions = {}
|
||||||
local stores = {}
|
local stores = {}
|
||||||
|
@ -273,17 +273,23 @@ function string.slice(s, start, end_pos)
|
|||||||
end
|
end
|
||||||
getmetatable("").__index.slice = string.slice
|
getmetatable("").__index.slice = string.slice
|
||||||
|
|
||||||
-- Custom find that returns matched substring instead of position
|
|
||||||
function string.find(s, pattern, init, plain)
|
function string.find(s, pattern, init, plain)
|
||||||
if type(s) ~= "string" then error("string.find: first argument must be a string", 2) end
|
if type(s) ~= "string" then error("string.find: first argument must be a string", 2) end
|
||||||
if type(pattern) ~= "string" then error("string.find: second argument must be a string", 2) end
|
if type(pattern) ~= "string" then error("string.find: second argument must be a string", 2) end
|
||||||
|
return _orig_find(s, pattern, init, plain)
|
||||||
|
end
|
||||||
|
getmetatable("").__index.find = string.find
|
||||||
|
|
||||||
|
function string.find_match(s, pattern, init, plain)
|
||||||
|
if type(s) ~= "string" then error("string.find_match: first argument must be a string", 2) end
|
||||||
|
if type(pattern) ~= "string" then error("string.find_match: second argument must be a string", 2) end
|
||||||
local start_pos, end_pos = _orig_find(s, pattern, init, plain)
|
local start_pos, end_pos = _orig_find(s, pattern, init, plain)
|
||||||
if start_pos then
|
if start_pos then
|
||||||
return s:sub(start_pos, end_pos)
|
return s:sub(start_pos, end_pos)
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
getmetatable("").__index.find = string.find
|
getmetatable("").__index.find_match = string.find_match
|
||||||
|
|
||||||
function string.find_all(s, pattern)
|
function string.find_all(s, pattern)
|
||||||
if type(s) ~= "string" then error("string.find_all: first argument must be a string", 2) end
|
if type(s) ~= "string" then error("string.find_all: first argument must be a string", 2) end
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
require("tests")
|
require("tests")
|
||||||
local json = require("json")
|
--local json = require("json")
|
||||||
|
|
||||||
-- Test data
|
-- Test data
|
||||||
local test_data = {
|
local test_data = {
|
||||||
@ -39,7 +39,7 @@ test("Round-trip Encoding/Decoding", function()
|
|||||||
local decoded = json.decode(encoded)
|
local decoded = json.decode(encoded)
|
||||||
local re_encoded = json.encode(decoded)
|
local re_encoded = json.encode(decoded)
|
||||||
local re_decoded = json.decode(re_encoded)
|
local re_decoded = json.decode(re_encoded)
|
||||||
|
|
||||||
assert_equal(re_decoded.name, test_data.name)
|
assert_equal(re_decoded.name, test_data.name)
|
||||||
assert_equal(re_decoded.address.city, test_data.address.city)
|
assert_equal(re_decoded.address.city, test_data.address.city)
|
||||||
end)
|
end)
|
||||||
@ -50,7 +50,7 @@ test("Pretty Printing", function()
|
|||||||
assert_equal(type(pretty), "string")
|
assert_equal(type(pretty), "string")
|
||||||
assert(string.find(pretty, "\n"), "pretty should contain newlines")
|
assert(string.find(pretty, "\n"), "pretty should contain newlines")
|
||||||
assert(string.find(pretty, " "), "pretty should contain indentation")
|
assert(string.find(pretty, " "), "pretty should contain indentation")
|
||||||
|
|
||||||
-- Should still be valid JSON
|
-- Should still be valid JSON
|
||||||
local decoded = json.decode(pretty)
|
local decoded = json.decode(pretty)
|
||||||
assert_equal(decoded.name, test_data.name)
|
assert_equal(decoded.name, test_data.name)
|
||||||
@ -61,7 +61,7 @@ test("Object Merging", function()
|
|||||||
local obj1 = {a = 1, b = 2}
|
local obj1 = {a = 1, b = 2}
|
||||||
local obj2 = {b = 3, c = 4}
|
local obj2 = {b = 3, c = 4}
|
||||||
local obj3 = {d = 5}
|
local obj3 = {d = 5}
|
||||||
|
|
||||||
local merged = json.merge(obj1, obj2, obj3)
|
local merged = json.merge(obj1, obj2, obj3)
|
||||||
assert_equal(merged.a, 1)
|
assert_equal(merged.a, 1)
|
||||||
assert_equal(merged.b, 3) -- later wins
|
assert_equal(merged.b, 3) -- later wins
|
||||||
@ -73,13 +73,13 @@ end)
|
|||||||
test("Data Extraction", function()
|
test("Data Extraction", function()
|
||||||
local name = json.extract(test_data, "name")
|
local name = json.extract(test_data, "name")
|
||||||
assert_equal(name, "John Doe")
|
assert_equal(name, "John Doe")
|
||||||
|
|
||||||
local city = json.extract(test_data, "address.city")
|
local city = json.extract(test_data, "address.city")
|
||||||
assert_equal(city, "Springfield")
|
assert_equal(city, "Springfield")
|
||||||
|
|
||||||
local first_score = json.extract(test_data, "scores.[0]")
|
local first_score = json.extract(test_data, "scores.[0]")
|
||||||
assert_equal(first_score, 85)
|
assert_equal(first_score, 85)
|
||||||
|
|
||||||
local missing = json.extract(test_data, "nonexistent.field")
|
local missing = json.extract(test_data, "nonexistent.field")
|
||||||
assert_equal(missing, nil)
|
assert_equal(missing, nil)
|
||||||
end)
|
end)
|
||||||
@ -95,10 +95,10 @@ test("Schema Validation", function()
|
|||||||
},
|
},
|
||||||
required = {name = true, age = true}
|
required = {name = true, age = true}
|
||||||
}
|
}
|
||||||
|
|
||||||
local valid, err = json.validate(test_data, schema)
|
local valid, err = json.validate(test_data, schema)
|
||||||
assert_equal(valid, true)
|
assert_equal(valid, true)
|
||||||
|
|
||||||
local invalid_data = {name = "John", age = "not_a_number"}
|
local invalid_data = {name = "John", age = "not_a_number"}
|
||||||
local invalid, err2 = json.validate(invalid_data, schema)
|
local invalid, err2 = json.validate(invalid_data, schema)
|
||||||
assert_equal(invalid, false)
|
assert_equal(invalid, false)
|
||||||
@ -108,18 +108,18 @@ end)
|
|||||||
-- Test 8: File operations
|
-- Test 8: File operations
|
||||||
test("File Save/Load", function()
|
test("File Save/Load", function()
|
||||||
local filename = "test_output.json"
|
local filename = "test_output.json"
|
||||||
|
|
||||||
-- Save to file
|
-- Save to file
|
||||||
json.save_file(filename, test_data, true) -- pretty format
|
json.save_file(filename, test_data, true) -- pretty format
|
||||||
|
|
||||||
-- Check file exists
|
-- Check file exists
|
||||||
assert(file_exists(filename), "file should exist after save")
|
assert(file_exists(filename), "file should exist after save")
|
||||||
|
|
||||||
-- Load from file
|
-- Load from file
|
||||||
local loaded = json.load_file(filename)
|
local loaded = json.load_file(filename)
|
||||||
assert_equal(loaded.name, test_data.name)
|
assert_equal(loaded.name, test_data.name)
|
||||||
assert_equal(loaded.address.zip, test_data.address.zip)
|
assert_equal(loaded.address.zip, test_data.address.zip)
|
||||||
|
|
||||||
-- Clean up
|
-- Clean up
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
end)
|
end)
|
||||||
@ -130,8 +130,8 @@ test("Error Handling", function()
|
|||||||
local success, err = pcall(json.decode, '{"invalid": json}')
|
local success, err = pcall(json.decode, '{"invalid": json}')
|
||||||
assert_equal(success, false)
|
assert_equal(success, false)
|
||||||
assert_equal(type(err), "string")
|
assert_equal(type(err), "string")
|
||||||
|
|
||||||
-- Missing file should throw error
|
-- Missing file should throw error
|
||||||
local success2, err2 = pcall(json.load_file, "nonexistent_file.json")
|
local success2, err2 = pcall(json.load_file, "nonexistent_file.json")
|
||||||
assert_equal(success2, false)
|
assert_equal(success2, false)
|
||||||
assert_equal(type(err2), "string")
|
assert_equal(type(err2), "string")
|
||||||
@ -144,7 +144,7 @@ test("Edge Cases", function()
|
|||||||
local encoded_empty = json.encode(empty_obj)
|
local encoded_empty = json.encode(empty_obj)
|
||||||
local decoded_empty = json.decode(encoded_empty)
|
local decoded_empty = json.decode(encoded_empty)
|
||||||
assert_equal(type(decoded_empty), "table")
|
assert_equal(type(decoded_empty), "table")
|
||||||
|
|
||||||
-- Special numbers
|
-- Special numbers
|
||||||
local special = {
|
local special = {
|
||||||
zero = 0,
|
zero = 0,
|
||||||
@ -168,21 +168,21 @@ test("Performance Test", function()
|
|||||||
data = {x = i * 2, y = i * 3, z = i * 4}
|
data = {x = i * 2, y = i * 3, z = i * 4}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local start = os.clock()
|
local start = os.clock()
|
||||||
local encoded = json.encode(large_data)
|
local encoded = json.encode(large_data)
|
||||||
local encode_time = os.clock() - start
|
local encode_time = os.clock() - start
|
||||||
|
|
||||||
start = os.clock()
|
start = os.clock()
|
||||||
local decoded = json.decode(encoded)
|
local decoded = json.decode(encoded)
|
||||||
local decode_time = os.clock() - start
|
local decode_time = os.clock() - start
|
||||||
|
|
||||||
print(string.format(" Encoded 1000 objects in %.3f seconds", encode_time))
|
print(string.format(" Encoded 1000 objects in %.3f seconds", encode_time))
|
||||||
print(string.format(" Decoded 1000 objects in %.3f seconds", decode_time))
|
print(string.format(" Decoded 1000 objects in %.3f seconds", decode_time))
|
||||||
|
|
||||||
assert_equal(#decoded, 1000)
|
assert_equal(#decoded, 1000)
|
||||||
assert_equal(decoded[500].name, "User 500")
|
assert_equal(decoded[500].name, "User 500")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
summary()
|
summary()
|
||||||
test_exit()
|
test_exit()
|
||||||
|
@ -290,11 +290,11 @@ test("string.match", function()
|
|||||||
assert_equal(true, string.match("testing", "test") ~= nil)
|
assert_equal(true, string.match("testing", "test") ~= nil)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test("string.find", function()
|
test("string.find_match", function()
|
||||||
assert_equal("123", string.find("hello123world", "%d+"))
|
assert_equal("123", string.find_match("hello123world", "%d+"))
|
||||||
assert_equal(nil, string.find("hello", "%d+"))
|
assert_equal(nil, string.find_match("hello", "%d+"))
|
||||||
assert_equal("test", string.find("testing", "test"))
|
assert_equal("test", string.find_match("testing", "test"))
|
||||||
assert_equal("world", string.find("hello world", "world"))
|
assert_equal("world", string.find_match("hello world", "world"))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
test("string.find_all", function()
|
test("string.find_all", function()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user