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 #if defined(MODULE_MSP430_COMMON)
40 # include "msp430_types.h"
41 #elif !defined(__linux__)
42 # include <sys/types.h>
43 #endif
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /* ***************************************************************************
50  * READ FUNCTIONS / DEFINITIONS
51  *************************************************************************** */
52 
59 typedef enum {
66 
73 
80 
88 
96 
103 
110 
117 
118  /* NOTE: High-precision numbers are not implemented, yet. Implement if needed. */
119  /* UBJSON_TYPE_HP_NUMBER, */
120 
128 
135 
142 
154 
166 } ubjson_type_t;
167 
171 typedef enum {
177 
186 typedef enum {
193 
194 struct ubjson_cookie;
195 
202 
211 typedef ssize_t (*ubjson_read_t)(ubjson_cookie_t *__restrict cookie, void *buf, size_t max_len);
212 
228  ubjson_type_t type1, ssize_t content1,
229  ubjson_type_t type2, ssize_t content2);
230 
243 typedef ssize_t (*ubjson_write_t)(ubjson_cookie_t *__restrict cookie, const void *buf, size_t len);
244 
253  union {
256  } rw;
257 
262  union {
264  } callback;
270  char marker;
271 };
272 
282 ubjson_read_callback_result_t ubjson_read_next(ubjson_cookie_t *__restrict cookie);
283 
300 static inline ubjson_read_callback_result_t ubjson_read(ubjson_cookie_t *__restrict cookie,
303 {
304  cookie->rw.read = read;
305  cookie->callback.read = callback;
306  cookie->marker = 0;
307  return ubjson_read_next(cookie);
308 }
309 
318 ubjson_read_callback_result_t ubjson_peek_value(ubjson_cookie_t *__restrict cookie,
319  ubjson_type_t *type, ssize_t *content);
320 
329 ssize_t ubjson_get_i32(ubjson_cookie_t *__restrict cookie, ssize_t content, int32_t *dest);
330 
338 ssize_t ubjson_get_i64(ubjson_cookie_t *__restrict cookie, ssize_t content, int64_t *dest);
339 
349 ssize_t ubjson_get_string(ubjson_cookie_t *__restrict cookie, ssize_t content, void *dest);
350 
359 static inline ssize_t ubjson_get_bool(ubjson_cookie_t *__restrict cookie, ssize_t content, bool *dest)
360 {
361  (void) cookie;
362  *dest = content;
363  return 1;
364 }
365 
373 static inline ssize_t ubjson_get_float(ubjson_cookie_t *__restrict cookie, ssize_t content, float *dest)
374 {
375  (void) content;
376  union {
377  float f;
378  int32_t i;
379  } value;
380  ubjson_read_callback_result_t result = ubjson_get_i32(cookie, UBJSON_INT32_INT32, &value.i);
381  *dest = value.f;
382  return result;
383 }
384 
392 static inline ssize_t ubjson_get_double(ubjson_cookie_t *__restrict cookie, ssize_t content, double *dest)
393 {
394  (void) content;
395  union {
396  double f;
397  int64_t i;
398  } value;
399  ubjson_read_callback_result_t result = ubjson_get_i64(cookie, -1, &value.i);
400  *dest = value.f;
401  return result;
402 }
403 
414 ubjson_read_callback_result_t ubjson_read_array(ubjson_cookie_t *__restrict cookie);
415 
416 
428 ubjson_read_callback_result_t ubjson_read_object(ubjson_cookie_t *__restrict cookie);
429 
430 /* ***************************************************************************
431  * WRITE FUNCTIONS / DEFINITIONS
432  *************************************************************************** */
433 
443 static inline void ubjson_write_init(ubjson_cookie_t *__restrict cookie, ubjson_write_t write_fun)
444 {
445  cookie->rw.write = write_fun;
446 }
447 
452 ssize_t ubjson_write_null(ubjson_cookie_t *__restrict cookie);
453 
459 ssize_t ubjson_write_noop(ubjson_cookie_t *__restrict cookie);
460 
467 ssize_t ubjson_write_bool(ubjson_cookie_t *__restrict cookie, bool value);
468 
475 ssize_t ubjson_write_i32(ubjson_cookie_t *__restrict cookie, int32_t value);
476 
484 ssize_t ubjson_write_i64(ubjson_cookie_t *__restrict cookie, int64_t value);
485 
492 ssize_t ubjson_write_float(ubjson_cookie_t *__restrict cookie, float value);
493 
500 ssize_t ubjson_write_double(ubjson_cookie_t *__restrict cookie, double value);
501 
509 ssize_t ubjson_write_string(ubjson_cookie_t *__restrict cookie, const void *value, size_t len);
510 
518 ssize_t ubjson_open_array(ubjson_cookie_t *__restrict cookie);
519 
527 ssize_t ubjson_open_array_len(ubjson_cookie_t *__restrict cookie, size_t len);
528 
534 ssize_t ubjson_close_array(ubjson_cookie_t *__restrict cookie);
535 
546 ssize_t ubjson_open_object(ubjson_cookie_t *__restrict cookie);
547 
558 ssize_t ubjson_open_object_len(ubjson_cookie_t *__restrict cookie, size_t len);
559 
571 ssize_t ubjson_write_key(ubjson_cookie_t *__restrict cookie, const void *value, size_t len);
572 
578 ssize_t ubjson_close_object(ubjson_cookie_t *__restrict cookie);
579 
580 #ifdef __cplusplus
581 }
582 #endif
583 
584 #endif /* UBJSON_H */
585 
success / do continue
Definition: ubjson.h:187
The next datum is an array index.
Definition: ubjson.h:153
ubjson_read_callback_result_t
Return value of ubjson_read_callback_t and ubjson_read()
Definition: ubjson.h:186
ssize_t ubjson_open_array(ubjson_cookie_t *__restrict cookie)
Open an array.
The next datum is an array.
Definition: ubjson.h:134
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:300
the length of a field exceeded SSIZE_MAX
Definition: ubjson.h:191
The next datum is a boolean.
Definition: ubjson.h:87
The next datum is a string (blob).
Definition: ubjson.h:127
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:243
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:227
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:443
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:174
basic types for MSP430
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:211
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:72
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:392
The next datum is an integer that fits into an int32_t.
Definition: ubjson.h:95
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:102
The next datum is an object.
Definition: ubjson.h:141
invalid marker or type
Definition: ubjson.h:189
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:373
The stream contains an int8_t.
Definition: ubjson.h:172
ubjson_int32_type_t
Length of the UBJSON_TYPE_INT32 datum.
Definition: ubjson.h:171
The next datum is a 64 bit floating point value.
Definition: ubjson.h:116
The stream contains an uint8_t.
Definition: ubjson.h:173
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:359
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:65
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:175
stdlib.h wrapper for MSP430
The next datum is a 32 bit floating point value.
Definition: ubjson.h:109
aborted / do abort
Definition: ubjson.h:188
ubjson_type_t
Status code of ubjson_read(), ubjson_read_array() and ubjson_read_object() callback.
Definition: ubjson.h:59
the stream abruptly ended
Definition: ubjson.h:190
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:165
The next datum is a no-op value.
Definition: ubjson.h:79