nfx-json 1.5.2
Modern C++20 JSON library with schema validation and generation
Loading...
Searching...
No Matches
Builder.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
32
33#pragma once
34
35#include "Document.h"
36
37#include <nfx/Containers.h>
38#include <nfx/string/StringBuilder.h>
39
40#include <charconv>
41#include <cstdint>
42#include <stdexcept>
43#include <string>
44#include <string_view>
45#include <type_traits>
46
47namespace nfx::json
48{
67 class Builder
68 {
69 public:
71 struct Options
72 {
74 int indent = 0;
75
77 size_t bufferSize = 4096;
78
85 bool escapeNonAscii = false;
86 };
87
92 inline explicit Builder( Options options = { 0, 4096, false } );
93
100
107
114
121
128 inline Builder& writeKey( std::string_view key );
129
135 inline Builder& writeRawJson( std::string_view rawJson );
136
143 inline Builder& write( std::string_view key, std::nullptr_t value );
144
151 inline Builder& write( std::string_view key, bool value );
152
159 inline Builder& write( std::string_view key, int value );
160
167 inline Builder& write( std::string_view key, unsigned int value );
168
175 inline Builder& write( std::string_view key, int64_t value );
176
183 inline Builder& write( std::string_view key, uint64_t value );
184
191 inline Builder& write( std::string_view key, float value );
192
199 inline Builder& write( std::string_view key, double value );
200
207 inline Builder& write( std::string_view key, std::string_view value );
208
215 inline Builder& write( std::string_view key, const char* value );
216
223 inline Builder& write( std::string_view key, const std::string& value );
224
231 inline Builder& write( std::string_view key, const Document& value );
232
238 inline Builder& write( std::nullptr_t value );
239
245 inline Builder& write( bool value );
246
252 inline Builder& write( int value );
253
259 inline Builder& write( unsigned int value );
260
266 inline Builder& write( int64_t value );
267
273 inline Builder& write( uint64_t value );
274
280 inline Builder& write( float value );
281
287 inline Builder& write( double value );
288
294 inline Builder& write( std::string_view value );
295
301 inline Builder& write( const char* value );
302
308 inline Builder& write( const std::string& value );
309
315 inline Builder& write( const Document& value );
316
323 template <typename T>
324 inline std::enable_if_t<
325 std::is_same_v<T, long> && !std::is_same_v<long, int> && !std::is_same_v<long, int64_t>,
326 Builder&>
327 write( std::string_view key, T value );
328
336 template <typename T>
337 inline std::enable_if_t<
338 std::is_same_v<T, unsigned long> && !std::is_same_v<unsigned long, unsigned int> &&
339 !std::is_same_v<unsigned long, uint64_t>,
340 Builder&>
341 write( std::string_view key, T value );
342
349 template <typename T>
350 inline std::enable_if_t<std::is_same_v<T, long long> && !std::is_same_v<long long, int64_t>, Builder&> write(
351 std::string_view key, T value );
352
360 template <typename T>
361 inline std::enable_if_t<
362 std::is_same_v<T, unsigned long long> && !std::is_same_v<unsigned long long, uint64_t>,
363 Builder&>
364 write( std::string_view key, T value );
365
371 template <typename T>
372 inline std::enable_if_t<
373 std::is_same_v<T, long> && !std::is_same_v<long, int> && !std::is_same_v<long, int64_t>,
374 Builder&>
375 write( T value );
376
383 template <typename T>
384 inline std::enable_if_t<
385 std::is_same_v<T, unsigned long> && !std::is_same_v<unsigned long, unsigned int> &&
386 !std::is_same_v<unsigned long, uint64_t>,
387 Builder&>
388 write( T value );
389
395 template <typename T>
396 inline std::enable_if_t<std::is_same_v<T, long long> && !std::is_same_v<long long, int64_t>, Builder&> write(
397 T value );
398
405 template <typename T>
406 inline std::enable_if_t<
407 std::is_same_v<T, unsigned long long> && !std::is_same_v<unsigned long long, uint64_t>,
408 Builder&>
409 write( T value );
410
415 inline std::string toString();
416
422 inline Builder& reset();
423
428 [[nodiscard]] inline size_t size() const noexcept;
429
434 [[nodiscard]] inline bool isEmpty() const noexcept;
435
442 inline Builder& reserve( size_t capacity );
443
449 [[nodiscard]] inline std::string_view toStringView() const noexcept;
450
455 [[nodiscard]] inline size_t capacity() const noexcept;
456
462 [[nodiscard]] inline size_t depth() const noexcept;
463
468 [[nodiscard]] inline bool isValid() const noexcept;
469
478 template <typename Container>
479 inline Builder& writeArray( std::string_view key, const Container& values );
480
488 template <typename Container>
489 inline Builder& writeArray( const Container& values );
490
491 private:
495 struct ContextFrame
496 {
497 bool isObject;
498 bool isEmpty;
499 bool expectingValue;
500 };
501
506 inline void writeInt( int64_t value );
507
512 inline void writeUInt( uint64_t value );
513
518 inline void writeDouble( double value );
519
524 inline void writeString( std::string_view str );
525
529 inline void writeNewlineAndIndent();
530
534 inline void writeCommaIfNeeded();
535
541 inline void writeDocument( const Document& doc );
542
547 inline void writeDocumentArray( const Array& array );
548
553 inline void writeDocumentObject( const Object& object );
554
555 string::StringBuilder m_buffer;
556 int m_indent;
557 int m_currentIndent;
558 bool m_escapeNonAscii;
559 containers::StackVector<ContextFrame, 8> m_contextStack;
560 };
561} // namespace nfx::json
562
563#include "detail/Builder.inl"
JSON Document type with low-level and high-level APIs.
std::vector< std::pair< std::string, Document > > Object
Ordered map for JSON objects (preserves insertion order).
Definition Document.h:69
std::vector< Document > Array
Array for JSON arrays.
Definition Document.h:74
Builder & writeEndArray()
End writing a JSON array.
bool isEmpty() const noexcept
Check if builder is empty.
Builder & write(std::string_view key, std::string_view value)
Write a string value property.
Builder & reset()
Reset builder to initial state for reuse.
std::string toString()
Get the constructed JSON string.
Builder & write(std::string_view key, bool value)
Write a boolean value property.
std::enable_if_t< std::is_same_v< T, unsigned long long > &&!std::is_same_v< unsigned long long, uint64_t >, Builder & > write(std::string_view key, T value)
Write an unsigned long long integer value property (SFINAE overload for platforms where unsigned long...
Builder & writeKey(std::string_view key)
Write a property key before a nested object or array.
Builder & write(const std::string &value)
Write a string value in an array.
Builder & write(std::string_view key, float value)
Write a float value property.
std::enable_if_t< std::is_same_v< T, long > &&!std::is_same_v< long, int > &&!std::is_same_v< long, int64_t >, Builder & > write(std::string_view key, T value)
Write a long integer value property (SFINAE overload for platforms where long != int64_t).
std::enable_if_t< std::is_same_v< T, long long > &&!std::is_same_v< long long, int64_t >, Builder & > write(std::string_view key, T value)
Write a long long integer value property (SFINAE overload for platforms where long long !...
std::enable_if_t< std::is_same_v< T, unsigned long long > &&!std::is_same_v< unsigned long long, uint64_t >, Builder & > write(T value)
Write an unsigned long long integer value in an array (SFINAE overload for platforms where unsigned l...
Builder & write(std::string_view key, const Document &value)
Write a Document value property.
Builder & write(const Document &value)
Write a Document value in an array.
Builder & write(std::string_view key, int64_t value)
Write a 64-bit integer value property.
Builder & write(int64_t value)
Write a 64-bit integer value in an array.
Builder & write(std::string_view key, const char *value)
Write a string value property.
std::enable_if_t< std::is_same_v< T, long > &&!std::is_same_v< long, int > &&!std::is_same_v< long, int64_t >, Builder & > write(T value)
Write a long integer value in an array (SFINAE overload for platforms where long !...
size_t size() const noexcept
Get current JSON string size.
std::enable_if_t< std::is_same_v< T, long long > &&!std::is_same_v< long long, int64_t >, Builder & > write(T value)
Write a long long integer value in an array (SFINAE overload for platforms where long long !...
Builder & writeArray(std::string_view key, const Container &values)
Write a complete array from a STL container as object property.
std::string_view toStringView() const noexcept
Get non-destructive view of generated JSON.
Builder & write(int value)
Write an integer value in an array.
Builder & write(bool value)
Write a boolean value in an array.
Builder & write(std::string_view key, double value)
Write a double value property.
Builder & write(std::string_view key, const std::string &value)
Write a string value property.
bool isValid() const noexcept
Check if builder state is valid.
Builder & writeStartObject()
Start writing a JSON object.
Builder & write(const char *value)
Write a string value in an array.
Builder & write(uint64_t value)
Write an unsigned 64-bit integer value in an array.
Builder & write(std::string_view value)
Write a string value in an array.
Builder & write(float value)
Write a float value in an array.
Builder & reserve(size_t capacity)
Reserve buffer capacity.
Builder & write(std::string_view key, std::nullptr_t value)
Write a null value property.
Builder & writeStartArray()
Start writing a JSON array.
Builder & write(std::string_view key, unsigned int value)
Write an unsigned integer value property.
Builder & write(std::string_view key, uint64_t value)
Write an unsigned 64-bit integer value property.
Builder & writeRawJson(std::string_view rawJson)
Write raw JSON string directly to output.
std::enable_if_t< std::is_same_v< T, unsigned long > &&!std::is_same_v< unsigned long, unsigned int > &&!std::is_same_v< unsigned long, uint64_t >, Builder & > write(T value)
Write an unsigned long integer value in an array (SFINAE overload for platforms where unsigned long !...
Builder(Options options={ 0, 4096, false })
Constructor with options.
size_t capacity() const noexcept
Get current buffer capacity.
Builder & write(std::nullptr_t value)
Write a null value in an array.
Builder & writeEndObject()
End writing a JSON object.
Builder & write(double value)
Write a double value in an array.
size_t depth() const noexcept
Get current nesting depth.
Builder & write(unsigned int value)
Write an unsigned integer value in an array.
Builder & write(std::string_view key, int value)
Write an integer value property.
std::enable_if_t< std::is_same_v< T, unsigned long > &&!std::is_same_v< unsigned long, unsigned int > &&!std::is_same_v< unsigned long, uint64_t >, Builder & > write(std::string_view key, T value)
Write an unsigned long integer value property (SFINAE overload for platforms where unsigned long !...
Configuration options for Builder.
Definition Builder.h:72
size_t bufferSize
Initial buffer capacity hint.
Definition Builder.h:77
int indent
Indentation level (0 = compact, >0 = pretty print with N spaces per level).
Definition Builder.h:74
bool escapeNonAscii
Escape non-ASCII UTF-8 characters as \uXXXX escape sequences.
Definition Builder.h:85
Low-level JSON value storage type.
Definition Document.h:108
Concept for JSON container types only.
Definition Concepts.h:101