Skip to content

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.

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.

{
"user": {
"name": "Alice",
"email": "alice@example.com",
"settings": {
"theme": "dark",
"notifications": true
}
}
}

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/off in addition to true/false.
  • Trailing Commas: Allowed in lists and objects, preventing common copy-paste errors.

JSON has no built-in mechanism for reusing data. Any repeated structure or value must be duplicated, leading to verbose and error-prone files.

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 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.

JSON relies on external technologies like JSON Schema for data validation. This means an additional layer, separate files, and often more complex tooling.

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 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: #struct and #enum are 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.

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 offers a straightforward module system for organizing multi-file configurations and sharing types/data.

config.mon
// Import definitions from a separate 'schemas.mon' file
import * as schemas from "./schemas.mon"
{
db_config :: schemas.Database = {
host: "localhost",
port: 5432,
},
}

MON Advantages:

  • Native Imports: import statements 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.

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.