1.\" $NetBSD: libmj.3,v 1.3 2010/09/08 22:17:27 wiz Exp $ 2.\" 3.\" Copyright (c) 2010 Alistair Crooks <agc@NetBSD.org> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.Dd August 5, 2010 27.Dt LIBMJ 3 28.Os 29.Sh NAME 30.Nm libmj 31.Nd minimalist JSON lightweight data interchange library 32.Sh LIBRARY 33.Lb libmj 34.Sh SYNOPSIS 35.In mj.h 36.Ft int 37.Fo mj_create 38.Fa "mj_t *atom" "const char *text" "..." 39.Fc 40.Ft int 41.Fo mj_parse 42.Fa "mj_t *atom" "const char *text" "int *tokfrom" "int *tokto" "int *toktype" 43.Fc 44.Ft int 45.Fo mj_append 46.Fa "mj_t *atom" "const char *text" "..." 47.Fc 48.Ft int 49.Fo mj_append_field 50.Fa "mj_t *atom" "const char *fieldname" "const char *text" "..." 51.Fc 52.Ft int 53.Fo mj_deepcopy 54.Fa "mj_t *dest" "mj_t *src" 55.Fc 56.Ft void 57.Fo mj_delete 58.Fa "mj_t *atom" 59.Fc 60.Pp 61Access to objects and array entries is made using the following functions: 62.Ft int 63.Fo mj_arraycount 64.Fa "mj_t *atom" 65.Fc 66.Ft int 67.Fo mj_object_find 68.Fa "mj_t *atom" "const char *name" "const unsigned startpoint" 69.Fa "const unsigned incr" 70.Fc 71.Ft mj_t * 72.Fo mj_get_atom 73.Fa "mj_t *atom" "..." 74.Fc 75.Pp 76JSON object output functions: 77.Ft int 78.Fo mj_snprint 79.Fa "char *buffer" "size_t size" "mj_t *atom" 80.Fc 81.Ft int 82.Fo mj_asprint 83.Fa "char **buffer" "mj_t *atom" 84.Fc 85.Ft int 86.Fo mj_string_size 87.Fa "mj_t *atom" 88.Fc 89.Ft int 90.Fo mj_pretty 91.Fa "mj_t *atom" "void *stream" "unsigned depth" "const char *trailer" 92.Fc 93.Sh DESCRIPTION 94.Nm 95is a small library interface to allow JSON text to be created and parsed. 96JSON is the Java Script Object Notation, 97a lightweight data-interchange format, standardised in the ECMA standard. 98The library name 99.Nm 100is derived from a further acronym of 101.Dq minimalist JSON . 102.\" Hey, Mary! 103.Pp 104The 105.Nm 106library can be used to create a string in memory which contains a textual 107representation of a number of objects, arbitrarily structured. 108The library can also be used to reconstruct the structure. 109Data can thus be serialised easily and efficiently, and data structures 110rebuilt to produce the original structure of the data. 111.Pp 112JSON contains basic units called atoms, the two 113basic atoms being strings and numbers. 114Three other useful atomic values are provided, 115.Dq null , 116.Dq false , 117and 118.Dq true . 119Atoms can be grouped together as key/value pairs in an 120.Dq object , 121and as individual, ordered atoms, in an 122.Dq array . 123.Pp 124To create a new object, the 125.Fn mj_create 126is used. 127It can be deleted using the 128.Fn mj_delete 129function. 130.Pp 131Atoms, objects and arrays can be appended 132to arrays and objects using the 133.Fn mj_append 134function. 135.Pp 136Objects can be printed out 137by using the 138.Fn mj_snprint 139function. 140The size of a string of JSON text can be calculated 141using the 142.Fn mj_string_size 143function. 144A utility function 145.Fn mj_asprint 146is provided which will allocate space dynamically, 147using 148.Xr calloc 3 , 149and the JSON serialised text is copied into it. 150This memory can later be de-allocated using 151.Xr free 3 . 152For formatted output to a 153.Dv FILE * 154stream, the 155.Fn mj_pretty 156function is used. 157The calling interface gives the ability to indent the 158output to a given 159.Fa depth 160and for the formatted output to be followed by a 161.Fa trailer 162string, which is usually 163.Dv NULL 164for external calls, but can be any valid string. 165Output is sent to the 166.Fa stream 167file stream. 168.Pp 169The 170.Fa type 171argument given to the 172.Fn mj_create , 173.Fn mj_append , 174and 175.Fn mj_append_field 176functions is taken from a list of 177.Dq false 178.Dq true 179.Dq null 180.Dq number 181.Dq integer 182.Dq string 183.Dq array 184and 185.Dq object 186types. 187An integer differs from a number in that it cannot take on 188any floating point values. 189It is implemented internally using a signed 64-bit integer type. 190This restriction of values for an integer type may be removed at a later date. 191.Pp 192Within a JSON object, the key values can be iterated over using an integer 193index to access the individual JSON objects. 194The index can also be found using the 195.Fn mj_object_find 196function. 197.Pp 198The way objects arrays are implemented in 199.Nm 200is by using varying-sized arrays internally. 201Objects have the field name as the even entry in this internal array, 202with the value being the odd entry. 203Arrays are implemented as a simple array. 204Thus, to find an object in an array using 205.Fn mj_object_find , 206a value of 1 should be used as the increment value. 207This means that every entry in the internal array will be examined, 208and the first match after the starting point will be returned. 209For objects, an incremental value of 2 should be used, 210and an even start value should be specified. 211.Sh SEE ALSO 212.Xr calloc 3 , 213.Xr free 3 214.Rs 215.%Q Ecma International 216.%D December 2009 217.%T ECMA-262: ECMAScript Language Specification 218.%U http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf 219.%O 5th Edition 220.Re 221.Sh HISTORY 222The 223.Nm 224library first appeared in 225.Nx 6.0 . 226.Sh AUTHORS 227.An Alistair Crooks Aq agc@NetBSD.org 228wrote this implementation and manual page. 229