Grassroots Infrastructure
The Grassroots Infrastructure is a suite of computing tools to help users and developers use scientific data infrastructure that can easily be interconnected.
json_util.h
Go to the documentation of this file.
1 /*
2 ** Copyright 2014-2016 The Earlham Institute
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
21 #ifndef JSON_UTIL_H
22 #define JSON_UTIL_H
23 
24 
25 #include <stdio.h>
26 
28 
29 #include "jansson.h"
30 #include "linked_list.h"
31 #include "string_linked_list.h"
32 #include "schema_keys.h"
33 
34 
43 typedef struct JsonNode
44 {
49 
51  json_t *jn_json_p;
52 } JsonNode;
53 
54 
71 typedef struct FieldNode
72 {
75 
77  json_type fn_type;
78 } FieldNode;
79 
80 
81 
82 #ifdef __cplusplus
83 extern "C"
84 {
85 #endif
86 
87 
98 GRASSROOTS_UTIL_API int PrintJSON (FILE *out_f, const json_t * const json_p, const char * const prefix_s);
99 
100 
111 GRASSROOTS_UTIL_API const char *GetJSONString (const json_t *json_p, const char * const key_s);
112 
113 
114 
115 GRASSROOTS_UTIL_API bool SetJSONString (json_t *json_p, const char * const key_s, const char * const value_s);
116 
117 GRASSROOTS_UTIL_API bool SetJSONStringOrNull (json_t *json_p, const char * const key_s, const char * const value_s, const bool null_flag);
118 
129 GRASSROOTS_UTIL_API char *GetCopiedJSONString (const json_t *json_p, const char * const key_s);
130 
131 
142 GRASSROOTS_UTIL_API bool GetJSONInteger (const json_t *json_p, const char * const key_s, json_int_t *value_p);
143 
144 
145 GRASSROOTS_UTIL_API bool SetJSONInteger (json_t *json_p, const char * const key_s, const json_int_t value);
146 
157 GRASSROOTS_UTIL_API bool GetJSONLong (const json_t *json_p, const char * const key_s, long *value_p);
158 
159 
170 GRASSROOTS_UTIL_API bool GetJSONReal (const json_t *json_p, const char * const key_s, double *value_p);
171 
172 
173 GRASSROOTS_UTIL_API bool SetJSONReal ( json_t *json_p, const char * const key_s, const double value);
174 
175 
186 GRASSROOTS_UTIL_API bool GetJSONBoolean (const json_t *json_p, const char * const key_s, bool *value_p);
187 
188 
189 GRASSROOTS_UTIL_API bool SetJSONBoolean (json_t *json_p, const char * const key_s, const bool value);
190 
191 
202 GRASSROOTS_UTIL_API bool AddStringArrayToJSON (json_t *parent_p, const char ** const values_ss, const char * const child_key_s);
203 
204 
214 GRASSROOTS_UTIL_API char **GetStringArrayFromJSON (const json_t * const array_p, bool add_terminating_null_flag);
215 
216 
227 GRASSROOTS_UTIL_API bool AddStringListToJSON (json_t *parent_p, LinkedList *values_p, const char * const child_key_s);
228 
229 
239 GRASSROOTS_UTIL_API LinkedList *GetStringListFromJSON (const json_t * const array_p);
240 
241 
251 
252 
261 
262 
273 GRASSROOTS_UTIL_API FieldNode *AllocateFieldNode (const char *name_s, const MEM_FLAG mf, json_type field_type);
274 
282 
283 
293 GRASSROOTS_UTIL_API json_t *LoadJSONFile (const char * const filename_s);
294 
295 
308 GRASSROOTS_UTIL_API json_t *ConvertTabularDataToJSON (char *data_s, const char column_delimiter, const char row_delimiter, LinkedList *headers_p);
309 
310 
326 GRASSROOTS_UTIL_API json_t *ConvertTabularDataWithHeadersToJSON (char *data_s, const char column_delimiter, const char row_delimiter, json_type (*get_type_fn) (const char *name_s, const void * const data_p), const void * const type_data_p);
327 
328 
344 GRASSROOTS_UTIL_API LinkedList *GetTabularHeaders (char **data_ss, const char column_delimiter, const char row_delimiter, json_type (*get_type_fn) (const char *name_s, const void * const data_p), const void * const type_data_p);
345 
346 
360 GRASSROOTS_UTIL_LOCAL json_t *GetJSONFromString (const char *value_s, json_type field_type);
361 
362 
374 GRASSROOTS_UTIL_API json_t *ConvertRowToJSON (char *row_s, LinkedList *headers_p, const char delimiter);
375 
376 
389 GRASSROOTS_UTIL_API bool AddValidJSONString (json_t *parent_p, const char * const key_s, const char * const value_s);
390 
391 
405 GRASSROOTS_UTIL_API int PrintJSONToLog (const uint32 level, const char *filename_s, const int line_number, const json_t *json_p, const char *message_s, ...);
406 
407 
421 GRASSROOTS_UTIL_API int PrintJSONToErrors (const uint32 level, const char *filename_s, const int line_number, const json_t *json_p, const char *message_s, ...);
422 
423 
424 
435 GRASSROOTS_UTIL_API bool IsJSONEmpty (const json_t *json_p);
436 
437 
438 
452 GRASSROOTS_UTIL_API void PrintJSONRefCounts (const uint32 log_level, const char * const filename_s, const int line_number, const json_t * const value_p, const char *initial_s);
453 
454 
466 GRASSROOTS_UTIL_API bool SetBooleanFromJSON (const json_t *json_p, bool *value_p);
467 
468 
480 GRASSROOTS_UTIL_API bool SetRealFromJSON (const json_t *json_p, double *value_p);
481 
482 
494 GRASSROOTS_UTIL_API bool SetIntegerFromJSON (const json_t *json_p, json_int_t *value_p);
495 
496 
508 GRASSROOTS_UTIL_API bool SetLongFromJSON (const json_t *json_p, int64 *value_p);
509 
510 
523 GRASSROOTS_UTIL_API bool SetStringFromJSON (const json_t *json_p, char **value_ss);
524 
525 
526 
536 GRASSROOTS_UTIL_API bool SetJSONNull (json_t *json_p, const char * const key_s);
537 
538 
549 GRASSROOTS_UTIL_API json_t *GetCompoundJSONObject (const json_t *input_p, const char * const compound_s);
550 
551 
567 GRASSROOTS_UTIL_API bool CopyJSONKeyStringValuePair (const json_t *src_p, json_t *dest_p, const char * const key_s, bool optional_flag);
568 
569 
570 
571 
587 GRASSROOTS_UTIL_API bool CopyJSONKeyIntegerValuePair (const json_t *src_p, json_t *dest_p, const char * const key_s, bool optional_flag);
588 
589 
590 
600 GRASSROOTS_UTIL_API bool DeepCopyValidJSON (const json_t *src_p, json_t **dest_pp);
601 
602 
642 GRASSROOTS_UTIL_API bool AddOntologyContextTerm (json_t *root_p, const char *key_s, const char *term_s, const bool add_id_flag);
643 
644 
645 GRASSROOTS_UTIL_API json_t *SplitJSON (json_t *src_p, uint8 percentage_to_move);
646 
647 
648 
649 
650 GRASSROOTS_UTIL_API bool GetJSONStringAsInteger (const json_t *json_p, const char * const key_s, int *answer_p);
651 
652 GRASSROOTS_UTIL_API bool GetJSONStringAsDouble (const json_t *json_p, const char * const key_s, double *answer_p);
653 
654 
655 GRASSROOTS_UTIL_API bool GetJSONUnsignedInteger (const json_t *json_p, const char * const key_s, uint32 *value_p);
656 
657 
658 GRASSROOTS_UTIL_API bool GetRealValueFromJSONString (const json_t *json_p, double64 *answer_p);
659 
660 
661 GRASSROOTS_UTIL_API json_t *ConvertStringArrayToJSON (char **values_ss, const size_t num_values);
662 
663 GRASSROOTS_UTIL_API bool SetNonTrivialUnsignedInt (json_t *json_p, const char *key_s, const uint32 *value_p, const bool null_flag);
664 
665 GRASSROOTS_UTIL_API bool SetNonTrivialString (json_t *value_p, const char *key_s, const char *value_s, const bool null_flag);
666 
667 GRASSROOTS_UTIL_API bool SetNonTrivialDouble (json_t *json_p, const char *key_s, const double64 *value_p, const bool null_fla);
668 
669 
670 #ifdef __cplusplus
671 }
672 #endif
673 
674 #endif /* #ifndef JSON_UTIL_H */
GetStringArrayFromJSON
char ** GetStringArrayFromJSON(const json_t *const array_p, bool add_terminating_null_flag)
Created an array of c-style string of StringListNodes from a json array.
FieldNode::FreeFieldNode
void FreeFieldNode(ListItem *node_p)
Free a FieldNode.
DeepCopyValidJSON
bool DeepCopyValidJSON(const json_t *src_p, json_t **dest_pp)
Make a deep copy of a source JSON fragment to another.
ConvertTabularDataToJSON
json_t * ConvertTabularDataToJSON(char *data_s, const char column_delimiter, const char row_delimiter, LinkedList *headers_p)
Convert a string of tabular data into a JSON array using the given column headings.
SetNonTrivialString
bool SetNonTrivialString(json_t *value_p, const char *key_s, const char *value_s, const bool null_flag)
MEM_FLAG
MEM_FLAG
An enum specifying the particular status of a piece of dynamically allocated memory for a particular ...
Definition: memory_allocations.h:38
GetTabularHeaders
LinkedList * GetTabularHeaders(char **data_ss, const char column_delimiter, const char row_delimiter, json_type(*get_type_fn)(const char *name_s, const void *const data_p), const void *const type_data_p)
Get the column headers from the first row of a tabular data variable.
JsonNode::FreeJsonNode
void FreeJsonNode(ListItem *node_p)
Free a JsonNode.
grassroots_util_library.h
ConvertRowToJSON
json_t * ConvertRowToJSON(char *row_s, LinkedList *headers_p, const char delimiter)
Create a JSON object from a delimited string of data.
PrintJSONToLog
int PrintJSONToLog(const uint32 level, const char *filename_s, const int line_number, const json_t *json_p, const char *message_s,...)
Print a json_t object to the logging stream.
SetJSONStringOrNull
bool SetJSONStringOrNull(json_t *json_p, const char *const key_s, const char *const value_s, const bool null_flag)
AddStringListToJSON
bool AddStringListToJSON(json_t *parent_p, LinkedList *values_p, const char *const child_key_s)
Create and add a LinkedList of StringListNodes to a json object.
IsJSONEmpty
bool IsJSONEmpty(const json_t *json_p)
Is a json_t NULL or empty?
SetNonTrivialUnsignedInt
bool SetNonTrivialUnsignedInt(json_t *json_p, const char *key_s, const uint32 *value_p, const bool null_flag)
SplitJSON
json_t * SplitJSON(json_t *src_p, uint8 percentage_to_move)
AddValidJSONString
bool AddValidJSONString(json_t *parent_p, const char *const key_s, const char *const value_s)
Add a string key-value pair to a JSON object only if the value is not NULL.
SetIntegerFromJSON
bool SetIntegerFromJSON(const json_t *json_p, json_int_t *value_p)
Get the integer value of a JSON object.
PrintJSON
int PrintJSON(FILE *out_f, const json_t *const json_p, const char *const prefix_s)
Print a json fragment to a FILE pointer.
AddOntologyContextTerm
bool AddOntologyContextTerm(json_t *root_p, const char *key_s, const char *term_s, const bool add_id_flag)
Add a "@context" section to a JSON fragment to define a class.
LoadJSONFile
json_t * LoadJSONFile(const char *const filename_s)
Load a JSON file.
SetNonTrivialDouble
bool SetNonTrivialDouble(json_t *json_p, const char *key_s, const double64 *value_p, const bool null_fla)
JsonNode::AllocateJsonNode
JsonNode * AllocateJsonNode(json_t *json_p)
Allocate a JsonNode that points to the given json object.
FieldNode::fn_base_node
StringListNode fn_base_node
The base node.
Definition: json_util.h:74
GetJSONStringAsDouble
bool GetJSONStringAsDouble(const json_t *json_p, const char *const key_s, double *answer_p)
schema_keys.h
The keys for the Grassroots Schema.
FieldNode::AllocateFieldNode
FieldNode * AllocateFieldNode(const char *name_s, const MEM_FLAG mf, json_type field_type)
Allocate a FieldNode that points to the given json object.
GetCopiedJSONString
char * GetCopiedJSONString(const json_t *json_p, const char *const key_s)
Get the newly-allocated value for a given key in a json_t object.
SetLongFromJSON
bool SetLongFromJSON(const json_t *json_p, int64 *value_p)
Get the long value of a JSON object.
JsonNode
A datatype for storing a json_t on a LinkedList.
Definition: json_util.h:43
JsonNode::jn_node
ListItem jn_node
The base List node.
Definition: json_util.h:48
GetJSONStringAsInteger
bool GetJSONStringAsInteger(const json_t *json_p, const char *const key_s, int *answer_p)
GetCompoundJSONObject
json_t * GetCompoundJSONObject(const json_t *input_p, const char *const compound_s)
Get a descendant JSON object from another using a given selector.
LinkedList
A doubly-linked list that can be traversed in either direction.
Definition: linked_list.h:56
CopyJSONKeyIntegerValuePair
bool CopyJSONKeyIntegerValuePair(const json_t *src_p, json_t *dest_p, const char *const key_s, bool optional_flag)
Make a copy of given key-value pair from one JSON object to another.
SetJSONInteger
bool SetJSONInteger(json_t *json_p, const char *const key_s, const json_int_t value)
GRASSROOTS_UTIL_LOCAL
#define GRASSROOTS_UTIL_LOCAL
Definition: grassroots_util_library.h:48
GetJSONLong
bool GetJSONLong(const json_t *json_p, const char *const key_s, long *value_p)
Get the long value for a given key in a json_t object.
GetJSONBoolean
bool GetJSONBoolean(const json_t *json_p, const char *const key_s, bool *value_p)
Get the boolean value for a given key in a json_t object.
SetBooleanFromJSON
bool SetBooleanFromJSON(const json_t *json_p, bool *value_p)
Set the boolean value of a JSON object.
GetJSONString
const char * GetJSONString(const json_t *json_p, const char *const key_s)
Get the value for a given key in a json_t object.
GetJSONUnsignedInteger
bool GetJSONUnsignedInteger(const json_t *json_p, const char *const key_s, uint32 *value_p)
GetJSONReal
bool GetJSONReal(const json_t *json_p, const char *const key_s, double *value_p)
Get the double value for a given key in a json_t object.
GetStringListFromJSON
LinkedList * GetStringListFromJSON(const json_t *const array_p)
Created a LinkedList of StringListNodes from a json array.
ConvertTabularDataWithHeadersToJSON
json_t * ConvertTabularDataWithHeadersToJSON(char *data_s, const char column_delimiter, const char row_delimiter, json_type(*get_type_fn)(const char *name_s, const void *const data_p), const void *const type_data_p)
Convert a string of tabular data into a JSON array.
StringListNode
A ListNode for LinkedLists that also stores a string value.
Definition: string_linked_list.h:49
AddStringArrayToJSON
bool AddStringArrayToJSON(json_t *parent_p, const char **const values_ss, const char *const child_key_s)
Create and add an array of c-style strings to a json object.
linked_list.h
A doubly-linked list.
PrintJSONToErrors
int PrintJSONToErrors(const uint32 level, const char *filename_s, const int line_number, const json_t *json_p, const char *message_s,...)
Print a json_t object to the error stream.
GetJSONInteger
bool GetJSONInteger(const json_t *json_p, const char *const key_s, json_int_t *value_p)
Get the integer value for a given key in a json_t object.
ConvertStringArrayToJSON
json_t * ConvertStringArrayToJSON(char **values_ss, const size_t num_values)
JsonNode::jn_json_p
json_t * jn_json_p
Pointer to the json object.
Definition: json_util.h:51
SetStringFromJSON
bool SetStringFromJSON(const json_t *json_p, char **value_ss)
Get the string value of a JSON object.
PrintJSONRefCounts
void PrintJSONRefCounts(const uint32 log_level, const char *const filename_s, const int line_number, const json_t *const value_p, const char *initial_s)
Print the reference counts for all of the entries in a json_t object to the logging stream.
SetJSONBoolean
bool SetJSONBoolean(json_t *json_p, const char *const key_s, const bool value)
CopyJSONKeyStringValuePair
bool CopyJSONKeyStringValuePair(const json_t *src_p, json_t *dest_p, const char *const key_s, bool optional_flag)
Make a copy of given key-value pair from one JSON object to another.
FieldNode::fn_type
json_type fn_type
This defines the type of the data.
Definition: json_util.h:77
SetRealFromJSON
bool SetRealFromJSON(const json_t *json_p, double *value_p)
Get the real value of a JSON object.
GetRealValueFromJSONString
bool GetRealValueFromJSONString(const json_t *json_p, double64 *answer_p)
SetJSONReal
bool SetJSONReal(json_t *json_p, const char *const key_s, const double value)
FieldNode
A datatype for storing a value as a string along with the datatype to convert it to.
Definition: json_util.h:71
string_linked_list.h
A doubly-linked list of strings.
SetJSONString
bool SetJSONString(json_t *json_p, const char *const key_s, const char *const value_s)
SetJSONNull
bool SetJSONNull(json_t *json_p, const char *const key_s)
Add a child key to a JSON object where the value is null.
GRASSROOTS_UTIL_API
#define GRASSROOTS_UTIL_API
Definition: grassroots_util_library.h:47
ListItem
A doubly-linked node that points to its predecessor and successor.
Definition: linked_list.h:43