Skip to content

Commit

Permalink
got parse and parsefile working with the ondemand parser. currently w…
Browse files Browse the repository at this point in the history
…orking on openFile with json pointer
  • Loading branch information
FourierTransformer committed Jul 26, 2024
1 parent 4ea3cc3 commit fb7e0c1
Show file tree
Hide file tree
Showing 6 changed files with 166,825 additions and 16,411 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SRC = src/luasimdjson.cpp src/simdjson.cpp
INCLUDE = -I$(LUA_INCDIR)
LIBS = -lpthread
FLAGS = -std=c++11 -Wall $(LIBFLAG) $(CFLAGS)
FLAGS = -std=c++11 -Wall -g $(LIBFLAG) $(CFLAGS)

ifdef LUA_LIBDIR
FLAGS += $(LUA_LIBDIR)/$(LUALIB)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

A basic lua binding to [simdjson](https://simdjson.org). The simdjson library is an incredibly fast JSON parser that uses SIMD instructions and fancy algorithms to parse JSON very quickly. It's been tested with LuaJIT 2.0/2.1 and Lua 5.1, 5.2, 5.3, and 5.4 on linux/osx. It has a general parsing mode and a lazy mode that uses a JSON pointer.

Current simdjson version: 0.5.0
Current simdjson version: 3.9.5

## Installation
If all the requirements are met, lua-simdjson can be install via luarocks with:
Expand Down Expand Up @@ -36,7 +36,7 @@ The `parse` methods will return a normal lua table that can be interacted with.
```lua
local simdjson = require("simdjson")
local response = simdjson.parse([[
{
{
"Image": {
"Width": 800,
"Height": 600,
Expand Down Expand Up @@ -64,7 +64,7 @@ The `open` methods currently require the use of a JSON pointer, but are very qui
```lua
local simdjson = require("simdjson")
local response = simdjson.open([[
{
{
"Image": {
"Width": 800,
"Height": 600,
Expand All @@ -82,7 +82,7 @@ local response = simdjson.open([[
print(response:atPointer("/Image/Width"))

-- OR to parse a file from disk
local fileResponse = simdjson.open("jsonexamples/twitter.json")
local fileResponse = simdjson.openFile("jsonexamples/twitter.json")
print(fileResponse:atPointer("/statuses/0/id")) --using a JSON pointer

```
Expand Down Expand Up @@ -115,7 +115,7 @@ lua-simdjson, like the simdjson library performs better on more modern hardware.
* since it's an external module, it's not quite as easy to just grab the file and go (dkjson has you covered here!)

## Philosophy
I plan to keep it fairly inline with what the original simdjson library is capable of doing, which really means not adding too many additional options. The big _thing_ that's missing so far is encoding a lua table to JSON. I may add in an encoder at some point (likely modified from an existing lua library). There are some rumours that simdjson _may_ support creating JSON structure in the future. If that happens, I would likely switch to it.
I plan to keep it fairly inline with what the original simdjson library is capable of doing, which really means not adding too many additional options. The big _thing_ that's missing so far is encoding a lua table to JSON. I may add in an encoder at some point (likely modified from an existing lua library). There are some rumours that simdjson _may_ support creating JSON structure in the future. If that happens, I would likely switch to it.

## Licenses
* The jsonexamples, src/simdjson.cpp, src/simdjson.h are unmodified from the released version simdjson under the Apache License 2.0.
Expand Down
90 changes: 62 additions & 28 deletions spec/compile_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,68 @@ end


local files = {
"apache_builds.json",
"canada.json",
"citm_catalog.json",
"github_events.json",
"google_maps_api_compact_response.json",
"google_maps_api_response.json",
"gsoc-2018.json",
"instruments.json",
"marine_ik.json",
"mesh.json",
"mesh.pretty.json",
"numbers.json",
"random.json",
"repeat.json",
"twitter_timeline.json",
"update-center.json",
"small/adversarial.json",
"small/demo.json",
"small/flatadversarial.json",
"small/smalldemo.json",
"small/truenull.json"
"apache_builds.json",
"canada.json",
"citm_catalog.json",
"github_events.json",
"google_maps_api_compact_response.json",
"google_maps_api_response.json",
"gsoc-2018.json",
"instruments.json",
"marine_ik.json",
"mesh.json",
"mesh.pretty.json",
"numbers.json",
"random.json",
"repeat.json",
"twitter_timeline.json",
"update-center.json",
"small/adversarial.json",
"small/demo.json",
"small/flatadversarial.json",
"small/smalldemo.json",
"small/truenull.json"
}

describe("Make sure everything compiled correctly", function()
for _, file in ipairs(files) do
it("should parse the file: " .. file, function()
local fileContents = loadFile("jsonexamples/" .. file)
assert.are.same(cjson.decode(fileContents), simdjson.parse(fileContents))
end)
end
describe("Make sure it parses strings correctly", function()
for _, file in ipairs(files) do
it("should parse the file: " .. file, function()
local fileContents = loadFile("jsonexamples/" .. file)
local cjsonDecodedValues = cjson.decode(fileContents)
assert.are.same(cjsonDecodedValues, simdjson.parse(fileContents))
end)
end
end)

describe("Make sure it parses files correctly", function()
for _, file in ipairs(files) do
it("should parse the file: " .. file, function()
local fileContents = loadFile("jsonexamples/" .. file)
local cjsonDecodedValues = cjson.decode(fileContents)
assert.are.same(cjsonDecodedValues, simdjson.parseFile("jsonexamples/" .. file))
end)
end
end)

--[[
describe("Make sure json pointer works with a string", function()
it("should handle a string", function()
local fileContents = loadFile("jsonexamples/small/demo.json")
local decodedFile = simdjson.open(fileContents)
assert.are.same(800, decodedFile:atPointer("/Image/Width"))
assert.are.same(600, decodedFile:atPointer("/Image/Height"))
assert.are.same(125, decodedFile:atPointer("/Image/Thumbnail/Height"))
assert.are.same(943, decodedFile:atPointer("/Image/IDs/1"))
end)
end)
describe("Make sure json pointer works with openfile", function()
it("should handle opening a file", function()
local decodedFile = simdjson.openFile("jsonexamples/small/demo.json")
assert.are.same(800, decodedFile:atPointer("/Image/Width"))
assert.are.same(600, decodedFile:atPointer("/Image/Height"))
assert.are.same(125, decodedFile:atPointer("/Image/Thumbnail/Height"))
assert.are.same(943, decodedFile:atPointer("/Image/IDs/1"))
end)
end)
]]
Loading

0 comments on commit fb7e0c1

Please sign in to comment.