Pharos
0.7.23
Modern Web Framework & Web App Hosting Service.
Toggle main menu visibility
Loading...
Searching...
No Matches
native_yyjson_helpers.h
Go to the documentation of this file.
1
/**
2
* @file
3
* @brief Declares shared helper wrappers around vendored yyjson parsing and serialization APIs.
4
*/
5
6
#ifndef PHAROS_NATIVE_YYJSON_HELPERS_H
7
#define PHAROS_NATIVE_YYJSON_HELPERS_H
8
9
#include "
yyjson.h
"
10
11
/** @name Shared yyjson helper APIs
12
* @{
13
*/
14
15
/**
16
* @brief Parses a JSON file into an owned yyjson document.
17
* @param path Path to the JSON file.
18
* @return Parsed document, or NULL on read or parse failure.
19
* @note The caller owns the returned document and must release it with
20
* `native_json_doc_free()`.
21
*/
22
yyjson_doc
*
native_json_doc_load_file
(
const
char
*path);
23
24
/**
25
* @brief Parses JSON text into an owned yyjson document.
26
* @param text NUL-terminated JSON text.
27
* @return Parsed document, or NULL on parse failure.
28
* @note The caller owns the returned document and must release it with
29
* `native_json_doc_free()`.
30
*/
31
yyjson_doc
*
native_json_doc_load_text
(
const
char
*text);
32
33
/**
34
* @brief Releases a document returned by the native yyjson helpers.
35
* @param doc Document to free. NULL is allowed.
36
*/
37
void
native_json_doc_free
(
yyjson_doc
*doc);
38
39
/**
40
* @brief Looks up a key from a JSON object value.
41
* @param obj Object value to inspect.
42
* @param key Key name to resolve.
43
* @return Matching value, or NULL when the object or key is invalid or absent.
44
* @note Returned values are borrowed from the source document.
45
*/
46
yyjson_val
*
native_json_obj_get
(
yyjson_val
*obj,
const
char
*key);
47
48
/**
49
* @brief Looks up an array-valued key from a JSON object value.
50
* @param obj Object value to inspect.
51
* @param key Key name to resolve.
52
* @return Matching array value, or NULL when absent or not an array.
53
* @note Returned values are borrowed from the source document.
54
*/
55
yyjson_val
*
native_json_obj_get_array
(
yyjson_val
*obj,
const
char
*key);
56
57
/**
58
* @brief Duplicates a string-valued key from a JSON object.
59
* @param obj Object value to inspect.
60
* @param key Key name to resolve.
61
* @return Newly allocated duplicated string, or NULL when absent, not a string,
62
* or allocation fails.
63
* @note The caller owns the returned string and must free it with `free()`.
64
*/
65
char
*
native_json_obj_get_string_dup
(
yyjson_val
*obj,
const
char
*key);
66
67
/**
68
* @brief Reads a numeric object member or returns a fallback value.
69
* @param obj Object value to inspect.
70
* @param key Key name to resolve.
71
* @param default_value Value returned when the key is absent or not numeric.
72
* @param found_out Optional output flag set to non-zero when a numeric value was
73
* found and consumed, otherwise 0.
74
* @return Numeric value converted to `long`, or `default_value` on failure.
75
*/
76
long
native_json_obj_get_long_default
(
77
yyjson_val
*obj,
78
const
char
*key,
79
long
default_value,
80
int
*found_out);
81
82
/**
83
* @brief Finds a migration record by `migration_id` under a fixture root.
84
* @param root Root object that may contain a `migrations` array.
85
* @param migration_id Migration identifier to match.
86
* @return Matching migration object, or NULL when not found.
87
* @note Returned values are borrowed from the source document.
88
*/
89
yyjson_val
*
native_json_find_migration_by_id
(
90
yyjson_val
*root,
91
const
char
*migration_id);
92
93
/**
94
* @brief Finds a backend record by `backend` name under a fixture root.
95
* @param root Root object that may contain a `backends` array.
96
* @param backend_name Backend name to match.
97
* @return Matching backend object, or NULL when not found.
98
* @note Returned values are borrowed from the source document.
99
*/
100
yyjson_val
*
native_json_find_backend_by_name
(
101
yyjson_val
*root,
102
const
char
*backend_name);
103
104
/**
105
* @brief Serializes a JSON value to compact JSON text.
106
* @param value Value to serialize.
107
* @return Newly allocated compact JSON string, or NULL on serialization failure.
108
* @note The caller owns the returned string and must free it with `free()`.
109
*/
110
char
*
native_json_serialize_compact_dup
(
yyjson_val
*value);
111
112
/**
113
* @brief Converts a JSON string array into newline-delimited text.
114
* @param array_value Array whose elements must all be JSON strings.
115
* @return Newly allocated newline-delimited string, or NULL when the input is
116
* invalid or allocation fails.
117
* @note The caller owns the returned string and must free it with `free()`.
118
*/
119
char
*
native_json_string_array_lines_dup
(
yyjson_val
*array_value);
120
121
/** @} */
122
123
#endif
native_json_doc_load_file
yyjson_doc * native_json_doc_load_file(const char *path)
Parses a JSON file into an owned yyjson document.
Definition
native_yyjson_helpers.c:113
native_json_find_migration_by_id
yyjson_val * native_json_find_migration_by_id(yyjson_val *root, const char *migration_id)
Finds a migration record by migration_id under a fixture root.
Definition
native_yyjson_helpers.c:186
native_json_obj_get_long_default
long native_json_obj_get_long_default(yyjson_val *obj, const char *key, long default_value, int *found_out)
Reads a numeric object member or returns a fallback value.
Definition
native_yyjson_helpers.c:166
native_json_doc_free
void native_json_doc_free(yyjson_doc *doc)
Releases a document returned by the native yyjson helpers.
Definition
native_yyjson_helpers.c:140
native_json_find_backend_by_name
yyjson_val * native_json_find_backend_by_name(yyjson_val *root, const char *backend_name)
Finds a backend record by backend name under a fixture root.
Definition
native_yyjson_helpers.c:196
native_json_doc_load_text
yyjson_doc * native_json_doc_load_text(const char *text)
Parses JSON text into an owned yyjson document.
Definition
native_yyjson_helpers.c:128
native_json_obj_get_string_dup
char * native_json_obj_get_string_dup(yyjson_val *obj, const char *key)
Duplicates a string-valued key from a JSON object.
Definition
native_yyjson_helpers.c:162
native_json_string_array_lines_dup
char * native_json_string_array_lines_dup(yyjson_val *array_value)
Converts a JSON string array into newline-delimited text.
Definition
native_yyjson_helpers.c:213
native_json_obj_get_array
yyjson_val * native_json_obj_get_array(yyjson_val *obj, const char *key)
Looks up an array-valued key from a JSON object value.
Definition
native_yyjson_helpers.c:154
native_json_obj_get
yyjson_val * native_json_obj_get(yyjson_val *obj, const char *key)
Looks up a key from a JSON object value.
Definition
native_yyjson_helpers.c:147
native_json_serialize_compact_dup
char * native_json_serialize_compact_dup(yyjson_val *value)
Serializes a JSON value to compact JSON text.
Definition
native_yyjson_helpers.c:206
yyjson_doc
Definition
yyjson.h:4973
yyjson_val
Definition
yyjson.h:4968
yyjson.h
src
native_runtime
c
native_yyjson_helpers.h
Generated by
1.17.0