ubjson.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 RenĂ© Kijewski <rene.kijewski@fu-berlin.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
31 #ifndef UBJSON_H
32 #define UBJSON_H
33 
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 
39 #include <sys/types.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /* ***************************************************************************
46  * READ FUNCTIONS / DEFINITIONS
47  *************************************************************************** */
48 
55 typedef enum {
62 
69 
76 
84 
92 
99 
106 
113 
114  /* NOTE: High-precision numbers are not implemented, yet. Implement if needed. */
115  /* UBJSON_TYPE_HP_NUMBER, */
116 
124 
131 
138 
150 
162 } ubjson_type_t;
163 
167 typedef enum {
173 
182 typedef enum {
189 
190 struct ubjson_cookie;
191 
198 
207 typedef ssize_t (*ubjson_read_t)(ubjson_cookie_t *__restrict cookie, void *buf, size_t max_len);
208 
224  ubjson_type_t type1, ssize_t content1,
225  ubjson_type_t type2, ssize_t content2);
226 
239 typedef ssize_t (*ubjson_write_t)(ubjson_cookie_t *__restrict cookie, const void *buf, size_t len);
240 
249  union {
252  } rw;
253 
258  union {
260  } callback;
266  char marker;
267 };
268 
278 ubjson_read_callback_result_t ubjson_read_next(ubjson_cookie_t *__restrict cookie);
279 
296 static inline ubjson_read_callback_result_t ubjson_read(ubjson_cookie_t *__restrict cookie,
299 {
300  cookie->rw.read = read;
301  cookie->callback.read = callback;
302  cookie->marker = 0;
303  return ubjson_read_next(cookie);
304 }
305 
314 ubjson_read_callback_result_t ubjson_peek_value(ubjson_cookie_t *__restrict cookie,
315  ubjson_type_t *type, ssize_t *content);
316 
325 ssize_t ubjson_get_i32(ubjson_cookie_t *__restrict cookie, ssize_t content, int32_t *dest);
326 
334 ssize_t ubjson_get_i64(ubjson_cookie_t *__restrict cookie, ssize_t content, int64_t *dest);
335 
345 ssize_t ubjson_get_string(ubjson_cookie_t *__restrict cookie, ssize_t content, void *dest);
346 
355 static inline ssize_t ubjson_get_bool(ubjson_cookie_t *__restrict cookie, ssize_t content, bool *dest)
356 {
357  (void) cookie;
358  *dest = content;
359  return 1;
360 }
361 
369 static inline ssize_t ubjson_get_float(ubjson_cookie_t *__restrict cookie, ssize_t content, float *dest)
370 {
371  (void) content;
372  union {
373  float f;
374  int32_t i;
375  } value;
376  ubjson_read_callback_result_t result = ubjson_get_i32(cookie, UBJSON_INT32_INT32, &value.i);
377  *dest = value.f;
378  return result;
379 }
380 
388 static inline ssize_t ubjson_get_double(ubjson_cookie_t *__restrict cookie, ssize_t content, double *dest)
389 {
390  (void) content;
391  union {
392  double f;
393  int64_t i;
394  } value;
395  ubjson_read_callback_result_t result = ubjson_get_i64(cookie, -1, &value.i);
396  *dest = value.f;
397  return result;
398 }
399 
410 ubjson_read_callback_result_t ubjson_read_array(ubjson_cookie_t *__restrict cookie);
411 
412 
424 ubjson_read_callback_result_t ubjson_read_object(ubjson_cookie_t *__restrict cookie);
425 
426 /* ***************************************************************************
427  * WRITE FUNCTIONS / DEFINITIONS
428  *************************************************************************** */
429 
439 static inline void ubjson_write_init(ubjson_cookie_t *__restrict cookie, ubjson_write_t write_fun)
440 {
441  cookie->rw.write = write_fun;
442 }
443 
448 ssize_t ubjson_write_null(ubjson_cookie_t *__restrict cookie);
449 
455 ssize_t ubjson_write_noop(ubjson_cookie_t *__restrict cookie);
456 
463 ssize_t ubjson_write_bool(ubjson_cookie_t *__restrict cookie, bool value);
464 
471 ssize_t ubjson_write_i32(ubjson_cookie_t *__restrict cookie, int32_t value);
472 
480 ssize_t ubjson_write_i64(ubjson_cookie_t *__restrict cookie, int64_t value);
481 
488 ssize_t ubjson_write_float(ubjson_cookie_t *__restrict cookie, float value);
489 
496 ssize_t ubjson_write_double(ubjson_cookie_t *__restrict cookie, double value);
497 
505 ssize_t ubjson_write_string(ubjson_cookie_t *__restrict cookie, const void *value, size_t len);
506 
514 ssize_t ubjson_open_array(ubjson_cookie_t *__restrict cookie);
515 
523 ssize_t ubjson_open_array_len(ubjson_cookie_t *__restrict cookie, size_t len);
524 
530 ssize_t ubjson_close_array(ubjson_cookie_t *__restrict cookie);
531 
542 ssize_t ubjson_open_object(ubjson_cookie_t *__restrict cookie);
543 
554 ssize_t ubjson_open_object_len(ubjson_cookie_t *__restrict cookie, size_t len);
555 
567 ssize_t ubjson_write_key(ubjson_cookie_t *__restrict cookie, const void *value, size_t len);
568 
574 ssize_t ubjson_close_object(ubjson_cookie_t *__restrict cookie);
575 
576 #ifdef __cplusplus
577 }
578 #endif
579 
580 #endif /* UBJSON_H */
581 
success / do continue
Definition: ubjson.h:183
The next datum is an array index.
Definition: ubjson.h:149
ubjson_read_callback_result_t
Return value of ubjson_read_callback_t and ubjson_read()
Definition: ubjson.h:182
ssize_t ubjson_open_array(ubjson_cookie_t *__restrict cookie)
Open an array.
The next datum is an array.
Definition: ubjson.h:130
ssize_t ubjson_write_double(ubjson_cookie_t *__restrict cookie, double value)
Write a floating point value.
ssize_t ubjson_write_bool(ubjson_cookie_t *__restrict cookie, bool value)
Write a boolean value.
static ubjson_read_callback_result_t ubjson_read(ubjson_cookie_t *__restrict cookie, ubjson_read_t read, ubjson_read_callback_t callback)
The entry function to read UBJSON serialized data.
Definition: ubjson.h:296
the length of a field exceeded SSIZE_MAX
Definition: ubjson.h:187
The next datum is a boolean.
Definition: ubjson.h:83
The next datum is a string (blob).
Definition: ubjson.h:123
ssize_t(* ubjson_write_t)(ubjson_cookie_t *__restrict cookie, const void *buf, size_t len)
Method called by ubjson_write_null() and friends.
Definition: ubjson.h:239
ubjson_read_callback_result_t(* ubjson_read_callback_t)(ubjson_cookie_t *__restrict cookie, ubjson_type_t type1, ssize_t content1, ubjson_type_t type2, ssize_t content2)
Method called by ubjson_read() to denote the next element in the structure.
Definition: ubjson.h:223
static void ubjson_write_init(ubjson_cookie_t *__restrict cookie, ubjson_write_t write_fun)
The first call when you serialize data to UBJSON.
Definition: ubjson.h:439
ssize_t ubjson_close_array(ubjson_cookie_t *__restrict cookie)
Close an array that was opened with ubjson_open_array().
ssize_t ubjson_close_object(ubjson_cookie_t *__restrict cookie)
Close an array that was opened with ubjson_open_object().
ssize_t ubjson_write_i32(ubjson_cookie_t *__restrict cookie, int32_t value)
Write an integer value.
The stream contains an int16_t.
Definition: ubjson.h:170
ssize_t(* ubjson_read_t)(ubjson_cookie_t *__restrict cookie, void *buf, size_t max_len)
Method called by ubjson_read() to get more data.
Definition: ubjson.h:207
ssize_t ubjson_write_string(ubjson_cookie_t *__restrict cookie, const void *value, size_t len)
Write a string or blob.
ssize_t ubjson_get_i32(ubjson_cookie_t *__restrict cookie, ssize_t content, int32_t *dest)
Call if type1 of the callback was UBJSON_TYPE_INT32.
ssize_t ubjson_write_noop(ubjson_cookie_t *__restrict cookie)
Write a no-operation value.
ssize_t ubjson_open_array_len(ubjson_cookie_t *__restrict cookie, size_t len)
Open an array with a known length.
ssize_t ubjson_get_i64(ubjson_cookie_t *__restrict cookie, ssize_t content, int64_t *dest)
Call if type1 of the callback was UBJSON_TYPE_INT64.
The next datum is a null value.
Definition: ubjson.h:68
static ssize_t ubjson_get_double(ubjson_cookie_t *__restrict cookie, ssize_t content, double *dest)
Call if type1 of the callback was UBJSON_TYPE_DOUBLE.
Definition: ubjson.h:388
The next datum is an integer that fits into an int32_t.
Definition: ubjson.h:91
ubjson_read_callback_result_t ubjson_read_object(ubjson_cookie_t *__restrict cookie)
Call if type1 of the callback was UBJSON_ENTER_OBJECT.
signed int ssize_t
Used for a count of bytes or an error indication.
Definition: msp430_types.h:89
ubjson_read_callback_result_t ubjson_peek_value(ubjson_cookie_t *__restrict cookie, ubjson_type_t *type, ssize_t *content)
Use in a callback if type1 is UBJSON_KEY or UBJSON_INDEX.
ssize_t ubjson_open_object_len(ubjson_cookie_t *__restrict cookie, size_t len)
Open an object with a known length.
ssize_t ubjson_write_key(ubjson_cookie_t *__restrict cookie, const void *value, size_t len)
Write a key inside an object.
ubjson_read_callback_result_t ubjson_read_array(ubjson_cookie_t *__restrict cookie)
Call if type1 of the callback was UBJSON_ENTER_ARRAY.
The next datum is an integer that fits into an int64_t.
Definition: ubjson.h:98
The next datum is an object.
Definition: ubjson.h:137
invalid marker or type
Definition: ubjson.h:185
static ssize_t ubjson_get_float(ubjson_cookie_t *__restrict cookie, ssize_t content, float *dest)
Call if type1 of the callback was UBJSON_TYPE_FLOAT.
Definition: ubjson.h:369
The stream contains an int8_t.
Definition: ubjson.h:168
ubjson_int32_type_t
Length of the UBJSON_TYPE_INT32 datum.
Definition: ubjson.h:167
The next datum is a 64 bit floating point value.
Definition: ubjson.h:112
This file is a modification of original sys/types.h.
The stream contains an uint8_t.
Definition: ubjson.h:169
static ssize_t ubjson_get_bool(ubjson_cookie_t *__restrict cookie, ssize_t content, bool *dest)
Call if type1 of the callback was UBJSON_TYPE_BOOL.
Definition: ubjson.h:355
ssize_t ubjson_get_string(ubjson_cookie_t *__restrict cookie, ssize_t content, void *dest)
Call if type1 of the callback was UBJSON_TYPE_STRING.
ubjson_read_callback_result_t ubjson_read_next(ubjson_cookie_t *__restrict cookie)
Used to read with a setup cookie.
There is no such value.
Definition: ubjson.h:61
ssize_t ubjson_write_i64(ubjson_cookie_t *__restrict cookie, int64_t value)
Write an integer value.
ssize_t ubjson_open_object(ubjson_cookie_t *__restrict cookie)
Open an object.
The stream contains an int32_t.
Definition: ubjson.h:171
stdlib.h wrapper for MSP430
The next datum is a 32 bit floating point value.
Definition: ubjson.h:105
aborted / do abort
Definition: ubjson.h:184
ubjson_type_t
Status code of ubjson_read(), ubjson_read_array() and ubjson_read_object() callback.
Definition: ubjson.h:55
the stream abruptly ended
Definition: ubjson.h:186
ssize_t ubjson_write_float(ubjson_cookie_t *__restrict cookie, float value)
Write a floating point value.
ssize_t ubjson_write_null(ubjson_cookie_t *__restrict cookie)
Write a null value.
The next datum is an object key.
Definition: ubjson.h:161
The next datum is a no-op value.
Definition: ubjson.h:75