Skip to content

Commit

Permalink
Preserve state left by Jeremy
Browse files Browse the repository at this point in the history
  • Loading branch information
awaismirza92 committed Sep 26, 2024
1 parent 4c8d37a commit 2892934
Show file tree
Hide file tree
Showing 13 changed files with 986 additions and 78 deletions.
13 changes: 7 additions & 6 deletions docs/concepts/field_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ These annotations are required if you are planning to use a compiler other than
There are a two things you have to keep in mind:
!!! tip 1. If you annotate *some* of the fields using `rfl::Field`, then you must annotate *all* of them.
!!! note
1. If you annotate *some* of the fields using `rfl::Field`, then you must annotate *all* of them.
2. If you are combining structs using `rfl::Flatten`, then they structs combined like this must either be annotated or not.
2. If you are combining structs using `rfl::Flatten`, then they structs combined like this must either be annotated or not.
You can initilize your struct like this:
Expand Down Expand Up @@ -90,7 +91,7 @@ all fields will be flattened to a single JSON object.

If there are duplicate field names, you will get a compile-time error.

** Example: Every employee is a person **
**Example: Every employee is a person**

```cpp
struct Person {
Expand Down Expand Up @@ -121,7 +122,7 @@ This flattens all fields into a single JSON object:
{"first_name":"Homer","last_name":"Simpson","age":45,"salary":60000.0}
```

** Example, using the `rfl::Field`-syntax.**
**Example, using the `rfl::Field`-syntax.**

```cpp
struct Person {
Expand Down Expand Up @@ -168,8 +169,8 @@ This can be fully serialized and deserialized using `rfl::json::read` and `rfl::
However, there are a two things you have to keep in mind:
1) If you annotate *some* of the fields using either `rfl::Field`, then you must annotate *all* of them using one of these two.
2) If you are combining structs using `rfl::Flatten`, then they structs combined like this must either be annotated or not.
1. If you annotate *some* of the fields using either `rfl::Field`, then you must annotate *all* of them using one of these two.
2. If you are combining structs using `rfl::Flatten`, then they structs combined like this must either be annotated or not.
### Struct flattening with anonymous fields
Expand Down
19 changes: 9 additions & 10 deletions docs/concepts/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,19 @@ And you can parse it back into a struct:
const auto homer = rfl::json::read<Person>(json_string).value();
```

## Important note
!!! note
**Do not create custom constructors on the structs.**

**Do not create custom constructors on the structs.**
reflect-cpp needs to be able to create the structs like this:

reflect-cpp needs to be able to create the structs like this:
```cpp
Person{"Bart", "Simpson", ...};
```

```cpp
Person{"Bart", "Simpson", ...};
```
But if you create a custom constructor, then C++ will no longer allow this kind of constructions.
But if you create a custom constructor, then C++ will no longer allow this kind of constructions.

If you want to create the struct from one of your classes (the most like reason, you want to create custom constructors in the first place),
you might want to check out the section on [custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) or [custom parsers](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md).
If you want to create the struct from one of your classes (the most like reason, you want to create custom constructors in the first place),
you might want to check out the section on [custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) or [custom parsers](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md).


## Utility functions
Expand Down
8 changes: 4 additions & 4 deletions docs/concepts/supported_formats/json_schema.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# JSON schema

JSON schemata are a powerful tool for expressing the expected structure of your input. You can use it to validate your input before you even send it to your C++ backend, which will result in better UX.

It can also be used for code generation. For instance, tools such as https://app.quicktype.io/ allow you to generate code in multiple programming languages from the JSON schema (even though the validations are usually omitted).
!!! tip
It can also be used for code generation. For instance, tools such as [quicktype.io](https://app.quicktype.io/) allow you to generate code in multiple programming languages from the JSON schema (even though the validations are usually omitted).

If you are interacting with Python, we warmly recommend https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/. This allows you to generate Pydantic dataclasses, including the validation, from the JSON schema.
If you are interacting with Python, we warmly recommend the [pydantic data model code generator](https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/). This allows you to generate Pydantic dataclasses, including the validation, from the JSON schema.

Note that this is only supported for JSON, not for other formats.
Note that this is only supported for JSON, not for other formats.

## Basic idea

Expand Down
49 changes: 25 additions & 24 deletions docs/concepts/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,36 +218,37 @@ This results in the following JSON string:
{"radius":2.0,"color":"green"}
```
However, some limitations apply:
!!! note
However, some limitations apply:
1. They must be be scoped enumerations.
1. They must be be scoped enumerations.
```cpp
/// OK - scoped enumeration
enum class Color1 { red, green, blue, yellow };
```cpp
/// OK - scoped enumeration
enum class Color1 { red, green, blue, yellow };
/// OK - scoped enumeration
enum struct Color2 { red, green, blue, yellow };
/// OK - scoped enumeration
enum struct Color2 { red, green, blue, yellow };
/// unsupported - unscoped enumerations
enum Color3 { red, green, blue, yellow };
```
/// unsupported - unscoped enumerations
enum Color3 { red, green, blue, yellow };
```
2. You cannot have more than 128 values and if you explicitly assign values, they must be between 0 and 127.
2. You cannot have more than 128 values and if you explicitly assign values, they must be between 0 and 127.
```cpp
/// OK
enum class Color1 { red, green, blue, yellow };
```cpp
/// OK
enum class Color1 { red, green, blue, yellow };
/// OK
enum struct Color2 { red, green, blue = 50, yellow };
/// OK
enum struct Color2 { red, green, blue = 50, yellow };
/// unsupported - red is a negative value
enum Color3 { red = -10, green, blue, yellow };
/// unsupported - red is a negative value
enum Color3 { red = -10, green, blue, yellow };
/// unsupported - red is greater than 127
enum Color3 { red = 200, green, blue, yellow };
```
/// unsupported - red is greater than 127
enum Color3 { red = 200, green, blue, yellow };
```
### Flag enums
Expand Down Expand Up @@ -503,7 +504,7 @@ is a key-value-pair will be represented as arrays of pairs.
## Variants and tagged unions
### untagged
### Untagged
Sometimes you know that the JSON object can be one of several alternatives. For example,
you might have several shapes like `Circle`, `Rectangle` or `Square`. For these kind of
Expand Down Expand Up @@ -541,7 +542,7 @@ several problems with this:
3. It is dangerous. Imagine we had written `std::variant<Circle, Square, Rectangle>` instead of `std::variant<Circle, Rectangle, Square>`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs.
### internally tagged
### Internally tagged
One way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
Expand Down Expand Up @@ -660,7 +661,7 @@ const auto r2 = rfl::json::read<Shapes>(json_string);
Note that in this case the type of the field `shape` MUST be `rfl::Literal`.
Also note that this is exactly how tagged unions work in Pydantic.
### externally tagged
### Externally tagged
Another approach is to add external tags.
Expand Down
6 changes: 4 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
As the aforementioned libraries are among the most widely used in the respective languages, reflect-cpp fills an important gap in C++ development. It reduces boilerplate code and increases code safety.

## Why use reflect-cpp?

- Close integration with containers from the C++ standard library
- Close adherence to C++ idioms
- Out-of-the-box support for JSON
Expand All @@ -39,9 +40,10 @@ The following table lists the serialization formats currently supported by refle
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |

Support for more serialization formats is in development. Refer to the [issues](https://github.com/getml/reflect-cpp/issues) for details.
!!! note
Support for more serialization formats is in development. Refer to the [issues](https://github.com/getml/reflect-cpp/issues) for details.

Please also refer to the *vcpkg.json* in this repository.
Please also refer to the *vcpkg.json* in this repository.

## Simple Example

Expand Down
7 changes: 4 additions & 3 deletions docs/json_schema.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# JSON schema

JSON schemata are a powerful tool for expressing the expected structure of your input. You can use it to validate your input before you even send it to your C++ backend, which will result in better UX.

It can also be used for code generation. For instance, tools such as https://app.quicktype.io/ allow you to generate code in multiple programming languages from the JSON schema (even though the validations are usually omitted).

If you are interacting with Python, we warmly recommend https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/. This allows you to generate Pydantic dataclasses, including the validation, from the JSON schema.
!!! note
If you are interacting with Python, we warmly recommend https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/.
This allows you to generate Pydantic dataclasses, including the validation, from the JSON schema.

Note that this is only supported for JSON, not for other formats.
Note that this is only supported for JSON, not for other formats.

## Basic idea

Expand Down
Binary file added docs/reflect-cpp-head.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/reflect-cpp-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/reflect-cpp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
176 changes: 171 additions & 5 deletions docs/stylesheets/extra.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,173 @@
/* Colors */
/* Define brand */
:root,
[data-md-color-scheme="default"] {
--md-default-bg-color: rgb(250, 250, 250);
--md-primary-fg-color: rgb(255, 255, 255);
--md-primary-fg-color--light: #865ffe;
--md-primary-fg-color--dark: #7a7685;
--md-primary-bg-color: rgba(0, 0, 0, 0.75);
--md-primary-bg-color--light: rgba(0, 0, 0, 0.54);
--md-accent-fg-color: var(--md-primary-fg-color--dark);
--md-accent-fg-color: var(--md-primary-fg-color--dark);
--pg-light-border: rgb(229, 231, 235);
--hb-hero-color: rgb(45, 45, 45);
--md-code-bg-color: #f2f2f2;
--md-typeset-color--mute: hsla(0deg, 0%, 0%, 0.7);
--md-typeset-color--light: var(--md-typeset-color);
}
:root,
[data-md-color-scheme="slate"] {
--md-default-bg-color: rgb(26, 26, 27);
--md-primary-fg-color: rgb(15, 15, 15);
--md-primary-fg-color--light: #865ffe;
--md-primary-fg-color--dark: #fca515;
--md-primary-bg-color: rgba(0, 0, 0, 0.75);
--md-primary-bg-color--light: rgba(0, 0, 0, 0.54);
--md-accent-fg-color: var(--md-primary-fg-color--dark);
--pg-light-border: rgb(47, 47, 47);
--hb-hero-color: var(--md-primary-fg-color--light);

--md-hue: 225deg;
--md-code-bg-color: hsla(var(--md-hue), 15%, 18%, 1);
--md-typeset-color--mute: hsla(var(--md-hue), 15%, 90%, 0.76);

--md-typeset-color--light: hsla(var(--md-hue), 15%, 95%, 0.95);
--md-typeset-color: hsla(var(--md-hue), 15%, 95%, 0.82);
}

/* Better contrast link colors */
[data-md-color-scheme="default"] > * {
--md-typeset-a-color: #6241be;
/* #00B1BB; */
--md-banner-bg: var(--md-typeset-a-color);
}
[data-md-color-scheme="slate"] > * {
--md-typeset-a-color: var(--md-primary-fg-color--light);
--md-banner-bg: #784ff4;
}

/* Hide shadow under navigation */
.md-header[data-md-state="shadow"],
.md-header--shadow {
box-shadow: none;
}

/* Logo mod */

#logo_light_mode {
display: var(--md-footer-logo-light-mode);
}

#logo_dark_mode {
display: var(--md-footer-logo-dark-mode);
}

[data-md-color-scheme="default"] {
--md-footer-logo-dark-mode: none;
--md-footer-logo-light-mode: block;
}

[data-md-color-scheme="slate"] {
--md-footer-logo-dark-mode: block;
--md-footer-logo-light-mode: none;
}

.md-header__button.md-logo img,
.md-header__button.md-logo svg {
height: 1.1rem;
margin-top: 0.2rem;
}

/* header font */

.md-typeset h2 {
font-weight: 600;
}

.md-typeset h2,
.md-typeset h3 {
color: var(--md-typeset-color--light);
}

/* content partial */

.md-typeset {
font-size: 0.85rem;
}

.md-typeset .admonition,
.md-typeset details {
font-size: 0.675rem;
}

.md-typeset code {
font-size: 0.9em;
}

.md-typeset h2 code {
font-size: 0.75em;
}

.md-typeset h1.title-description {
margin-bottom: 0em !important;
}

.md-typeset p.description {
margin-bottom: 1.25em;
color: var(--md-typeset-color--mute);
font-size: 1.15em;
/* font-weight: */
}

.md-typeset hr.description {
height: 1px;
margin-top: 0;
margin-bottom: 3em;
border: none;
background-color: var(--pg-light-border);
}

/* Header - background */

:root {
--md-primary-fg-color: #42A5B9;
--md-primary-fg-color--light: #42A5B9;
--md-primary-fg-color--dark: #7A6A6E;
--md-accent-fg-color: #F0685C;
--md-accent-fg-color--transparent: #F0685C;
--header-backdrop-blur: 10px;
}

.md-header,
.md-tabs {
/* firefox fallback */
background-color: rgba(255, 255, 255, 0.95);
}

[data-md-color-scheme="slate"] .md-header,
[data-md-color-scheme="slate"] .md-tabs {
/* firefox fallback */
background-color: rgba(9, 9, 9, 0.95);
}

@supports ((-webkit-backdrop-filter: none) or (backdrop-filter: none)) {
.md-header,
.md-tabs {
background-color: rgba(255, 255, 255, 0.5);
-webkit-backdrop-filter: blur(var(--header-backdrop-blur));
backdrop-filter: blur(var(--header-backdrop-blur));
}
[data-md-color-scheme="slate"] .md-header,
[data-md-color-scheme="slate"] .md-tabs {
background-color: hsla(0, 0%, 100%, 0.03);
-webkit-backdrop-filter: blur(var(--header-backdrop-blur));
backdrop-filter: blur(var(--header-backdrop-blur));
}
}

/* Nav */

.md-nav {
font-size: 0.73rem;
line-height: 1.25;
}

.md-tabs__link {
font-size: 0.75rem;
}
Loading

0 comments on commit 2892934

Please sign in to comment.