|
| 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 | + |
0 commit comments