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