simdjson 4.4.0
Ridiculously Fast JSON
Loading...
Searching...
No Matches
fractured_json_builder.h
1#ifndef SIMDJSON_GENERIC_FRACTURED_JSON_BUILDER_H
2#define SIMDJSON_GENERIC_FRACTURED_JSON_BUILDER_H
3
4#ifndef SIMDJSON_CONDITIONAL_INCLUDE
5#include "simdjson/generic/builder/json_builder.h"
6#include "simdjson/dom/fractured_json.h"
7#endif // SIMDJSON_CONDITIONAL_INCLUDE
8
9#if SIMDJSON_STATIC_REFLECTION
10
11namespace simdjson {
12namespace SIMDJSON_IMPLEMENTATION {
13namespace builder {
14
37template <class T>
38simdjson_warn_unused simdjson_result<std::string> to_fractured_json_string(
39 const T& obj,
40 const fractured_json_options& opts = {},
41 size_t initial_capacity = string_builder::DEFAULT_INITIAL_CAPACITY) {
42 // Step 1: Serialize to minified JSON
43 std::string formatted;
44 auto error = to_json_string(obj, initial_capacity).get(formatted);
45 if (error) {
46 return error;
47 }
48
49 // Step 2: Reformat with FracturedJson
50 return fractured_json_string(formatted, opts);
51}
52
67template<constevalutil::fixed_string... FieldNames, typename T>
68 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
69simdjson_warn_unused simdjson_result<std::string> extract_fractured_json(
70 const T& obj,
71 const fractured_json_options& opts = {},
72 size_t initial_capacity = string_builder::DEFAULT_INITIAL_CAPACITY) {
73 // Step 1: Extract fields to minified JSON
74 std::string formatted;
75 auto error = extract_from<FieldNames...>(obj, initial_capacity).get(formatted);
76 if (error) {
77 return error;
78 }
79
80 // Step 2: Reformat with FracturedJson
81 return fractured_json_string(formatted, opts);
82}
83
84} // namespace builder
85} // namespace SIMDJSON_IMPLEMENTATION
86
87// Global namespace convenience functions
88
93template <class T>
94simdjson_warn_unused simdjson_result<std::string> to_fractured_json_string(
95 const T& obj,
96 const fractured_json_options& opts = {},
97 size_t initial_capacity = SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
98 return SIMDJSON_IMPLEMENTATION::builder::to_fractured_json_string(obj, opts, initial_capacity);
99}
104template<constevalutil::fixed_string... FieldNames, typename T>
105 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
106simdjson_warn_unused simdjson_result<std::string> extract_fractured_json(
107 const T& obj,
108 const fractured_json_options& opts = {},
109 size_t initial_capacity = SIMDJSON_IMPLEMENTATION::builder::string_builder::DEFAULT_INITIAL_CAPACITY) {
110 return SIMDJSON_IMPLEMENTATION::builder::extract_fractured_json<FieldNames...>(obj, opts, initial_capacity);
111}
112
113} // namespace simdjson
114
115#endif // SIMDJSON_STATIC_REFLECTION
116
117#endif // SIMDJSON_GENERIC_FRACTURED_JSON_BUILDER_H
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
simdjson_result< std::string_view > to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document &x) noexcept
Create a string-view instance out of a document instance.
std::string fractured_json_string(std::string_view json_str)
Format a JSON string using FracturedJson formatting.