Chapter 1: Introduction to BEJSON 104 – The Genesis of a Standard
The Genesis and Purpose of BEJSON 104
Standard JSON, while versatile and widely adopted, inherently lacks built-in mechanisms for strict data validation and explicit schema definition. Data structures can vary wildly between JSON files, requiring external schema validation (e.g., JSON Schema) and leading to fragile systems where data inconsistencies can easily occur. This absence of internal structure is analogous to the "cascade problem" observed in CSS, where lack of architectural discipline leads to unpredictable behavior. BEJSON 104 was conceived to mitigate these issues by embedding its schema directly within the document, enforcing structural integrity from the outset.
The primary purpose of BEJSON 104 is to provide a single-entity store with a self-describing, rigidly defined structure suitable for portable documentation and CMS environments. It enforces a predictable data matrix where every record adheres to a declared set of fields, preventing common errors such as missing keys, type mismatches, or positional shifts that plague less structured data formats. This makes BEJSON 104 exceptionally reliable for storing critical configurations, content entities, or any dataset where structural consistency is paramount.
Core Structure and Advantages Over Standard JSON
BEJSON 104 introduces mandatory top-level keys that define the document's structure, type, and content. These keys are Format, Format_Version, Format_Creator, Records_Type, Fields, and Values. This rigid structure provides several advantages over standard JSON:
- Self-Describing Schema: The
Fieldsarray explicitly defines the name and type of each data column. This eliminates ambiguity and the need for external schema files, making the BEJSON 104 document entirely self-contained and interpretable. - Positional Integrity: The
Valuesarray holds arrays of data, where each inner array's elements correspond positionally to theFieldsarray. This ensures that data points are consistently located, even if a field is null. The mandate to usenullfor absent data, rather than omitting the field, maintains the matrix's integrity. As stated in the universal requirements, "Structural Nulls:nullmust be used to preserve the matrix for any absent data; field shifting is a hard validation failure." - Predictable Validation: With an internal schema and positional integrity, validation of BEJSON 104 documents can be highly efficient and deterministic. This is crucial for applications where data consistency is non-negotiable.
- Optimized Field Access: The explicit
Fieldsarray allows for optimized lookups. Libraries such aslib_bejson_core.jsimplement field map caching (bejson_core_get_field_map,bejson_core_get_field_index), enabling O(1) access to field indices once the map is generated. This significantly improves performance compared to iterating through object keys in conventional JSON. Thebejson_cache.test.jsdemonstrates this functionality:const doc = { Format_Version: "104", Fields: [{name: "id"}, {name: "name"}, {name: "value"}], Values: [] }; console.assert(BEJSON.bejson_core_get_field_index(doc, "name") === 1, "Field 'name' index should be 1");
BEJSON 104 Schema and Code Example
A BEJSON 104 document, by definition, adheres to a specific schema. Consider an example for storing a list of products:
{
"Format": "BEJSON",
"Format_Version": "104",
"Format_Creator": "Elton Boehnen",
"Records_Type": ["Product"],
"Fields": [
{ "name": "product_id", "type": "string" },
{ "name": "product_name", "type": "string" },
{ "name": "price", "type": "number" },
{ "name": "in_stock", "type": "boolean" },
{ "name": "description", "type": "string" }
],
"Values": [
["PROD-001", "Laptop Pro X", 1200.00, true, "High-performance laptop."],
["PROD-002", "Mechanical Keyboard", 85.50, true, "Tactile gaming keyboard."],
["PROD-003", "Wireless Mouse", 25.99, false, null]
]
}
In this example:
Records_Typecontains["Product"], indicating this document stores a single type of entity.Fieldsdefines five columns, each with anameandtype.Valuescontains arrays where each element corresponds to a field in theFieldsarray. Notice thenullfor thedescriptionof "Wireless Mouse", maintaining the array length and positional integrity.
Creating such a document programmatically is facilitated by the BEJSON core libraries, as seen in lib_bejson_Core_bejson_bejson.js:
BEJSON = {
// ... other methods ...
create104(recordType, fields, values) {
return {
Format: "BEJSON",
Format_Version: "104",
Format_Creator: "Elton Boehnen",
Records_Type: [recordType],
Fields: fields,
Values: values
};
},
// ... other methods ...
};
This create104 function directly implements the mandatory structure for BEJSON 104, ensuring new documents are conformant. The Format_Creator being "Elton Boehnen" is a non-negotiable requirement across all BEJSON formats to establish authoritative origin.
BEJSON 104's strict, self-describing nature and its explicit focus on single-entity storage differentiate it from the more flexible, but less structurally enforced, standard JSON. It serves as the bedrock upon which the entire BEJSON ecosystem is built, providing a reliable and predictable format for structured data documentation.