# rmp::Value

> A tree: nothing, a bool, a number, a string, a list or an object -- exactly what JSON holds, so a real structure fits and not only flat pairs.

Source: https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html

class `#include <rmp/save.h>` [include/rmp/save.h:64](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L64-L221)

class Value

A tree: nothing, a bool, a number, a string, a list or an object — exactly what JSON holds, so a real structure fits and not only flat pairs.

### `Type`

enum class Type

enum [include/rmp/save.h:70](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L70)

What a Value holds, one kind per JSON value. NONE is nothing: a missing key, an index out of range, a default-constructed Value, and JSON's null. BOOL is true or false, NUMBER a double, STRING text. LIST is values in order, and OBJECT is keys naming values, in the order they were added.

| Value |  | Means |
|---|---|---|
| `NONE` |  |  |
| `BOOL` |  |  |
| `NUMBER` |  |  |
| `STRING` |  |  |
| `LIST` |  |  |
| `OBJECT` |  |  |

### `Value`

template <class B> requires std::is_same_v<B, bool> // NOLINTNEXTLINE(google-explicit-constructor): v["alive"] = true; a bool is a Value Value(B b)

template <class N> requires(std::is_arithmetic_v<N> && !std::is_same_v<N, bool>) // NOLINTNE

template <class S> requires(std::is_convertible_v<const S &, std::string_view> && !std::is_pointer_v<S> && !std::is_null_pointer_v<S>) // NOLINTNEXTLINE(goo

function [include/rmp/save.h:76](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L76-L79)

A template so that only a real bool gets here: as a plain `Value(bool)`, every pointer in the program would quietly convert to `true`.

### `list`, `object`

static Value list()

static Value object()

static function [include/rmp/save.h:100](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L100)

An empty list or object, for when "nothing yet" has to be one of them: v["inventory"] = [`rmp::Value::list()`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html#list);

## Reading: never throws, never changes anything

### `as_bool`, `as_int`, `as_float`

bool as_bool(bool fallback = false) const

int as_int(int fallback = 0) const

float as_float(float fallback = 0) const

function [include/rmp/save.h:109](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L109)

The fallback when this is not of that type — a missing key is NONE. Types are not converted: a number is not a bool and "7" is not 7. as_int truncates towards zero and clamps to int's range; as_float clamps to float's, so a number in a save never reads as an infinity.

### `as_string`

std::string_view as_string(std::string_view fallback = "") const

function [include/rmp/save.h:114](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L114)

Valid while this Value is alive and unchanged — and adding to the object or list that holds it moves it. The fallback's lifetime is yours.

### `type`

Type type() const

function [include/rmp/save.h:118](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L118)

What this holds. Reading never needs it — a read of the wrong type gives its fallback — but walking a Value you did not write does.

### `size`

int size() const

function [include/rmp/save.h:122](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L122)

Elements of a list, keys of an object; 0 for anything else. A member assigned NONE counts as absent, here and in `key()`, `contains()`, == and the file: nothing and missing read the same.

### `contains`

bool contains(std::string_view key) const

function [include/rmp/save.h:124](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L124)

Whether this is an object with that key. False for anything else.

### `key`

std::string_view key(int index) const

function [include/rmp/save.h:127](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L127)

The i-th key of an object, in the order the keys were added; "" out of range. With operator[](key) it is how to walk an object you did not write.

## Structure

### `operator[]`

const Value &operator[](std::string_view key) const

const Value &operator[](int index) const

Ref operator[](std::string_view key)

Ref operator[](int index)

function [include/rmp/save.h:142](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L142)

[] on a const Value reads: a missing key, an index out of range, or a Value of another type is NONE.

[] on a non-const Value gives a Ref — a path into this Value that READS exactly like the const one, creating nothing, and WRITES by creating what is missing on the way: v["a"]["b"].`as_int(1)` leaves v as it was, v["a"]["b"] = 1 builds both objects. So reading a save never grows it, and a read never says anything. A write that cannot land — through a number read from an old save, at a negative index, past the end of a list (`size()` appends one) — goes nowhere and is said once: turning a number into an object behind your back would lose it.

### `push`

void push(Value value)

function [include/rmp/save.h:149](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L149)

Append. NONE becomes a list first; anything else that is not a list is left alone and said once.

### `erase`

bool erase(std::string_view key)

function [include/rmp/save.h:151](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L151)

Remove a key from an object. False when it was not there.

### `version`

int version() const

function [include/rmp/save.h:155](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L155)

The `[save] version` this came from, when [`rmp::save::read()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-save.html#read) filled it; a Value made in code is of this build's version. Not compared by ==.

### `changed`

bool changed() const

function [include/rmp/save.h:171](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L171)

A Value read with [`rmp::assets::load_value()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-assets.html#load_value) remembers its file in resources/: `changed()` is true once the file differs from what this Value holds — edited and saved while the game runs — and `reload()` reads it again into this Value, which keeps remembering it:

```cpp
if (tuning.changed()) tuning.reload();
speed = tuning["speed"].as_float(200);
```

Debug builds on the desktop only: a release reads its resources from the pack, which does not change, and everywhere else `changed()` is false. Cheap enough for every frame: `changed()` compares two numbers, and the file itself is looked at four times a second at most. A Value made in code, read from a save or taken out of another (tuning["enemy"]) has no file: `changed()` is false and `reload()` does nothing. Neither is compared by ==.

### `reload`

bool reload()

function [include/rmp/save.h:174](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L174)

Reads the file again into this Value; false, and this Value as it was, when it has no file or the file is not JSON now (half saved, say).

### `operator==`

bool operator==(const Value &a, const Value &b)

function [include/rmp/save.h:178](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L178)

Same type and same contents, keys in any order. A true equality: a NaN equals a NaN, so a Value always equals itself and every copy of it.

## `Value::Ref`

class [include/rmp/save.h:233](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L233-L301)

What [] on a non-const Value gives: the root it started from and the keys and indices it went through, looked up again on every use — so a Ref kept while other keys are added never points at moved memory. You do not name this type; you index, read and assign through it:

```cpp
v["player"]["x"] = 3;                 // creates "player"
int x = v["player"]["x"].as_int(0);   // creates nothing
const rmp::Value &p = v["player"];    // the Value itself, to read
```

To fill a whole subtree, build it and assign it: v["player"] = player;

### `get`

const Value &get() const

function [include/rmp/save.h:236](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L236)

Reading: the Value this names, or NONE where the path leads nowhere.

### `operator const Value &`

operator const Value &() const

function [include/rmp/save.h:240](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L240)

The Value it names, wherever a `const rmp::Value &` is wanted: a Ref passes where a Value is read.

### `as_bool`, `as_int`, `as_float`, `as_string`, `type`, `size`, `contains`, `key`

bool as_bool(bool fallback = false) const

int as_int(int fallback = 0) const

float as_float(float fallback = 0) const

std::string_view as_string(std::string_view fallback = "") const

Type type() const

int size() const

bool contains(std::string_view key) const

std::string_view key(int index) const

function [include/rmp/save.h:242](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L242-L244)

Value's reads, answered from `get()`: they create nothing.

### `operator[]`

Ref operator[](std::string_view key) const

Ref operator[](int index) const

function [include/rmp/save.h:260](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L260)

Deeper, still creating nothing.

### `operator=`

Ref &operator=(Value value)

function [include/rmp/save.h:265](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L265)

Writing: the path is created, then assigned. A Ref assigned from a Ref copies the VALUE, never the path — v["a"] = v["b"] copies b into a.

### `push`, `erase`

void push(Value value)

bool erase(std::string_view key)

function [include/rmp/save.h:279](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L279)

Value's `push()` and `erase()` on the Value this names. `push()` creates the path first, like a write; `erase()` creates nothing and is false when the path leads nowhere.
