nfx-serialization 0.3.0
Cross-platform C++ JSON serialization library with extensible trait capabilities
Loading...
Searching...
No Matches
SchemaGenerator.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 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 <optional>
36#include <string>
37#include <string_view>
38#include <vector>
39
40#include "Document.h"
41
42namespace nfx::serialization::json
43{
44 //=====================================================================
45 // SchemaGenerator class
46 //=====================================================================
47
54 class SchemaGenerator final
55 {
56 public:
57 //----------------------------------------------
58 // Options
59 //----------------------------------------------
60
64 struct Options
65 {
66 bool inferFormats = true;
67 bool inferConstraints = false;
68 bool detectEnums = false;
69 size_t enumThreshold = 10;
70 std::string title = {};
71 std::string description = {};
72 std::string id = {};
73 };
74
75 //----------------------------------------------
76 // Construction
77 //----------------------------------------------
78
83 explicit SchemaGenerator( const Document& document );
84
90 SchemaGenerator( const Document& document, const Options& options );
91
98 explicit SchemaGenerator( const std::vector<Document>& documents );
99
107 SchemaGenerator( const std::vector<Document>& documents, const Options& options );
108
110 SchemaGenerator( const SchemaGenerator& ) = default;
111
113 SchemaGenerator( SchemaGenerator&& ) noexcept = default;
114
115 //----------------------------------------------
116 // Destruction
117 //----------------------------------------------
118
120 ~SchemaGenerator() = default;
121
122 //----------------------------------------------
123 // Assignment
124 //----------------------------------------------
125
130 SchemaGenerator& operator=( const SchemaGenerator& ) = default;
131
136 SchemaGenerator& operator=( SchemaGenerator&& ) noexcept = default;
137
138 //----------------------------------------------
139 // Accessors
140 //----------------------------------------------
141
146 const Document& schema() const noexcept { return m_schema; }
147
148 private:
149 //----------------------------------------------
150 // Internal methods
151 //----------------------------------------------
152
156 void addMetadata();
157
163 Document generateSchema( const Document& value ) const;
164
165 //----------------------------------------------
166 // Member data
167 //----------------------------------------------
168
169 Document m_schema;
170 Options m_options;
171 };
172} // namespace nfx::serialization::json
Generic document abstraction for JSON serialization.
Generic JSON document abstraction for serialization.
Definition Document.h:59
SchemaGenerator(const std::vector< Document > &documents)
Generate JSON Schema from multiple sample documents.
SchemaGenerator(const Document &document)
Generate JSON Schema from a single document.
SchemaGenerator(const std::vector< Document > &documents, const Options &options)
Generate JSON Schema from multiple sample documents.
SchemaGenerator(const SchemaGenerator &)=default
Copy constructor.
SchemaGenerator(SchemaGenerator &&) noexcept=default
Move constructor.
SchemaGenerator(const Document &document, const Options &options)
Generate JSON Schema from a single document.
const Document & schema() const noexcept
Get the generated schema.
Configuration options for schema generation.
bool detectEnums
Detect limited value sets as enums - requires multi-sample.
bool inferFormats
Detect string formats (email, date, URI, UUID, etc.).
std::string description
Schema description (optional).
std::string title
Schema title (optional).
bool inferConstraints
Generate constraints (minLength, minimum, etc.) - requires multi-sample.
size_t enumThreshold
Maximum unique values to consider as enum.