Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
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 */
22yyjson_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 */
31yyjson_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 */
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 */
46yyjson_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 */
55yyjson_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 */
65char *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 */
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 */
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 */
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 */
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 */
120
121/** @} */
122
123#endif
yyjson_doc * native_json_doc_load_file(const char *path)
Parses a JSON file into an owned yyjson document.
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.
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.
void native_json_doc_free(yyjson_doc *doc)
Releases a document returned by the native yyjson helpers.
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.
yyjson_doc * native_json_doc_load_text(const char *text)
Parses JSON text into an owned yyjson document.
char * native_json_obj_get_string_dup(yyjson_val *obj, const char *key)
Duplicates a string-valued key from a JSON object.
char * native_json_string_array_lines_dup(yyjson_val *array_value)
Converts a JSON string array into newline-delimited text.
yyjson_val * native_json_obj_get_array(yyjson_val *obj, const char *key)
Looks up an array-valued key from a JSON object value.
yyjson_val * native_json_obj_get(yyjson_val *obj, const char *key)
Looks up a key from a JSON object value.
char * native_json_serialize_compact_dup(yyjson_val *value)
Serializes a JSON value to compact JSON text.