Skip to content

Commit bf05026

Browse files
committed
man page for js0n and example program
1 parent 49455b0 commit bf05026

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
js0n_test
2+
example

Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
all:
1+
MANDIR=/usr/share/man/man3
2+
3+
all: js0n_test example
4+
5+
js0n_test: test/js0n_test.c js0n.c j0g.c
26
gcc -Wall -o js0n_test test/js0n_test.c js0n.c j0g.c
7+
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
13+
14+
15+
man: ${MANDIR}/js0n.3
16+
17+
${MANDIR}/js0n.3: js0n.3
18+
sudo cp $? $@

js0n.3

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
.\"
2+
.\" Copyright (c) 2014 Jeremie Miller <[email protected]>
3+
.\"
4+
.\"
5+
.\" This software is provided 'as-is', without any express or implied
6+
.\" warranty. In no event will the authors be held liable for any damages
7+
.\" arising from the use of this software.
8+
.\"
9+
.\" Permission is granted to anyone to use this software for any purpose,
10+
.\" including commercial applications, and to alter it and redistribute it
11+
.\" freely, subject to the following restrictions:
12+
.\"
13+
.\" The origin of this software must not be misrepresented; you must not
14+
.\" claim that you wrote the original software. If you use this software
15+
.\" in a product, an acknowledgment in the product documentation would be
16+
.\" appreciated but is not required.
17+
.\" Altered source versions must be plainly marked as such, and must not be
18+
.\" misrepresented as being the original software.
19+
.\" This notice may not be removed or altered from any source distribution.
20+
.\"
21+
.\"
22+
.Dd $Mdocdate: August 1, 2014 $
23+
.Dt JS0N 3
24+
.Os
25+
.Sh NAME
26+
.Nm js0n
27+
.Nd json parsing library
28+
.Sh SYNOPSIS
29+
.Fd "#include <js0n.h>"
30+
.Pp
31+
.Ft int
32+
.Fn js0n "const unsigned char *js" "unsigned int len" "unsigned short *out" "unsigned int olen"
33+
34+
.Sh DESCRIPTION
35+
The
36+
.Nm js0n
37+
function parses a JSON string and
38+
is designed to be simple, lightweight and fast.
39+
Unlike most JSON parsers,
40+
.Nm js0n
41+
hardly allocates any memory.
42+
Rather, it walks the string and
43+
records the sequence of (offset, length) integer pairs
44+
that describe the location of the first-level keys and values.
45+
.Pp
46+
It should parse any valid json, but trades full
47+
validation for efficiency (some invalid json will still parse).
48+
.Pp
49+
Excellent for low level high speed scanning/routing of small chunks
50+
of json.
51+
.Sh RETURN VALUE
52+
.Nm js0n
53+
returns 0 on success.
54+
If the data was incomplete (for example, missing a close brace)
55+
or invalid (for example, a string value containing a character
56+
with an ASCII code less than 32),
57+
then a number greater than zero is returned.
58+
59+
.Sh EXAMPLE
60+
The following code fragment illustrates the simple case:
61+
.Bd -literal -offset indent
62+
char *s = "{\\"foo\\":\\"bar\\",\\"barbar\\":[1,2,3],\\"obj\\":{\\"a\\":\\"b\\"}}";
63+
// 3 keys, 3 values, each with a start and offset = 12
64+
// Plus one for a terminating zero = 13.
65+
unsigned short kvpairs[13];
66+
67+
\&...
68+
69+
int rc = js0n((unsigned char*) s, strlen(s), kvpairs, 13);
70+
if (!rc)
71+
for (int i = 0; kvpairs[i]; i += 2)
72+
printf("%d: at %d len %d is %.*s\n", i,
73+
kvpairs[i], kvpairs[i + 1], kvpairs[i + 1], s + kvpairs[i]);
74+
else
75+
errx("Parse failed.");
76+
77+
.Ed
78+
79+
produces this output:
80+
81+
.Bd -literal -offset indent0: at 2 len 3 is foo
82+
2: at 8 len 3 is bar
83+
4: at 14 len 6 is barbar
84+
6: at 22 len 7 is [1,2,3]
85+
8: at 31 len 3 is obj
86+
10: at 36 len 9 is {"a":"b"}
87+
.Ed
88+
89+
.Sh SEE ALSO
90+
.Xr j0g 3
91+

test/example.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "../js0n.h"
5+
#include "../j0g.h"
6+
7+
void
8+
ex1()
9+
{
10+
char *s = "{\"foo\":\"bar\",\"barbar\":[1,2,3],\"obj\":{\"a\":\"b\"}}";
11+
12+
// 3 keys, 3 values, each with a start and offset --> 12
13+
// Plus one for a terminating zero = 13.
14+
unsigned short kvpairs[13];
15+
16+
printf("Parsing '%s'\n", s);
17+
18+
int rc = js0n((unsigned char*) s, strlen(s), kvpairs, 13);
19+
20+
printf("returned %d\n",rc);
21+
22+
for (int i = 0; kvpairs[i]; i += 2)
23+
24+
printf("%d: at %d len %d is %.*s\n", i,
25+
kvpairs[i], kvpairs[i + 1], kvpairs[i + 1], s + kvpairs[i]);
26+
27+
}
28+
29+
int main(int argc, char **argv)
30+
{
31+
ex1();
32+
}
33+

0 commit comments

Comments
 (0)