pub enum NxValue {
Bool(bool),
Int32(i32),
Int(i64),
Float32(f32),
Float(f64),
String(String),
Null,
Array(Vec<NxValue>),
Record {
type_name: Option<String>,
properties: BTreeMap<String, NxValue>,
},
}Expand description
A JSON-like tree value used as the stable NX API value type.
It is the Rust form of a canonical value, which docs/nx-ir-format.md (Host values) defines:
a host builds one and passes it as it is, whether it read the value from JSON or computed it,
and JSON and MessagePack are encodings of it for a wire or a store. It can hold nothing that is
not a canonical value, so a runtime given one has nothing to refuse for its kind.
Constant union cases are represented as NxValue::String carrying the bare authored case
name. The declaring union is recovered from the target schema (declared NX type, typed DTO
property, or other type annotation) at the point where the value is consumed; the IR itself
carries no in-payload union type wrapper.
NX has no null: its absent value is the empty sequence, NxValue::empty. NxValue::Null
is the host’s spelling of absence, which NX reads as the empty value. NX writes it in two
places: an entry call’s result whose type is a standalone T? and holds nothing, and a
present empty field of an update record — a cleared field — which is how key presence carries
“cleared” through JSON. An absent optional record field is an omitted key.
Variants§
Bool(bool)
Int32(i32)
Int(i64)
Float32(f32)
Float(f64)
String(String)
String value.
A string value may represent either a plain string or a constant union case. The two are distinguishable only against the target schema — a schema-less consumer cannot tell them apart from the payload alone.
Null
The host’s null: JSON null, MessagePack nil. NX reads it as the empty value.
Array(Vec<NxValue>)
Record
Record value (ordered properties).
When serialized to JSON, type_name is encoded as a "$type" string property if present.
Implementations§
Source§impl NxValue
impl NxValue
Sourcepub fn is_empty_value(&self) -> bool
pub fn is_empty_value(&self) -> bool
True for the empty value, or for a host null, which NX reads as the empty value.
Sourcepub fn from_json_str(source: &str) -> Result<Self, Error>
pub fn from_json_str(source: &str) -> Result<Self, Error>
Deserialize a value from a JSON string.
Sourcepub fn from_json_reader<R: Read>(reader: R) -> Result<Self, Error>
pub fn from_json_reader<R: Read>(reader: R) -> Result<Self, Error>
Deserialize a value from a JSON reader.
Sourcepub fn from_json_file(path: impl AsRef<Path>) -> Result<Self, NxValueIoError>
pub fn from_json_file(path: impl AsRef<Path>) -> Result<Self, NxValueIoError>
Deserialize a value from a JSON file.
Sourcepub fn to_json_string(&self) -> Result<String, Error>
pub fn to_json_string(&self) -> Result<String, Error>
Serialize a value to a compact JSON string.
Sourcepub fn to_json_string_pretty(&self) -> Result<String, Error>
pub fn to_json_string_pretty(&self) -> Result<String, Error>
Serialize a value to a pretty JSON string.
Sourcepub fn to_json_writer<W: Write>(&self, writer: W) -> Result<(), Error>
pub fn to_json_writer<W: Write>(&self, writer: W) -> Result<(), Error>
Serialize a value to JSON using the provided writer.
Sourcepub fn to_json_file(&self, path: impl AsRef<Path>) -> Result<(), NxValueIoError>
pub fn to_json_file(&self, path: impl AsRef<Path>) -> Result<(), NxValueIoError>
Serialize a value to a JSON file (compact form).
Sourcepub fn to_json_file_pretty(
&self,
path: impl AsRef<Path>,
) -> Result<(), NxValueIoError>
pub fn to_json_file_pretty( &self, path: impl AsRef<Path>, ) -> Result<(), NxValueIoError>
Serialize a value to a JSON file (pretty form).
Sourcepub fn from_msgpack_slice(bytes: &[u8]) -> Result<Self, Error>
pub fn from_msgpack_slice(bytes: &[u8]) -> Result<Self, Error>
Deserialize a value from MessagePack bytes.