/* (c) Copyright, Real-Time Innovations, 2020. All rights reserved. No duplications, whole or partial, manual or electronic, may be made without express written permission. Any such copies, or revisions thereof, must display this notice unaltered. This code contains trade secrets of Real-Time Innovations, Inc. =========================================================================*/ #ifndef rtijson_object_h #define rtijson_object_h #ifndef osapi_type_h #include "osapi/osapi_type.h" #endif #ifndef rtijson_dll_h #include "rtijson/rtijson_dll.h" #endif #include "rtijson/json.h" #ifdef __cplusplus extern "C" { #endif typedef enum RTIJSONObjectKind { RTI_JSON_NONE_OBJECT, RTI_JSON_OBJECT_OBJECT, RTI_JSON_ARRAY_OBJECT, RTI_JSON_INTEGER_OBJECT, RTI_JSON_DOUBLE_OBJECT, RTI_JSON_STRING_OBJECT, RTI_JSON_BOOLEAN_OBJECT, RTI_JSON_NULL_OBJECT } RTIJSONObjectKind; #define RTIJSONObjectKind_toString(kind__) \ (((kind__) == RTI_JSON_NONE_OBJECT) ? "RTI_JSON_NONE" \ : ((kind__) == RTI_JSON_OBJECT_OBJECT) ? "RTI_JSON_OBJECT" \ : ((kind__) == RTI_JSON_ARRAY_OBJECT) ? "RTI_JSON_ARRAY" \ : ((kind__) == RTI_JSON_INTEGER_OBJECT) ? "RTI_JSON_INTEGER" \ : ((kind__) == RTI_JSON_DOUBLE_OBJECT) ? "RTI_JSON_DOUBLE" \ : ((kind__) == RTI_JSON_STRING_OBJECT) ? "RTI_JSON_STRING" \ : ((kind__) == RTI_JSON_BOOLEAN_OBJECT) ? "RTI_JSON_BOOLEAN" \ : ((kind__) == RTI_JSON_NULL_OBJECT) ? "RTI_JSON_NULL" \ : "Unknown") struct RTIJSONObjectImpl { json_value *_json; }; typedef struct RTIJSONObjectImpl RTIJSONObject; #define RTIJSONObject_INITIALIZER {NULL} #define RTIJSON_jsonSettings_INITIALIZER \ { \ 0, /* max_memory */ \ 0, /* settings */ \ NULL, /* mem_alloc */ \ NULL, /* mem_free */ \ NULL, /* user_data */ \ 0, /* value_extra */ \ } extern RTIJSONDllExport RTIJSONObject * RTIJSONObject_new(void); /** * @brief Free a JSON that wasn't modified by the builder. * * This function only deletes the object if it has no parents, although you * should still delete it even if the parent is deleted. * * @param self [inout] The JSON object to free. */ extern RTIJSONDllExport void RTIJSONObject_delete(RTIJSONObject *self); extern RTIJSONDllExport RTIBool RTIJSONObject_initialize(RTIJSONObject *self, json_value *json); extern RTIJSONDllExport void RTIJSONObject_finalize(RTIJSONObject *self); extern RTIJSONDllExport RTIJSONObjectKind RTIJSONObject_getKind(const RTIJSONObject *self); extern RTIJSONDllExport size_t RTIJSONObject_getChildCount(const RTIJSONObject *self); /** * @brief Set a child of a JSON array at a specific index. * * @pre The JSON object must be of kind RTI_JSON_ARRAY_OBJECT. * @pre The index must be less than the number of children in the array. * * @post The child at the specified index will be replaced with the new child * and the old child will be freed. * * @param self [inout] The parent JSON object. * @param child [inout] The child JSON object to set. * @param index [in] The index at which to set the child. * * @return RTI_TRUE on success, or RTI_FALSE on failure. */ extern RTIJSONDllExport RTIBool RTIJSONObject_setChildAt( RTIJSONObject *self, RTIJSONObject *child, size_t index); extern RTIJSONDllExport RTIBool RTIJSONObject_getChildAt( const RTIJSONObject *self, RTIJSONObject *child, size_t index); /** * @brief Set a child of a JSON object by name. * * @pre The JSON object must be of kind RTI_JSON_OBJECT_OBJECT. * @pre The name must exist in the object. * * @post The child with the specified name will be replaced with the new child * and the old child will be freed. * * @param self [inout] The parent JSON object. * @param child [inout] The child JSON object to set. * @param name [in] The name of the child to set. * * @return RTI_TRUE on success, or RTI_FALSE on failure. */ extern RTIJSONDllExport RTIBool RTIJSONObject_setChildByName( RTIJSONObject *self, RTIJSONObject *child, const char *name); extern RTIJSONDllExport RTIBool RTIJSONObject_getChildByName( const RTIJSONObject *self, RTIJSONObject *child, const char *name); extern RTIJSONDllExport const char * RTIJSONObject_getChildName(const RTIJSONObject *self, size_t index); /** * @brief Set the integer value of a JSON object. * * @pre The JSON object must be of kind RTI_JSON_INTEGER_OBJECT. * * @param self [inout] The JSON object. * @param val [in] The integer value to set. * * @return RTI_TRUE on success, or RTI_FALSE on failure. */ extern RTIJSONDllExport RTIBool RTIJSONObject_setIntValue(RTIJSONObject *self, RTI_INT64 val); extern RTIJSONDllExport RTIBool RTIJSONObject_getIntValue(const RTIJSONObject *self, RTI_INT64 *val); extern RTIJSONDllExport RTIBool RTIJSONObject_getDoubleValue( const RTIJSONObject *self, RTI_DOUBLE64 *val); extern RTIJSONDllExport RTIBool RTIJSONObject_getBoolValue(const RTIJSONObject *self, RTIBool *val); extern RTIJSONDllExport RTIBool RTIJSONObject_getStringValue( const RTIJSONObject *self, char *string, size_t maxLength); extern RTIJSONDllExport const char * RTIJSONObject_getStringValueEx(const RTIJSONObject *self); extern RTIJSONDllExport RTIBool RTIJSONObject_equals( const RTIJSONObject *node1, const RTIJSONObject *node2); extern RTIJSONDllExport int RTIJSONObject_openJsonList(char *jsonStr, size_t jsonStrSize); extern RTIJSONDllExport int RTIJSONObject_closeJsonList(char *jsonStr, size_t jsonStrSize); extern RTIJSONDllExport int RTIJSONObject_addJsonElementToStr( char *jsonStr, size_t jsonStrSize, const char *elementName, const char *elementValue, RTIBool prependComma); #ifdef __cplusplus } /* extern "C" */ #endif #endif /* rtijson_object_h */