Skip to content

Introduce jomini::Value for loosely typed data - #184

Open
nickbabcock wants to merge 1 commit into
masterfrom
value3
Open

Introduce jomini::Value for loosely typed data#184
nickbabcock wants to merge 1 commit into
masterfrom
value3

Conversation

@nickbabcock

@nickbabcock nickbabcock commented Jun 16, 2025

Copy link
Copy Markdown
Contributor

🚧 👷 WIP code and WIP description👷 🚧


This PR introduces jomini::Value, the analog serde_json::Value for jomini.

jomini::Value serves a variety of use cases.

untyped data

When reading data where you have no inkling of the structure of a field, and need to dynamically access and manipulate values.

Whereas with a TextTape, any edits to the underlying tokens (if such an API were to exist) risks corrupting object and array indices, jomini::Value is structured as a DOM tree, allowing for easier modifications.

It is not recommended to reach for jomini::Value when the structure of values can vary simply, as this use case is often covered by implementing Deserialize over an enum. For instance, the following data would not be a good use for jomini::Value

hello = "world"

# or

hello = { val = "world" }

as it can be represented by:

enum MaybeObject {
    Text(String)
    Object { val: String }
}

impl<'de> Deserialize<'de> for MaybeObject {
    // ...
}

lossless data

owned data

drawbacks

encoding issues

Performance

Write API

Example

We can write use jomini::Value to construct an eager variable interpolation program, which can then be deserialized into strongly typed object.

For instance, given:

@my_var = 10
my_obj = {
  pos_x = @[100-my_var]
  pos_y = @my_var
}

We deserialize it as:

my_obj = {
  pos_x = 90
  pos_y = 10
}

This is accomplished via the following steps:

  • deserialize the input into a jomini::Value
  • Make a pass over the value to collect all the variables
  • Make another pass to interpolate values and update the underlying structure
  • Finally we can either print out the resulting structure or deserialize a value from it.

TODO: code to follow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant