Skip to content

Commit b58e811

Browse files
committed
Merge pull request #13 from quartzjer/v2
Version 2!
2 parents 4cbf3b4 + 71bd993 commit b58e811

File tree

18 files changed

+240
-315
lines changed

18 files changed

+240
-315
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: c
2+
script: make test
3+
compiler:
4+
- clang
5+
- gcc

Makefile

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
MANDIR=/usr/share/man/man3
21

3-
all: js0n_test example
2+
all: test example
43

5-
js0n_test: test/js0n_test.c js0n.c j0g.c
6-
gcc -Wall -o js0n_test test/js0n_test.c js0n.c j0g.c
4+
test.c: test/test.c src/js0n.c
5+
gcc -Wall -o test/test test/test.c src/js0n.c
76

8-
example: test/example.c js0n.c j0g.c
9-
gcc -Wall -o example test/example.c js0n.c j0g.c
10-
11-
clean:
12-
rm -f example js0n_test
7+
test: test.c
8+
@if ./test/test ; then \
9+
rm -f ./test/test; \
10+
echo "TESTS PASSED"; \
11+
else \
12+
rm -f ./test/test; \
13+
echo "TESTS FAILED"; exit 1; \
14+
fi; \
1315

16+
example: test/example.c src/js0n.c
17+
gcc -Wall -o example test/example.c src/js0n.c
1418

15-
man: ${MANDIR}/js0n.3 ${MANDIR}/j0g.3
19+
clean:
20+
rm -f example test/test
1621

17-
${MANDIR}/%.3: %.3
18-
sudo cp $? $@
22+
.PHONY: all test clean

README

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# js0n - Flexible Zero-Footprint JSON Parser in C
2+
3+
A one-pass minimal overhead walk of the raw bytes, using each one as an index into a jump table to determine state transitions.
4+
5+
* public domain, single js0n.c file with one function
6+
* easy to use, just returns a given key-value pair location in the JSON string
7+
* no dependencies, embedded friendly
8+
* requires no memory/malloc or copying, uses only what is passed in (up to `int` max length)
9+
* more flexible than strict JSON and allows bare keys (non-validating)
10+
* optimized for high speed scanning/routing of small chunks of json, stops parsing upon match
11+
* safely errors on anything really bad (binary, NULLs, etc)
12+
13+
Parsing this:
14+
15+
````json
16+
{
17+
"barbar": [
18+
1,
19+
2,
20+
3
21+
],
22+
"foo": "bar",
23+
"obj": {
24+
"a": "b"
25+
}
26+
}
27+
28+
````
29+
30+
Using `val = js0n("barbar", 6, json, strlen(json), &vlen)` would return a val pointing to `[1,2,3]` and set vlen to `7`.
31+
32+
For arrays, pass `NULL` as the key, and the array offset as the second argument.
33+
34+
When the value is not found `NULL` is returned, if there were any parsing errors then vlen will be set to `-1`.
35+
36+
To determine if the returned value is an actual JSON string or a bare value (like `"true"` vs `true`), simply check if it starts with a quote character via `if(val && *(val-1) == '"')`.
37+
38+
For more usage examples see the [test.json](test/test.json) and [test.c](test/test.c).
39+
40+
## History
41+
42+
See [v1.0](https://github.com/quartzjer/js0n/tree/v1.0) for the original `js0n` function that returned an index of offsets for all key/values in one pass.

UNLICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org/>

j0g.3

Lines changed: 0 additions & 114 deletions
This file was deleted.

j0g.c

Lines changed: 0 additions & 70 deletions
This file was deleted.

j0g.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

js0n.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

js0n.3 renamed to man/js0n.3

File renamed without changes.

0 commit comments

Comments
 (0)