libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmJson_h
  4. #define cmJson_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc: "JSON reader and writer" kw:[file] }
  9. //
  10. // Limitations:
  11. //
  12. // 1. Accpets two digit hex sequences with
  13. // the \\u escape command. JSON specifies 4 digits.
  14. //
  15. // 2. The scientific notation for real numbers is limited to
  16. // exponent prefixes: e,E,e-,E-. The prefixes e+ and E+ are
  17. // not recognized by cmLex.
  18. //
  19. // Extensions:
  20. //
  21. // 1. Will accept C style identifiers where JSON demands
  22. // quoted strings.
  23. //
  24. // 2. Will accept C style hex notation (0xddd) for integer values.
  25. //
  26. //)
  27. //(
  28. // JSON data type flags
  29. enum
  30. {
  31. kInvalidTId = 0x0000,
  32. kObjectTId = 0x0001, // children are pairs
  33. kPairTId = 0x0002, // children are string : value pairs
  34. kArrayTId = 0x0004, // children may be of any type
  35. kStringTId = 0x0008, // terminal
  36. kNullTId = 0x0040, // terminal
  37. kIntTId = 0x0010, // terminal
  38. kRealTId = 0x0020, // terminal
  39. kTrueTId = 0x0080, // terminal
  40. kFalseTId = 0x0100, // terminal
  41. kMaskTId = 0x01ff,
  42. kOptArgJsFl = 0x0800, // only used by cmJsonVMemberValues()
  43. kTempJsFl = 0x1000, // used internally
  44. kNumericTId = kIntTId | kRealTId | kTrueTId | kFalseTId,
  45. kBoolTId = kTrueTId | kFalseTId
  46. };
  47. enum
  48. {
  49. kOkJsRC,
  50. kMemAllocErrJsRC,
  51. kLexErrJsRC,
  52. kSyntaxErrJsRC,
  53. kFileOpenErrJsRC,
  54. kFileCreateErrJsRC,
  55. kFileReadErrJsRC,
  56. kFileSeekErrJsRC,
  57. kFileCloseErrJsRC,
  58. kInvalidHexEscapeJsRC,
  59. kSerialErrJsRC,
  60. kNodeNotFoundJsRC,
  61. kNodeCannotCvtJsRC,
  62. kCannotRemoveLabelJsRC,
  63. kInvalidNodeTypeJsRC,
  64. kValidateFailJsRC,
  65. kCsvErrJsRC,
  66. kBufTooSmallJsRC
  67. };
  68. typedef unsigned cmJsRC_t;
  69. typedef cmHandle_t cmJsonH_t;
  70. // JSON tree node
  71. typedef struct cmJsonNode_str
  72. {
  73. unsigned typeId; // id of this node
  74. struct cmJsonNode_str* siblingPtr; // next ele in array or member list
  75. struct cmJsonNode_str* ownerPtr; // parent node ptr
  76. union
  77. {
  78. // childPtr usage:
  79. // object: first pair
  80. // array: first element
  81. // pair: string
  82. struct cmJsonNode_str* childPtr;
  83. int intVal; // valid if typeId == kIntTId
  84. double realVal; // valid if typeId == kRealTId
  85. char* stringVal; // valid if typeId == kStringTId
  86. bool boolVal; // valid if typeId == kTrueTId || kFalseTId
  87. } u;
  88. } cmJsonNode_t;
  89. extern cmJsonH_t cmJsonNullHandle;
  90. // Initialize a json parser/tree object
  91. cmJsRC_t cmJsonInitialize( cmJsonH_t* hp, cmCtx_t* ctx );
  92. // Equivalent to cmJsonInitialize() followed by cmJsonParseFile()
  93. cmJsRC_t cmJsonInitializeFromFile( cmJsonH_t* hp, const char* fn, cmCtx_t* ctx );
  94. // Equivalent to cmJsonInitialize() followed by cmJsonParse(h,buf,cnt,NULL).
  95. cmJsRC_t cmJsonInitializeFromBuf( cmJsonH_t* hp, cmCtx_t* ctx, const char* buf, unsigned bufByteCnt );
  96. // Release all the resources held by the tree.
  97. cmJsRC_t cmJsonFinalize( cmJsonH_t* hp );
  98. // Returns true if 'h' is a valid cmJsonH_t handle.
  99. bool cmJsonIsValid( cmJsonH_t h );
  100. // Returns true if the tree has been modified since it was initialized.
  101. // If changes to the tree are done directly on the nodes, rather than using
  102. // the API functions, then this function may not indicate the actual
  103. // modification state of the tree.
  104. bool cmJsonIsModified( cmJsonH_t h );
  105. // Build the internal tree by parsing a text buffer.
  106. // altRootPtr is an optional alternate root ptr which can be used
  107. // append to an existing tree. Set to altRootPtr to
  108. // NULL to append the tree to the internal root.
  109. // If altRootPtr is given it must point ot either an array or
  110. // object node.
  111. cmJsRC_t cmJsonParse( cmJsonH_t h, const char* buf, unsigned bufCharCnt, cmJsonNode_t* altRootPtr );
  112. // Fills a text buffer from a file and calls cmJsonParse().
  113. cmJsRC_t cmJsonParseFile( cmJsonH_t h, const char* fn, cmJsonNode_t* altRootPtr );
  114. // Return the root node of the internal tree.
  115. cmJsonNode_t* cmJsonRoot( cmJsonH_t h );
  116. // Return the tree to the post initialize state by clearing the internal tree.
  117. cmJsRC_t cmJsonClearTree( cmJsonH_t h );
  118. // Node type predicates.
  119. bool cmJsonIsObject( const cmJsonNode_t* np );
  120. bool cmJsonIsArray( const cmJsonNode_t* np );
  121. bool cmJsonIsPair( const cmJsonNode_t* np );
  122. bool cmJsonIsString( const cmJsonNode_t* np );
  123. bool cmJsonIsInt( const cmJsonNode_t* np );
  124. bool cmJsonIsReal( const cmJsonNode_t* np );
  125. bool cmJsonIsBool( const cmJsonNode_t* np );
  126. // Return the count of child nodes of 'np'.
  127. // Note that only object,array, and pair nodes have children.
  128. unsigned cmJsonChildCount( const cmJsonNode_t* np );
  129. // Return the node at 'index' from an element array.
  130. // 'np must point to an array element.
  131. cmJsonNode_t* cmJsonArrayElement( cmJsonNode_t* np, unsigned index );
  132. const cmJsonNode_t* cmJsonArrayElementC( const cmJsonNode_t* np, unsigned index );
  133. // Return the child value node of a pair with a label node equal to 'label'.
  134. // Set 'root' to NULL to begin the search at the internal tree root node.
  135. // Set 'typeIdMask' with all type flags to match.
  136. // If 'typeIdMask' is equal to kInvalidTId then all types will match.
  137. cmJsonNode_t* cmJsonFindValue( cmJsonH_t h, const char* label, const cmJsonNode_t* root, unsigned typeIdMask );
  138. // Return the value node of a pair at the end of an object path.
  139. // 'path' is a '/' seperated list of object names where the final
  140. // object specifies the pair label for the value to return.
  141. const cmJsonNode_t* cmJsonFindPathValueC( cmJsonH_t h, const char* path, const cmJsonNode_t* root, unsigned typeIdMask );
  142. cmJsonNode_t* cmJsonFindPathValue( cmJsonH_t h, const char* path, const cmJsonNode_t* root, unsigned typeIdMask );
  143. // Return the node value. If 'np' does not point to the same type as
  144. // specified in '*retPtr' then the value is converted if possible.
  145. // If the value cannot be converted function returns a 'kNodeCannotCvtJsRC'
  146. // error
  147. cmJsRC_t cmJsonUIntValue( const cmJsonNode_t* np, unsigned* retPtr );
  148. cmJsRC_t cmJsonIntValue( const cmJsonNode_t* np, int* retPtr );
  149. cmJsRC_t cmJsonRealValue( const cmJsonNode_t* np, double* retPtr );
  150. cmJsRC_t cmJsonBoolValue( const cmJsonNode_t* np, bool* retPtr );
  151. cmJsRC_t cmJsonStringValue( const cmJsonNode_t* np, const char ** retPtrPtr );
  152. cmJsRC_t cmJsonPairNode( const cmJsonNode_t* vp, cmJsonNode_t ** retPtrPtr );
  153. cmJsRC_t cmJsonArrayNode( const cmJsonNode_t* vp, cmJsonNode_t ** retPtrPtr );
  154. cmJsRC_t cmJsonObjectNode( const cmJsonNode_t* vp, cmJsonNode_t ** retPtrPtr );
  155. // Return the label from a pair object.
  156. const char* cmJsonPairLabel( const cmJsonNode_t* pairPtr );
  157. unsigned cmJsonPairTypeId( const cmJsonNode_t* pairPtr );
  158. cmJsonNode_t* cmJsonPairValue( cmJsonNode_t* pairPtr );
  159. // Return a labelled pair node from an object.
  160. cmJsonNode_t* cmJsonFindPair( const cmJsonNode_t* objectNodePtr, const char* label );
  161. // Return the type of a member value.
  162. cmJsRC_t cmJsonMemberType( const cmJsonNode_t* objectNodePtr, const char* label, unsigned* typeIdRef );
  163. // Return the pair at the specified index. Use cmJsonChildCount() to get the count of pairs in the object.
  164. cmJsonNode_t* cmJsonMemberAtIndex( cmJsonNode_t* objNodePtr, unsigned idx );
  165. // Return values associated with the member values in the object
  166. // pointed to by object objectNodePtr.
  167. cmJsRC_t cmJsonUIntMember( const cmJsonNode_t* objectNodePtr, const char* label, unsigned* retPtr );
  168. cmJsRC_t cmJsonIntMember( const cmJsonNode_t* objectNodePtr, const char* label, int* retPtr );
  169. cmJsRC_t cmJsonRealMember( const cmJsonNode_t* objectNodePtr, const char* label, double* retPtr );
  170. cmJsRC_t cmJsonBoolMember( const cmJsonNode_t* objectNodePtr, const char* label, bool* retPtr );
  171. cmJsRC_t cmJsonStringMember( const cmJsonNode_t* objectNodePtr, const char* label, const char** retPtrPtr );
  172. // Returns array or object nodes.
  173. cmJsRC_t cmJsonNodeMember( const cmJsonNode_t* objectNodePtr, const char* label, cmJsonNode_t** nodePtrPtr );
  174. // Returns the value of the member pair named by 'label' or NULL if the
  175. // named pair does not exist.
  176. cmJsonNode_t* cmJsonNodeMemberValue( const cmJsonNode_t* np, const char* label );
  177. // Return values for specified pairs from an object node.
  178. //
  179. // The var args syntax is: <label>,<typeId>,<valuePtr>.
  180. // This functionis implemented in terms of cmJsonXXXMember().
  181. //
  182. // Add kOptArgJsFl to <typeId> if the member may not exist in
  183. // the object - otherwise the function will fail with a
  184. // kNodeNotFoundJsRC error and errLabelPtr will have the name of the missing pair.
  185. //
  186. // Terminate the var args list with NULL.
  187. //
  188. // Object,Array, and Pair members are returned as node pointers.
  189. //
  190. // Since kBoolTId does not exist use kTrueTId or kFalseTId to
  191. // return bool values.
  192. cmJsRC_t cmJsonVMemberValues(const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, va_list vl );
  193. cmJsRC_t cmJsonMemberValues( const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, ... );
  194. // If objectNodePtr is set to NULL then the tree root is used as the base for the search.
  195. // pathPrefix may be set to NULL.
  196. cmJsRC_t cmJsonPathToValueNode( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, const cmJsonNode_t** nodePtrPtr );
  197. cmJsRC_t cmJsonPathToBool( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, bool* retValPtr );
  198. cmJsRC_t cmJsonPathToInt( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, int* retValPtr );
  199. cmJsRC_t cmJsonPathToUInt( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, unsigned* retValPtr );
  200. cmJsRC_t cmJsonPathToReal( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, double* retValPtr );
  201. cmJsRC_t cmJsonPathToString( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, const char** retValPtr );
  202. cmJsRC_t cmJsonPathToPair( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, cmJsonNode_t** retValPtr );
  203. cmJsRC_t cmJsonPathToArray( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, cmJsonNode_t** retValPtr );
  204. cmJsRC_t cmJsonPathToObject( cmJsonH_t h, const cmJsonNode_t* objectNodePtr, const char* pathPrefix, const char* path, cmJsonNode_t** retValPtr );
  205. // Same as cmJsonMemberValues() except labels may be paths to a given variable.
  206. // These paths are equivalent to those used in cmJsonFindPathValue()
  207. // If objectNodePtr is NULL then the JSON tree root is used as the
  208. // base reference object.
  209. // If pathPrefix is non-NULL then it is appended to each of the
  210. // individual paths.
  211. cmJsRC_t cmJsonVPathValues(cmJsonH_t h, const char* pathPrefix, const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, va_list vl );
  212. cmJsRC_t cmJsonPathValues( cmJsonH_t h, const char* pathPrefix, const cmJsonNode_t* objectNodePtr, const char** errLabelPtrPtr, ... );
  213. // Set 'typeId' to the type of the new node.
  214. // Use 'intVal' for the value of int nodes.
  215. // Use 'realVal' for the value of real nodes.
  216. // Use 'stringVal' for the label of pairs and the value of string nodes.
  217. // 'retNodePtrPtr' is optional
  218. cmJsRC_t cmJsonCreate( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned typeId, const char* stringVal, int intVal, double realVal, cmJsonNode_t** newNodePtrPtr );
  219. // Insert new nodes in the tree. If the tree is empty then the first
  220. // inserted node will become the root (this must be an object or array node.).
  221. cmJsonNode_t* cmJsonCreateObject( cmJsonH_t h, cmJsonNode_t* parentPtr );
  222. cmJsonNode_t* cmJsonCreateArray( cmJsonH_t h, cmJsonNode_t* parentPtr );
  223. cmJsonNode_t* cmJsonCreatePair( cmJsonH_t h, cmJsonNode_t* parentPtr, const char* label );
  224. cmJsRC_t cmJsonCreateString( cmJsonH_t h, cmJsonNode_t* parentPtr, const char* stringValue );
  225. cmJsRC_t cmJsonCreateStringN(cmJsonH_t h, cmJsonNode_t* parentPtr, const char* stringValue, unsigned stringCharCnt );
  226. cmJsRC_t cmJsonCreateInt( cmJsonH_t h, cmJsonNode_t* parentPtr, int value );
  227. cmJsRC_t cmJsonCreateReal( cmJsonH_t h, cmJsonNode_t* parentPtr, double value );
  228. cmJsRC_t cmJsonCreateBool( cmJsonH_t h, cmJsonNode_t* parentPtr, bool value );
  229. cmJsRC_t cmJsonCreateNull( cmJsonH_t h, cmJsonNode_t* parentPtr );
  230. cmJsRC_t cmJsonCreateStringArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const char** values );
  231. cmJsRC_t cmJsonCreateIntArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const int* values );
  232. cmJsRC_t cmJsonCreateRealArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const double* values );
  233. cmJsRC_t cmJsonCreateBoolArray( cmJsonH_t h, cmJsonNode_t* parentPtr, unsigned n, const bool* values );
  234. //--------------------------------------------------------------------------------------------------------------
  235. //
  236. // Tree creation helper functiosn
  237. //
  238. cmJsRC_t cmJsonSetInt( cmJsonH_t h, cmJsonNode_t* np, int ival );
  239. cmJsRC_t cmJsonSetReal( cmJsonH_t h, cmJsonNode_t * np, double rval );
  240. cmJsRC_t cmJsonSetBool( cmJsonH_t h, cmJsonNode_t * np, bool bval );
  241. cmJsRC_t cmJsonSetString( cmJsonH_t h, cmJsonNode_t* np, const char* sval );
  242. // Insert a pair with a value indicated by 'typeId'.
  243. // 'stringVal','intVal' and 'realVal' are used as in cmJsonCreate().
  244. // Return a pointer to the new pair.
  245. cmJsonNode_t* cmJsonInsertPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned typeId, const char* stringVal, int intVal, double realVal );
  246. // Create a pair node and the associated label and value nodes and insert the pair in a parent object.
  247. // The object,array and pair creation functions return pointers to the pair value node.
  248. // These are helper functions that are implemented in terms of cmJsonCreateXXX() function.
  249. cmJsonNode_t* cmJsonInsertPairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label );
  250. cmJsonNode_t* cmJsonInsertPairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label );
  251. cmJsonNode_t* cmJsonInsertPairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* pairLabel );
  252. cmJsRC_t cmJsonInsertPairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, int intVal );
  253. cmJsRC_t cmJsonInsertPairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, double realVal );
  254. cmJsRC_t cmJsonInsertPairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* string );
  255. cmJsRC_t cmJsonInsertPairStringN(cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, const char* string, unsigned stringCharCnt );
  256. cmJsRC_t cmJsonInsertPairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, bool boolVal );
  257. cmJsRC_t cmJsonInsertPairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label );
  258. cmJsRC_t cmJsonInsertPairIntArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const int* values );
  259. cmJsRC_t cmJsonInsertPairRealArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const double* values );
  260. cmJsRC_t cmJsonInsertPairStringArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const char** values );
  261. cmJsRC_t cmJsonInsertPairBoolArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const bool* values );
  262. // Returns pair pointer
  263. cmJsonNode_t* cmJsonInsertPairIntArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const int* values );
  264. cmJsonNode_t* cmJsonInsertPairRealArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const double* values );
  265. cmJsonNode_t* cmJsonInsertPairStringArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const char** values );
  266. cmJsonNode_t* cmJsonInsertPairBoolArray2( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned n, const bool* values );
  267. // Insert a pair (same as cmJsonInsertPair()) or if a pair
  268. // with a matching label/type already exists then replace the
  269. // existing value with a the new value.
  270. //
  271. // Set matchTypeMask with the type id's of all pair value types
  272. // which sould be considered a match. If matchTypeMask is set to
  273. // kInvalidTId then all value types will match.
  274. //
  275. // Return a pointer to the new or existing pair.
  276. //
  277. // When newTypeId == kObjectTId or kArrayTId then 'nv' may optionally be set to an object or array
  278. // to be set as the new value node for the selected pair. If 'nv' is NULL then an empty array or
  279. // object is created as the pair value node.
  280. //
  281. // When newTypeId == kPairTId then we are inserting/replacing a pair as the value of the selected pair.
  282. // In this case sv must be set to the new pair label. 'nv' may be optionally
  283. // set to a new pair value node. If 'nv' is NULL then the the new pair will have a value of type kNullTId
  284. cmJsonNode_t* cmJsonInsertOrReplacePair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, unsigned newTypeId, const char* sv, int iv, double dv, cmJsonNode_t* nv );
  285. // Returns pointer to object node.
  286. cmJsonNode_t* cmJsonInsertOrReplacePairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newObjNodePtr );
  287. // Returns pointer to array node.
  288. cmJsonNode_t* cmJsonInsertOrReplacePairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newArrayNodePtr );
  289. // Returns pointer to child pair node
  290. cmJsonNode_t* cmJsonInsertOrReplacePairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* newPairLabel, cmJsonNode_t* newPairValNodePtr );
  291. cmJsRC_t cmJsonInsertOrReplacePairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, int intVal );
  292. cmJsRC_t cmJsonInsertOrReplacePairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, double realVal );
  293. cmJsRC_t cmJsonInsertOrReplacePairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* string );
  294. cmJsRC_t cmJsonInsertOrReplacePairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, bool boolVal );
  295. cmJsRC_t cmJsonInsertOrReplacePairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask );
  296. // Same as the above cmJsonInsertOrReplaceXXX() functions except
  297. // the function fails if a matching pair is not found.
  298. //
  299. // Replace a pair with the same name/type and return a pointer to the
  300. // effected pair. If a pair with the same name and type are not
  301. // found then no change is made and the function returns NULL.
  302. // For newTypeId=kObjectTId,kArrayTId,kPairTId the replaced pair node is blank,
  303. // in other words it will have no child nodes.
  304. cmJsonNode_t* cmJsonReplacePair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, unsigned newTypeId, const char* sv, int iv, double dv, cmJsonNode_t* nv );
  305. // Returns pointer to object node.
  306. cmJsonNode_t* cmJsonReplacePairObject( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newObjNodePtr );
  307. // Return pointer to array node.
  308. cmJsonNode_t* cmJsonReplacePairArray( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, cmJsonNode_t* newArrayNodePtr );
  309. // Returns pointer to child pair node.
  310. cmJsonNode_t* cmJsonReplacePairPair( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* newPairLabel, cmJsonNode_t* newPairValueNodePtr );
  311. cmJsRC_t cmJsonReplacePairInt( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, int intVal );
  312. cmJsRC_t cmJsonReplacePairReal( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, double realVal );
  313. cmJsRC_t cmJsonReplacePairString( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, const char* string );
  314. cmJsRC_t cmJsonReplacePairBool( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask, bool boolVal );
  315. cmJsRC_t cmJsonReplacePairNull( cmJsonH_t h, cmJsonNode_t* objectNodePtr, const char* label, unsigned matchTypeMask );
  316. // Insert multiple pairs in a parent object. Terminate the pair sets with NULL.
  317. // Note that pair,int,real,bool, and string pairs are specified with 3 args: <label>,<typeId>,<value>
  318. // all others are specified with 2 args: <label>,<typeId>.
  319. // The last argument in this function must always be NULL.
  320. cmJsRC_t cmJsonVInsertPairs( cmJsonH_t h, cmJsonNode_t* objectNodePtr, va_list vl );
  321. cmJsRC_t cmJsonInsertPairs( cmJsonH_t h, cmJsonNode_t* objectNodePtr, ... );
  322. // Create an object node, fill it with the specified pairs, and return a pointer to
  323. // the new object node.
  324. // This function uses same var args syntax as cmJsonInsertPairs()
  325. cmJsonNode_t* cmJsonVCreateFilledObject( cmJsonH_t h, cmJsonNode_t* parentPtr, va_list vl );
  326. cmJsonNode_t* cmJsonCreateFilledObject( cmJsonH_t h, cmJsonNode_t* parentPtr, ... );
  327. //--------------------------------------------------------------------------------------------------------------
  328. // Remove a node from the tree by unlinking it from its
  329. // parent and siblings.
  330. // Set the freeFl to true if the node memory should also
  331. // be released.
  332. //
  333. // If 'freeFl' is false then np->ownerPtr and
  334. // np->siblingPtr remain valid when the function returns.
  335. // Even with the valid pointer however be careful
  336. // not to use the node in a way that it depends
  337. // on existing in the tree since the parent and
  338. // siblings will no longer know about it.
  339. //
  340. // If np is the root then the internal tree will be
  341. // cleared (i.e. cmJsonRoot(h) == NULL).
  342. //
  343. // This function will fail if 'np' points to the label value
  344. // of a pair node. Pair labels cannot be removed because this
  345. // would result in an invalid tree (all pairs must have two
  346. // child nodes where the first node is a string value). If 'np'
  347. // points to a pair value node then the value node is replaced
  348. // with a null node to maintain a valid pair structure.
  349. cmJsRC_t cmJsonRemoveNode( cmJsonH_t h, cmJsonNode_t* np, bool freeFl );
  350. //--------------------------------------------------------------------------------------------------------------
  351. // Duplicate the subtree pointed to by 'np' and attach it as a child
  352. // of the node pointed to by 'parentPtr'. This function performs a
  353. // deep copy of the subtree pointed to by np and returns the pointer
  354. // to the duplicated subtree.
  355. //
  356. // 'parentPtr' must be a legal parent for the sub-tree or NULL to not
  357. // attach the duplicate tree to any parent.
  358. //
  359. // The returned value is a pointer to the new subtree.
  360. //
  361. // If an error occurs the return value is NULL and cmJsonErrorCode()
  362. // can be used to obtain the code associated with the error.
  363. cmJsonNode_t* cmJsonDuplicateNode( cmJsonH_t h, const cmJsonNode_t* np, cmJsonNode_t* parentPtr );
  364. //--------------------------------------------------------------------------------------------------------------
  365. // Copy any pairs not found in the destintaion object from the
  366. // source object.
  367. // If an error occurs during merging the destination object is
  368. // returned unmodified. The most likely cause of an error is a
  369. // destination pair with the same name but different type
  370. // than a source pair.
  371. cmJsRC_t cmJsonMergeObjectNodes( cmJsonH_t h, cmJsonNode_t* destObjNodePtr, const cmJsonNode_t* srcObjNodePtr );
  372. //--------------------------------------------------------------------------------------------------------------
  373. // Validate the tree.
  374. cmJsRC_t cmJsonValidateTree( cmJsonH_t h );
  375. // Validate the tree beginning with np. Note that this function does
  376. // not print an error on failure but simply returns kValidateFailJsRC.
  377. cmJsRC_t cmJsonValidate( const cmJsonNode_t* np );
  378. // Get the count of bytes required to serialize the tree rooted at 'np'.
  379. unsigned cmJsonSerialByteCount( const cmJsonNode_t* np );
  380. // Serialize the tree rooted at 'np' into the buffer buf[bufByteCnt].
  381. cmJsRC_t cmJsonSerialize( const cmJsonNode_t* np, void* buf, unsigned bufByteCnt );
  382. // Serialize the subtree indicated by 'np' or the entire tree
  383. // if 'np' is NULL. The buffer created by this call will exist
  384. // for the life of 'h' or until the next call to cmJsonSerialize().
  385. // This function is implemented in terms of cmJsonSerialByteCount()
  386. // and cmJsonSerializeTree().
  387. cmJsRC_t cmJsonSerializeTree( cmJsonH_t h, const cmJsonNode_t* np, void** bufPtrPtr, unsigned* bufByteCntPtr);
  388. // Recreate the objects previously serialzed via cmJsonSerialize().
  389. // The tree held in the buffer will be reconstructed as a child of
  390. // altRootPtr (if it is non-NULL) or the internal root.
  391. // If altRootPtr is given then it must point to an array
  392. // or object node.
  393. cmJsRC_t cmJsonDeserialize( cmJsonH_t h, const void* bufPtr, cmJsonNode_t* altRootPtr );
  394. // Return a string/int/real/null/bool node as a string value.
  395. cmJsRC_t cmJsonLeafToString( const cmJsonNode_t* np, cmChar_t* buf, unsigned bufCharCnt );
  396. // Given a CSV file convert it to an array of JSON objects.
  397. // The first line of the CSV file must contain a comma seperated lists of types.
  398. // The type labels must be from the set: 'int','real','string','true','false'.
  399. // Note that either 'true' or 'false' can be use for boolean columns.
  400. // The seocnd line contains the field names as comma separated quoted strings.
  401. // For example "column1","column2","column3"
  402. // The data is presented as comma separated fields.
  403. // If parentNodePtr is NULL then the array will be created unattached to
  404. // the tree.
  405. // if arrayNodePtrPtr is non-NULL then the array node ptr will be returned.
  406. cmJsRC_t cmJsonFromCSV( cmJsonH_t h, const char* iFn, cmJsonNode_t* parentPtr, cmJsonNode_t** arrayNodePtrPtr );
  407. // Write a CSV file from an array of objects.
  408. // arrayNodePtr must point to an array of objects.
  409. cmJsRC_t cmJsonToCSV( cmJsonH_t h, const char* oFn, const cmJsonNode_t* arrayNodePtr );
  410. // Print the subtree using 'np as the root.
  411. void cmJsonPrintTree( const cmJsonNode_t* np, cmRpt_t* rpt );
  412. // Print the subtree using 'np' as the root to a file.
  413. // 'np' is optional and defaults to cmJsonRoot().
  414. cmJsRC_t cmJsonWrite( cmJsonH_t h, const cmJsonNode_t* np, const cmChar_t* fn );
  415. // Return the code of the last error generated. This is useful for the
  416. // the cmJsonCreateXXX() functions which do not return error codes but
  417. // may still fail.
  418. cmJsRC_t cmJsonErrorCode( cmJsonH_t h );
  419. void cmJsonClearErrorCode( cmJsonH_t h );
  420. // Validate the tree and print all the nodes.
  421. cmJsRC_t cmJsonReport( cmJsonH_t h );
  422. // Testing stub.
  423. cmJsRC_t cmJsonTest( const char* fn, cmCtx_t* ctx );
  424. //)
  425. #ifdef __cplusplus
  426. }
  427. #endif
  428. #endif