Skip to content

Commit 2a1a64f

Browse files
committed
Merge pull request #12 from mbucc/master
Man pages and make man target.
2 parents 49455b0 + d986c04 commit 2a1a64f

File tree

5 files changed

+242
-1
lines changed

5 files changed

+242
-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 ${MANDIR}/j0g.3
16+
17+
${MANDIR}/%.3: %.3
18+
sudo cp $? $@

j0g.3

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
.\"
2+
.\" Copyright (c) 2014 Jeremie Miller <[email protected]>
3+
.\"
4+
.\"
5+
.\" This software is in the pubic domain.
6+
.\"
7+
.\"
8+
.Dd $Mdocdate: August 1, 2014 $
9+
.Dt J0G 3
10+
.Os
11+
.Sh NAME
12+
.Nm j0g ,
13+
.Nm j0g_str ,
14+
.Nm j0g_safe ,
15+
.Nm j0g_test ,
16+
.Nm j0g_val ,
17+
.Nm j0g_val
18+
.Nd utility functions for js0n parsing libary.
19+
.Sh SYNOPSIS
20+
.Fd "#include <js0n.h>"
21+
.Fd "#include <j0g.h>"
22+
.Pp
23+
.Ft char *
24+
.Fn j0g "const char *json" "unsigned short *index" "int ilen"
25+
.Pp
26+
.Ft int
27+
.Fn j0g_val "const char *key" "char *json" "const unsigned short *index"
28+
.Pp
29+
.Ft char *
30+
.Fn j0g_safe "int val" "char *json" "const unsigned short *index"
31+
.Pp
32+
.Ft char *
33+
.Fn j0g_str "const char *key" "char *json" "const unsigned short *index"
34+
.Pp
35+
.Ft int
36+
.Fn j0g_test "const char *key" "char *json" "const unsigned short *index"
37+
38+
.Sh DESCRIPTION
39+
Convenience functions that make it easier to work with
40+
.Nm js0n .
41+
All but
42+
.Fn j0g
43+
operate on a particular key in the json string.
44+
45+
.Bl -tag -width Ds
46+
47+
.It Xo
48+
.Fa char *
49+
.Fn j0g "const char *json" "unsigned short *index" "int ilen"
50+
.Xc
51+
.Pp
52+
A simpler version of
53+
.Fn js0n
54+
that assumes the
55+
.Fa json
56+
argument is a null-terminated string.
57+
58+
59+
.It Xo
60+
.Fa int
61+
.Fn j0g_val "const char *key" "char *json" "const unsigned short *index" ;
62+
.Xc
63+
.Pp
64+
For the given key, return the array element
65+
that holds the starting position of the key's value. For example, if
66+
index[2] holds the offset from the beginning of the json string
67+
to the beginning of the
68+
.Fa key ,
69+
then
70+
.Fn j0g_val
71+
would return a 4 (index[3] holds the length of the key).
72+
.Pp
73+
If the key is not found (or if the key or the json is a null pointer), return 0.
74+
75+
.It Xo
76+
.Fa char *
77+
.Fn j0g_safe "int val" "char *json" "const unsigned short *index" ;
78+
.Xc
79+
.Pp
80+
Modifies
81+
.Fa json
82+
passed in, null-terminating the value (or
83+
key) whose offset is stored in the
84+
.Fa index
85+
array array location
86+
.Fa val .
87+
Also, unescapes newlines and double quotes in-place in
88+
.Fa json.
89+
Returns the null-terminated and unescaped string value.
90+
.Pp
91+
Note that this function does not unescape all escaped characters
92+
in the JSON specification; only
93+
newlines and double-quotes.
94+
95+
.It Xo
96+
.Fa char *
97+
.Fn j0g_str "const char *key" "char *json" "const unsigned short *index" ;
98+
.Xc
99+
.Pp
100+
Return the null-terminated and (partially) unescaped string value
101+
matching the given key.
102+
If no such key, return NULL.
103+
104+
.It Xo
105+
.Fa int
106+
.Fn j0g_test "const char *key" "char *json" "const unsigned short *index" ;
107+
.Xc
108+
.Pp
109+
Return 1 if the value is ``true'' or ``1'', false otherwise
110+
.El
111+
112+
.Sh SEE ALSO
113+
.Xr js0n 3
114+

js0n.3

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
.\"
2+
.\" Copyright (c) 2014 Jeremie Miller <[email protected]>
3+
.\"
4+
.\"
5+
.\" This software is in the public domain.
6+
.\"
7+
.\"
8+
.Dd $Mdocdate: August 1, 2014 $
9+
.Dt JS0N 3
10+
.Os
11+
.Sh NAME
12+
.Nm js0n
13+
.Nd json parsing library
14+
.Sh SYNOPSIS
15+
.Fd "#include <js0n.h>"
16+
.Pp
17+
.Ft int
18+
.Fn js0n "const unsigned char *js" "unsigned int len" "unsigned short *out" "unsigned int olen"
19+
20+
.Sh DESCRIPTION
21+
The
22+
.Nm js0n
23+
function parses a JSON string and
24+
is designed to be simple, lightweight and fast.
25+
Unlike most JSON parsers,
26+
.Nm js0n
27+
hardly allocates any memory.
28+
Rather, it walks the string and
29+
records the sequence of (offset, length) integer pairs
30+
that describe the location of the first-level keys and values.
31+
.Pp
32+
It should parse any valid json, but trades full
33+
validation for efficiency (some invalid json will still parse).
34+
.Pp
35+
Excellent for low level high speed scanning/routing of small chunks
36+
of json.
37+
.Sh RETURN VALUE
38+
.Nm js0n
39+
returns 0 on success.
40+
If the data was incomplete (for example, missing a close brace)
41+
or invalid (for example, a string value containing a character
42+
with an ASCII code less than 32),
43+
then a number greater than zero is returned.
44+
45+
.Sh EXAMPLE
46+
The following code fragment illustrates the simple case:
47+
.Bd -literal -offset indent
48+
char *s = "{\\"foo\\":\\"bar\\",\\"barbar\\":[1,2,3],\\"obj\\":{\\"a\\":\\"b\\"}}";
49+
// 3 keys, 3 values, each with a start and offset = 12
50+
// Plus one for a terminating zero = 13.
51+
unsigned short kvpairs[13];
52+
53+
\&...
54+
55+
int rc = js0n((unsigned char*) s, strlen(s), kvpairs, 13);
56+
if (!rc)
57+
for (int i = 0; kvpairs[i]; i += 2)
58+
printf("%d: at %d len %d is %.*s\n", i,
59+
kvpairs[i], kvpairs[i + 1], kvpairs[i + 1], s + kvpairs[i]);
60+
else
61+
errx("Parse failed.");
62+
63+
.Ed
64+
65+
produces this output:
66+
67+
.Bd -literal -offset indent0: at 2 len 3 is foo
68+
2: at 8 len 3 is bar
69+
4: at 14 len 6 is barbar
70+
6: at 22 len 7 is [1,2,3]
71+
8: at 31 len 3 is obj
72+
10: at 36 len 9 is {"a":"b"}
73+
.Ed
74+
75+
.Sh SEE ALSO
76+
.Xr j0g 3
77+

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)