shared: add JSON helper functions for NMValueType

This commit is contained in:
Thomas Haller
2019-05-10 10:33:29 +02:00
parent 75703a2425
commit 848a80598e
2 changed files with 52 additions and 0 deletions

View File

@@ -116,6 +116,34 @@ nm_jansson_json_as_string (const json_t *elem,
return 1;
}
/*****************************************************************************/
#ifdef NM_VALUE_TYPE_DEFINE_FUNCTIONS
#include "nm-value-type.h"
static inline gboolean
nm_value_type_from_json (NMValueType value_type,
const json_t *elem,
gpointer out_val)
{
switch (value_type) {
case NM_VALUE_TYPE_BOOL: return (nm_jansson_json_as_bool (elem, out_val) > 0);
case NM_VALUE_TYPE_INT32: return (nm_jansson_json_as_int32 (elem, out_val) > 0);
case NM_VALUE_TYPE_INT: return (nm_jansson_json_as_int (elem, out_val) > 0);
/* warning: this overwrites/leaks the previous value. You better have *out_val
* point to uninitialized memory or NULL. */
case NM_VALUE_TYPE_STRING: return (nm_jansson_json_as_string (elem, out_val) > 0);
case NM_VALUE_TYPE_UNSPEC:
break;
}
nm_assert_not_reached ();
return FALSE;
}
#endif
/*****************************************************************************/
#endif /* WITH_JANSON */
#endif /* __NM_JANSSON_H__ */

View File

@@ -56,4 +56,28 @@ void nm_json_aux_gstr_append_obj_name (GString *gstr,
/*****************************************************************************/
#ifdef NM_VALUE_TYPE_DEFINE_FUNCTIONS
#include "nm-value-type.h"
static inline void
nm_value_type_to_json (NMValueType value_type,
GString *gstr,
gconstpointer p_field)
{
nm_assert (p_field);
nm_assert (gstr);
switch (value_type) {
case NM_VALUE_TYPE_BOOL: nm_json_aux_gstr_append_bool (gstr, *((const bool *) p_field)); return;
case NM_VALUE_TYPE_INT32: nm_json_aux_gstr_append_int64 (gstr, *((const gint32 *) p_field)); return;
case NM_VALUE_TYPE_INT: nm_json_aux_gstr_append_int64 (gstr, *((const int *) p_field)); return;
case NM_VALUE_TYPE_STRING: nm_json_aux_gstr_append_string (gstr, *((const char *const *) p_field)); return;
case NM_VALUE_TYPE_UNSPEC:
break;
}
nm_assert_not_reached ();
}
#endif
/*****************************************************************************/
#endif /* __NM_JSON_AUX_H__ */