File: quick-start.md

package info (click to toggle)
glaze 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,948 kB
  • sloc: cpp: 121,839; sh: 99; ansic: 26; makefile: 13
file content (217 lines) | stat: -rw-r--r-- 5,528 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Glaze Quick Start Guide

Glaze is one of the fastest JSON libraries in the world, featuring compile-time reflection that lets you serialize C++ structs without writing any metadata or macros.

## Basic Reflection

### Simple Struct Serialization

```cpp
#include "glaze/glaze.hpp"

struct Person {
    int age = 25;
    std::string name = "John";
    double height = 5.9;
};

int main() {
    Person person{30, "Alice", 5.7};
    
    // Write to JSON
    std::string json = glz::write_json(person).value_or("error");
    // Result: {"age":30,"name":"Alice","height":5.7}
    
    // Read from JSON
    std::string input = R"({"age":25,"name":"Bob","height":6.1})";
    Person new_person{};
    auto error = glz::read_json(new_person, input);
    if (error) {
       std::string error_msg = glz::format_error(error, input);
       std::cout << error_msg << std::endl;
    }
    else {
       // Success! new_person is now populated
       std::cout << new_person.name << " is " << new_person.age << " years old\n";
    }
    
    return 0;
}
```

### Alternative Expected Handling

```cpp
// Using std::expected return values
auto result = glz::read_json<Person>(input);
if (result) {
    Person person = result.value();
    // Use person...
} else {
    // Handle error
    std::string error_msg = glz::format_error(result, input);
    std::cout << error_msg << std::endl;
}
```

## Custom Field Names

Use `glz::meta` to customize JSON field names:

```cpp
struct Product {
    int id;
    std::string name;
    double price;
};

template <>
struct glz::meta<Product> {
    using T = Product;
    static constexpr auto value = glz::object(
        "product_id", &T::id,
        "product_name", &T::name,
        "unit_price", &T::price
    );
};

// JSON will use: {"product_id":123,"product_name":"Widget","unit_price":9.99}
```

## Containers and Standard Types

Glaze automatically handles standard containers:

```cpp
struct Data {
    std::vector<int> numbers = {1, 2, 3};
    std::map<std::string, std::string> config = {{"key", "value"}};
    std::array<double, 3> coordinates = {1.0, 2.0, 3.0};
    std::optional<std::string> description; // null if empty (output skipped by default)
};

// JSON: {"numbers":[1,2,3],"config":{"key":"value"},"coordinates":[1,2,3]}
```

## Enums as Strings

```cpp
enum class Status { Active, Inactive, Pending };

template <>
struct glz::meta<Status> {
    using enum Status;
    static constexpr auto value = glz::enumerate(Active, Inactive, Pending);
};

struct Task {
    std::string name;
    Status status = Status::Pending;
};

// JSON: {"name":"My Task","status":"Pending"}
```

## File I/O

```cpp
Person person{25, "Charlie", 5.8};

// Write to file
auto write_error = glz::write_file_json(person, "./person.json", std::string{});

// Read from file  
Person loaded_person{};
auto read_error = glz::read_file_json(loaded_person, "./person.json", std::string{});
```

## Pretty Printing

```cpp
Person person{25, "Diana", 5.6};

// Write prettified JSON
std::string pretty_json;
auto error = glz::write<glz::opts{.prettify = true}>(person, pretty_json);

// Or prettify existing JSON
std::string minified = R"({"age":25,"name":"Diana","height":5.6})";
std::string beautiful = glz::prettify_json(minified);
```

## Optional Fields and Null Handling

```cpp
struct User {
    std::string username;
    std::optional<std::string> email;  // Can be null
    std::unique_ptr<std::string> bio;  // Can be null
};

User user{"alice", std::nullopt, nullptr};

// With skip_null_members (default: true)
// JSON: {"username":"alice"}

// With skip_null_members = false
std::string json;
glz::write<glz::opts{.skip_null_members = false}>(user, json);
// JSON: {"username":"alice","email":null,"bio":null}
```

## Variants

```cpp
struct Circle { double radius; };
struct Rectangle { double width, height; };

using Shape = std::variant<Circle, Rectangle>;

struct Drawing {
    std::string name;
    Shape shape;
};

// Glaze automatically handles variants based on which fields are present
```

## Error Handling with Details

```cpp
std::string invalid_json = R"({"name": "test", "age": "not_a_number"})";
Person person{};
auto error = glz::read_json(person, invalid_json);

if (error) {
    std::string detailed_error = glz::format_error(error, invalid_json);
    std::cout << detailed_error << std::endl;
    // Shows exactly where the error occurred with context
}
```

## Common Options

```cpp
// Read with comments support (JSONC)
auto error = glz::read<glz::opts{.comments = true}>(obj, json_with_comments);

// Allow unknown keys (don't error on extra fields)
auto error = glz::read<glz::opts{.error_on_unknown_keys = false}>(obj, json);

// Require all keys to be present
auto error = glz::read<glz::opts{.error_on_missing_keys = true}>(obj, json);

// Write with custom indentation
auto error = glz::write<glz::opts{.prettify = true, .indentation_width = 2}>(obj, json);
```

## Performance Tips

1. **Use `std::string` buffers**: Glaze is optimized for `std::string` input/output
3. **Reuse buffers**: Clear and reuse `std::string` buffers instead of creating new ones

## Next Steps

- Explore [advanced features](https://github.com/stephenberry/glaze/tree/main/docs) like JSON pointers, JMESPath queries, and binary formats
- Check out [wrappers](https://github.com/stephenberry/glaze/blob/main/docs/wrappers.md) for fine-grained control
- Learn about [custom serialization](https://github.com/stephenberry/glaze/blob/main/docs/custom-serialization.md) for complex types