MON vs. JSON
JSON (JavaScript Object Notation) has been the de facto standard for data serialization and configuration for a long time due to its simplicity and widespread support. However, for complex configuration files and scenarios demanding higher readability, safety, and reusability, MON (Mycel Object Notation) emerges as a powerful and modern JSON alternative.
This document outlines the key differences and advantages of MON over JSON.
1. Readability and Syntax
Section titled “1. Readability and Syntax”JSON’s syntax is concise but can become cumbersome for humans to read and write, especially with nested structures, strict quote requirements, and lack of comments.
JSON Example
Section titled “JSON Example”{ "user": { "name": "Alice", "email": "alice@example.com", "settings": { "theme": "dark", "notifications": true } }}MON Example
Section titled “MON Example”MON prioritizes human readability with features often missing in JSON.
{ // Comments make configurations self-documenting user: { name: "Alice", email: "alice@example.com", settings: { theme: "dark", notifications: on, // Booleans as 'on'/'off' are more natural }, },}MON Advantages:
- Comments: Explicit support for single-line
//comments. - Unquoted Keys: Keys can be unquoted unless they contain special characters or spaces, reducing visual noise.
- Flexible Booleans: Uses
on/offin addition totrue/false. - Trailing Commas: Allowed in lists and objects, preventing common copy-paste errors.
2. Data Reusability and Templating
Section titled “2. Data Reusability and Templating”JSON has no built-in mechanism for reusing data. Any repeated structure or value must be duplicated, leading to verbose and error-prone files.
JSON (No Native Reusability)
Section titled “JSON (No Native Reusability)”To reuse data in JSON, you typically rely on external tools or duplicate the data.
{ "default_settings": { "theme": "dark", "notifications": true }, "user_alice": { "name": "Alice", "settings": { "theme": "dark", "notifications": true } }, "user_bob": { "name": "Bob", "settings": { "theme": "dark", "notifications": false } }}MON (Anchors, Aliases, Spreads)
Section titled “MON (Anchors, Aliases, Spreads)”MON provides powerful first-class features for data reuse, making MON configurations incredibly DRY (Don’t Repeat Yourself).
{ // Define a reusable template for default settings &default_settings: { theme: "dark", notifications: on, },
user_alice: { name: "Alice", // Spread the default settings and keep them ...*default_settings, },
user_bob: { name: "Bob", // Spread the default settings and override notifications ...*default_settings, notifications: off, },}MON Advantages:
- Anchors (
&): Define reusable data blocks or fragments. - Aliases (
*): Create independent, deep copies of anchored values. - Spreads (
...*): Merge and extend anchored objects or arrays, allowing for powerful templating and overriding. - Reduced Duplication: Significantly cuts down on verbose, repeated data.
3. Integrated Schema Validation
Section titled “3. Integrated Schema Validation”JSON relies on external technologies like JSON Schema for data validation. This means an additional layer, separate files, and often more complex tooling.
JSON (External Schema)
Section titled “JSON (External Schema)”Requires a separate .json or .yaml file to define the schema, validated by an external parser.
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "User", "type": "object", "properties": { "id": { "type": "number" }, "username": { "type": "string" } }, "required": ["id", "username"]}MON (Built-in Type System)
Section titled “MON (Built-in Type System)”MON integrates schema validation directly into the language, using #struct and #enum to define types and the :: operator for validation.
{ // Define the schema for a User within the MON file itself User: #struct { id(Number), username(String), is_active(Boolean) = true, },
// Validate a user instance against the defined schema alice :: User = { id: 1, username: "alice", is_active: on, },
// This instance would fail validation because 'id' is a String, not a Number // bob :: User = { // id: "2", // username: "bob", // },}MON Advantages:
- First-Class Types:
#structand#enumare part of the core language, not an afterthought. - Integrated Validation: The
::operator allows direct validation within the MON file. - Improved Data Integrity: Catch errors during development, before they reach runtime.
- Clear Error Messages: MON compiler provides precise error reporting.
4. Module System for Large Projects
Section titled “4. Module System for Large Projects”JSON files are typically standalone. Managing multi-file JSON configurations for large projects can be cumbersome, relying on ad-hoc tools or merge scripts.
MON (Native Module System)
Section titled “MON (Native Module System)”MON offers a straightforward module system for organizing multi-file configurations and sharing types/data.
// Import definitions from a separate 'schemas.mon' fileimport * as schemas from "./schemas.mon"
{ db_config :: schemas.Database = { host: "localhost", port: 5432, },}MON Advantages:
- Native Imports:
importstatements allow for explicit dependency management. - Code Organization: Easily split large configurations into logical, manageable files.
- Shared Definitions: Share types, templates, and data blocks across your project.
Conclusion
Section titled “Conclusion”While JSON remains excellent for simple data interchange, MON presents itself as a powerful evolution, particularly for defining robust and maintainable configuration languages and complex data structures. By offering superior readability, native data reusability, integrated schema validation, and a module system, MON provides a compelling JSON alternative for modern software development.
Consider adopting MON for your next project if you value clarity, data integrity, and efficiency in your data management.