1*0a6a1f1dSLionel Sambuc.\" $NetBSD: libmj.3,v 1.7 2014/02/17 07:23:18 agc Exp $ 2ebfedea0SLionel Sambuc.\" 3ebfedea0SLionel Sambuc.\" Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org> 4ebfedea0SLionel Sambuc.\" All rights reserved. 5ebfedea0SLionel Sambuc.\" 6ebfedea0SLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 7ebfedea0SLionel Sambuc.\" modification, are permitted provided that the following conditions 8ebfedea0SLionel Sambuc.\" are met: 9ebfedea0SLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 10ebfedea0SLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 11ebfedea0SLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 12ebfedea0SLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 13ebfedea0SLionel Sambuc.\" documentation and/or other materials provided with the distribution. 14ebfedea0SLionel Sambuc.\" 15ebfedea0SLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16ebfedea0SLionel Sambuc.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17ebfedea0SLionel Sambuc.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18ebfedea0SLionel Sambuc.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19ebfedea0SLionel Sambuc.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20ebfedea0SLionel Sambuc.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21ebfedea0SLionel Sambuc.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22ebfedea0SLionel Sambuc.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23ebfedea0SLionel Sambuc.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24ebfedea0SLionel Sambuc.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25ebfedea0SLionel Sambuc.\" 26*0a6a1f1dSLionel Sambuc.Dd February 16, 2014 27ebfedea0SLionel Sambuc.Dt LIBMJ 3 28ebfedea0SLionel Sambuc.Os 29ebfedea0SLionel Sambuc.Sh NAME 30ebfedea0SLionel Sambuc.Nm libmj 31ebfedea0SLionel Sambuc.Nd minimalist JSON lightweight data interchange library 32ebfedea0SLionel Sambuc.Sh LIBRARY 33ebfedea0SLionel Sambuc.Lb libmj 34ebfedea0SLionel Sambuc.Sh SYNOPSIS 35ebfedea0SLionel Sambuc.In mj.h 36ebfedea0SLionel Sambuc.Ft int 37ebfedea0SLionel Sambuc.Fo mj_create 38ebfedea0SLionel Sambuc.Fa "mj_t *atom" "const char *text" "..." 39ebfedea0SLionel Sambuc.Fc 40ebfedea0SLionel Sambuc.Ft int 41ebfedea0SLionel Sambuc.Fo mj_parse 42ebfedea0SLionel Sambuc.Fa "mj_t *atom" "const char *text" "int *tokfrom" "int *tokto" "int *toktype" 43ebfedea0SLionel Sambuc.Fc 44ebfedea0SLionel Sambuc.Ft int 45ebfedea0SLionel Sambuc.Fo mj_append 46ebfedea0SLionel Sambuc.Fa "mj_t *atom" "const char *text" "..." 47ebfedea0SLionel Sambuc.Fc 48ebfedea0SLionel Sambuc.Ft int 49ebfedea0SLionel Sambuc.Fo mj_append_field 50ebfedea0SLionel Sambuc.Fa "mj_t *atom" "const char *fieldname" "const char *text" "..." 51ebfedea0SLionel Sambuc.Fc 52ebfedea0SLionel Sambuc.Ft int 53ebfedea0SLionel Sambuc.Fo mj_deepcopy 54ebfedea0SLionel Sambuc.Fa "mj_t *dest" "mj_t *src" 55ebfedea0SLionel Sambuc.Fc 56ebfedea0SLionel Sambuc.Ft void 57ebfedea0SLionel Sambuc.Fo mj_delete 58ebfedea0SLionel Sambuc.Fa "mj_t *atom" 59ebfedea0SLionel Sambuc.Fc 60ebfedea0SLionel Sambuc.Pp 61ebfedea0SLionel SambucAccess to objects and array entries is made using the following functions: 62ebfedea0SLionel Sambuc.Ft int 63ebfedea0SLionel Sambuc.Fo mj_arraycount 64ebfedea0SLionel Sambuc.Fa "mj_t *atom" 65ebfedea0SLionel Sambuc.Fc 66ebfedea0SLionel Sambuc.Ft int 67ebfedea0SLionel Sambuc.Fo mj_object_find 68ebfedea0SLionel Sambuc.Fa "mj_t *atom" "const char *name" "const unsigned startpoint" 69ebfedea0SLionel Sambuc.Fa "const unsigned incr" 70ebfedea0SLionel Sambuc.Fc 71ebfedea0SLionel Sambuc.Ft mj_t * 72ebfedea0SLionel Sambuc.Fo mj_get_atom 73ebfedea0SLionel Sambuc.Fa "mj_t *atom" "..." 74ebfedea0SLionel Sambuc.Fc 75ebfedea0SLionel Sambuc.Pp 76ebfedea0SLionel SambucJSON object output functions: 77ebfedea0SLionel Sambuc.Ft int 78ebfedea0SLionel Sambuc.Fo mj_snprint 79ebfedea0SLionel Sambuc.Fa "char *buffer" "size_t size" "mj_t *atom" 80ebfedea0SLionel Sambuc.Fc 81ebfedea0SLionel Sambuc.Ft int 82ebfedea0SLionel Sambuc.Fo mj_asprint 83ebfedea0SLionel Sambuc.Fa "char **buffer" "mj_t *atom" 84ebfedea0SLionel Sambuc.Fc 85ebfedea0SLionel Sambuc.Ft int 86ebfedea0SLionel Sambuc.Fo mj_string_size 87ebfedea0SLionel Sambuc.Fa "mj_t *atom" 88ebfedea0SLionel Sambuc.Fc 89ebfedea0SLionel Sambuc.Ft int 90ebfedea0SLionel Sambuc.Fo mj_pretty 91ebfedea0SLionel Sambuc.Fa "mj_t *atom" "void *stream" "unsigned depth" "const char *trailer" 92ebfedea0SLionel Sambuc.Fc 93ebfedea0SLionel Sambuc.Ft const char * 94ebfedea0SLionel Sambuc.Fo mj_string_rep 95ebfedea0SLionel Sambuc.Fa "mj_t *atom" 96ebfedea0SLionel Sambuc.Fc 97ebfedea0SLionel Sambuc.Sh DESCRIPTION 98ebfedea0SLionel Sambuc.Nm 99ebfedea0SLionel Sambucis a small library interface to allow JSON text to be created and parsed. 100ebfedea0SLionel SambucJSON is the Java Script Object Notation, 101*0a6a1f1dSLionel Sambuca lightweight data-interchange format, standardised by the ECMA. 102ebfedea0SLionel SambucThe library name 103ebfedea0SLionel Sambuc.Nm 104ebfedea0SLionel Sambucis derived from a further acronym of 105ebfedea0SLionel Sambuc.Dq minimalist JSON . 106ebfedea0SLionel Sambuc.\" Hey, Mary! 107ebfedea0SLionel Sambuc.Pp 108ebfedea0SLionel SambucThe 109ebfedea0SLionel Sambuc.Nm 110ebfedea0SLionel Sambuclibrary can be used to create a string in memory which contains a textual 111ebfedea0SLionel Sambucrepresentation of a number of objects, arbitrarily structured. 112ebfedea0SLionel SambucThe library can also be used to reconstruct the structure. 113ebfedea0SLionel SambucData can thus be serialised easily and efficiently, and data structures 114ebfedea0SLionel Sambucrebuilt to produce the original structure of the data. 115ebfedea0SLionel Sambuc.Pp 116ebfedea0SLionel SambucJSON contains basic units called atoms, the two 117ebfedea0SLionel Sambucbasic atoms being strings and numbers. 118*0a6a1f1dSLionel SambucThree other useful atomic values are provided: 119ebfedea0SLionel Sambuc.Dq null , 120ebfedea0SLionel Sambuc.Dq false , 121ebfedea0SLionel Sambucand 122ebfedea0SLionel Sambuc.Dq true . 123ebfedea0SLionel SambucAtoms can be grouped together as key/value pairs in an 124ebfedea0SLionel Sambuc.Dq object , 125ebfedea0SLionel Sambucand as individual, ordered atoms, in an 126ebfedea0SLionel Sambuc.Dq array . 127ebfedea0SLionel Sambuc.Pp 128ebfedea0SLionel SambucTo create a new object, the 129ebfedea0SLionel Sambuc.Fn mj_create 130*0a6a1f1dSLionel Sambucfunction is used. 131ebfedea0SLionel SambucIt can be deleted using the 132ebfedea0SLionel Sambuc.Fn mj_delete 133ebfedea0SLionel Sambucfunction. 134ebfedea0SLionel Sambuc.Pp 135ebfedea0SLionel SambucAtoms, objects and arrays can be appended 136ebfedea0SLionel Sambucto arrays and objects using the 137ebfedea0SLionel Sambuc.Fn mj_append 138ebfedea0SLionel Sambucfunction. 139ebfedea0SLionel Sambuc.Pp 140ebfedea0SLionel SambucObjects can be printed out 141ebfedea0SLionel Sambucby using the 142ebfedea0SLionel Sambuc.Fn mj_snprint 143ebfedea0SLionel Sambucfunction. 144ebfedea0SLionel SambucThe size of a string of JSON text can be calculated 145ebfedea0SLionel Sambucusing the 146ebfedea0SLionel Sambuc.Fn mj_string_size 147ebfedea0SLionel Sambucfunction. 148ebfedea0SLionel SambucA utility function 149ebfedea0SLionel Sambuc.Fn mj_asprint 150ebfedea0SLionel Sambucis provided which will allocate space dynamically, 151ebfedea0SLionel Sambucusing 152ebfedea0SLionel Sambuc.Xr calloc 3 , 153ebfedea0SLionel Sambucand the JSON serialised text is copied into it. 154ebfedea0SLionel SambucThis memory can later be de-allocated using 155ebfedea0SLionel Sambuc.Xr free 3 . 156ebfedea0SLionel SambucFor formatted output to a 157*0a6a1f1dSLionel Sambuc.Vt FILE * 158ebfedea0SLionel Sambucstream, the 159ebfedea0SLionel Sambuc.Fn mj_pretty 160ebfedea0SLionel Sambucfunction is used. 161ebfedea0SLionel SambucThe calling interface gives the ability to indent the 162ebfedea0SLionel Sambucoutput to a given 163ebfedea0SLionel Sambuc.Fa depth 164ebfedea0SLionel Sambucand for the formatted output to be followed by a 165ebfedea0SLionel Sambuc.Fa trailer 166ebfedea0SLionel Sambucstring, which is usually 167ebfedea0SLionel Sambuc.Dv NULL 168ebfedea0SLionel Sambucfor external calls, but can be any valid string. 169ebfedea0SLionel SambucOutput is sent to the 170ebfedea0SLionel Sambuc.Fa stream 171ebfedea0SLionel Sambucfile stream. 172ebfedea0SLionel Sambuc.Pp 173ebfedea0SLionel SambucThe 174ebfedea0SLionel Sambuc.Fa type 175ebfedea0SLionel Sambucargument given to the 176ebfedea0SLionel Sambuc.Fn mj_create , 177ebfedea0SLionel Sambuc.Fn mj_append , 178ebfedea0SLionel Sambucand 179ebfedea0SLionel Sambuc.Fn mj_append_field 180ebfedea0SLionel Sambucfunctions is taken from a list of 181ebfedea0SLionel Sambuc.Dq false 182ebfedea0SLionel Sambuc.Dq true 183ebfedea0SLionel Sambuc.Dq null 184ebfedea0SLionel Sambuc.Dq number 185ebfedea0SLionel Sambuc.Dq integer 186ebfedea0SLionel Sambuc.Dq string 187ebfedea0SLionel Sambuc.Dq array 188ebfedea0SLionel Sambucand 189ebfedea0SLionel Sambuc.Dq object 190ebfedea0SLionel Sambuctypes. 191ebfedea0SLionel SambucAn integer differs from a number in that it cannot take on 192ebfedea0SLionel Sambucany floating point values. 193ebfedea0SLionel SambucIt is implemented internally using a signed 64-bit integer type. 194ebfedea0SLionel SambucThis restriction of values for an integer type may be removed at a later date. 195ebfedea0SLionel Sambuc.Pp 196ebfedea0SLionel SambucWithin a JSON object, the key values can be iterated over using an integer 197ebfedea0SLionel Sambucindex to access the individual JSON objects. 198ebfedea0SLionel SambucThe index can also be found using the 199ebfedea0SLionel Sambuc.Fn mj_object_find 200ebfedea0SLionel Sambucfunction. 201ebfedea0SLionel Sambuc.Pp 202ebfedea0SLionel SambucThe way objects arrays are implemented in 203ebfedea0SLionel Sambuc.Nm 204ebfedea0SLionel Sambucis by using varying-sized arrays internally. 205ebfedea0SLionel SambucObjects have the field name as the even entry in this internal array, 206ebfedea0SLionel Sambucwith the value being the odd entry. 207ebfedea0SLionel SambucArrays are implemented as a simple array. 208ebfedea0SLionel SambucThus, to find an object in an array using 209ebfedea0SLionel Sambuc.Fn mj_object_find , 210ebfedea0SLionel Sambuca value of 1 should be used as the increment value. 211ebfedea0SLionel SambucThis means that every entry in the internal array will be examined, 212ebfedea0SLionel Sambucand the first match after the starting point will be returned. 213ebfedea0SLionel SambucFor objects, an incremental value of 2 should be used, 214ebfedea0SLionel Sambucand an even start value should be specified. 215ebfedea0SLionel Sambuc.Pp 216ebfedea0SLionel SambucString values should be created and appended using two parameters in 217ebfedea0SLionel Sambucthe stdarg fields, that of the string itself, and its length in bytes 218ebfedea0SLionel Sambucimmediately after the string. 219ebfedea0SLionel SambucA value of 220ebfedea0SLionel Sambuc.Dv \-1 221ebfedea0SLionel Sambucmay be used if the string length is not known. 222ebfedea0SLionel Sambuc.Sh EXAMPLES 223*0a6a1f1dSLionel SambucThe following code fragment will make a JSON object 224ebfedea0SLionel Sambucout of the string 225ebfedea0SLionel Sambuc.Dq Hello <USERNAME>\en 226ebfedea0SLionel Sambucin the 227ebfedea0SLionel Sambucbuffer called 228*0a6a1f1dSLionel Sambuc.Va buf 229ebfedea0SLionel Sambucwhere 230ebfedea0SLionel Sambuc.Dq USERNAME 231ebfedea0SLionel Sambucis the name of the user taken from the runtime environment. 232ebfedea0SLionel SambucThe encoded text will be in an allocated buffer called 233*0a6a1f1dSLionel Sambuc.Va s 234ebfedea0SLionel Sambuc.Bd -literal -offset indent 235ebfedea0SLionel Sambucmj_t atom; 236ebfedea0SLionel Sambucchar buf[BUFSIZ]; 237ebfedea0SLionel Sambucchar *s; 238ebfedea0SLionel Sambucint cc; 239ebfedea0SLionel Sambuc 240ebfedea0SLionel Sambuc(void) memset(\*[Am]atom, 0x0, sizeof(atom)); 241ebfedea0SLionel Sambuccc = snprintf(buf, sizeof(buf), "Hello %s\en", getenv("USER")); 242ebfedea0SLionel Sambucmj_create(\*[Am]atom, "string", buf, cc); 243ebfedea0SLionel Sambuccc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_JSON_ENCODE); 244ebfedea0SLionel Sambuc.Ed 245ebfedea0SLionel Sambuc.Pp 246*0a6a1f1dSLionel SambucNext, the following example will take the (binary) text which has been encoded into 247ebfedea0SLionel SambucJSON and is in the buffer 248*0a6a1f1dSLionel Sambuc.Va buf , 249ebfedea0SLionel Sambucsuch as in the previous example, and re-create the original text: 250ebfedea0SLionel Sambuc.Bd -literal -offset indent 251ebfedea0SLionel Sambucint from, to, tok, cc; 252ebfedea0SLionel Sambucchar *s; 253ebfedea0SLionel Sambucmj_t atom; 254ebfedea0SLionel Sambuc 255ebfedea0SLionel Sambuc(void) memset(\*[Am]atom, 0x0, sizeof(atom)); 256ebfedea0SLionel Sambucfrom = to = tok = 0; 257ebfedea0SLionel Sambucmj_parse(\*[Am]atom, buf, \*[Am]from, \*[Am]to, \*[Am]tok); 258ebfedea0SLionel Sambuccc = mj_asprint(\*[Am]s, \*[Am]atom, MJ_HUMAN); 259ebfedea0SLionel Sambucprintf("%.*s", cc, s); 260ebfedea0SLionel Sambuc.Ed 261ebfedea0SLionel Sambuc.Pp 262ebfedea0SLionel SambucThe 263*0a6a1f1dSLionel Sambuc.Va s 264ebfedea0SLionel Sambucpointer points to allocated storage with the original NUL-terminated string 265ebfedea0SLionel Sambucin it. 266ebfedea0SLionel Sambuc.Sh SEE ALSO 267ebfedea0SLionel Sambuc.Xr calloc 3 , 268ebfedea0SLionel Sambuc.Xr free 3 269ebfedea0SLionel Sambuc.Rs 270ebfedea0SLionel Sambuc.%Q Ecma International 271ebfedea0SLionel Sambuc.%D December 2009 272ebfedea0SLionel Sambuc.%T ECMA-262: ECMAScript Language Specification 273ebfedea0SLionel Sambuc.%U http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf 274ebfedea0SLionel Sambuc.%O 5th Edition 275ebfedea0SLionel Sambuc.Re 276ebfedea0SLionel Sambuc.Sh HISTORY 277ebfedea0SLionel SambucThe 278ebfedea0SLionel Sambuc.Nm 279ebfedea0SLionel Sambuclibrary first appeared in 280ebfedea0SLionel Sambuc.Nx 6.0 . 281ebfedea0SLionel Sambuc.Sh AUTHORS 282ebfedea0SLionel Sambuc.An Alistair Crooks Aq Mt agc@NetBSD.org 283ebfedea0SLionel Sambucwrote this implementation and manual page. 284