Module serialization

Module serialization 

Source
Expand description

§MON AST to Serializable Value Conversion

This module provides the logic for converting a resolved MON Abstract Syntax Tree (AST) into a generic, serializable data structure. This is the final step in the pipeline before the MON data can be output as JSON, YAML, or any other format supported by serde.

§Architectural Overview

The process is straightforward:

  1. A fully resolved and validated MonValue from the AST is passed to the internal to_value function.
  2. The function recursively traverses the MonValue, converting it into a tree of Value enums.
  3. During this conversion, language-specific AST nodes that are not part of the data model—such as TypeDefinition members—are discarded. Only Pair members are included in the final object.
  4. The resulting Value is designed to be directly serializable by serde. It uses a BTreeMap for objects to ensure that the output has a deterministic order of keys, which is good practice for configuration and data files.

§Use Cases

This module is used internally by AnalysisResult to provide the to_json() and to_yaml() methods. Direct interaction with this module is typically not necessary for end-users, as the public API in the api module provides a more convenient interface.

use mon_core::api::analyze;

let source = "{ b: 2, a: 1 }";

// The serialization module is used behind the scenes by `to_json`.
let result = analyze(source, "test.mon")?;
let json = result.to_json().unwrap();

// Note that the keys are sorted alphabetically because of the BTreeMap.
assert_eq!(json, "{\n  \"a\": 1.0,\n  \"b\": 2.0\n}");

Enums§

Value