CBOT (Character Based Object Transport)
CBOT (Character Based Object Transport) protocol is a character-based data transfer protocol designed to replace JSON in web development. Its primary goal is to provide a more robust and richer model for transporting structured data between server and browser.
This protocol is not intended to compete with or replace any binary protocols used in server-to-server communications, but rather to create a better alternative. Therefore, the design goals are always aligned with the JavaScript environment in browsers, even when implemented on other platforms.
Key Features
Key features of the protocol include:
- Drop-in replacement for JSON: At the simplest level, CBOT can be used as a drop-in replacement for
JSON.stringify()andJSON.parse(), creating JSON-like behavior. - Line-based format: Straightforward to serialize and deserialize, with each command on its own line
- Machine-readable first: Primarily designed for machine processing but can be visualized using provided tools in assembly-like format.
- Communication focus: Designed for real-time communication where data is immediately deserialized, not for long-term data storage
- Extended data types: CBOT natively supports a broader range of data types, including integer/decimal types, dates, sets, maps, and binary data.
- Type System: Protocol is able to transport type information at protocol level, which enables the transport of polymorphic and complex types. (Referred as Model)
- Web development focus: Optimized for browser-server communication scenarios
- JavaScript alignment: Designed to work with JavaScript's type system and limitations
- Browser compatibility: Works within the constraints and capabilities of web browsers
- Platform independence: While optimized for JavaScript, the protocol can be implemented on any platform
Comparison with JSON
CBOT addresses several key limitations of JSON while maintaining compatibility with JSON-like usage patterns.
| Feature | JSON | CBOT |
|---|---|---|
| Primitive Types | Few basic types | Extended native types |
| Model Validation | Application-level only | Protocol-level validation |
| Date Support | String representation only | Native date/time types |
| Collections | Arrays only | Arrays, Sets, Maps |
| Binary Data | Manual strings | Native byte arrays |
| Message Size | Repetitive property names | Key-ID mapping |
| Extensibility | None | Custom encoders |
| Type support | Plain objects only | Protocol-level support |
Examples
Simple JSON replacement
In JavaScript:
const cbot = Cbot.getInstance();
const john = {
name: 'John Smith',
age: 40
};
const msg = cbot.serialize(john);
console.log(cbot.deserialize(msg));
In Java:
public class Person {
private String name;
private Integer age;
// ...getters/setters
}
public static void main(...) {
var cbot = Cbot.getInstance();
var john = new Person();
john.setName("John Smith");
john.setAge(40);
var msg = cbot.serialize(john);
var john2 = cbot.deserialize(msg, Person.class);
}
Typed version
In typescript:
const NS = Namespace.of('example');
@NS.class('Person')
class Person {
@Value.string()
name: string
@Value.int32()
age: number
}
const cbot = Cbot.getInstance({ namespaces: [NS] });
const john = new Person();
john.name = 'John Smith';
john.age = 40;
const msg = cbot.serialize(john);
const john2:Person = cbot.deserialize(msg);
console.log(john2);
In Java:
package fi.sisujs.example;
public class Person {
private String name;
private Integer age;
// ...getters/setters
}
public static void main(...) {
var ns = Namespace.of("example", "fi.sisujs.example");
var cbot = Cbot.getInstance(
OptionsBuilder
.create()
.withNamespaces(List.of(ns))
.build());
var john = new Person();
john.setName("John Smith");
john.setAge(40);
var msg = cbot.serialize(john);
var john2 = (Person) cbot.deserialize(msg);
}
Language Support
TypeScript/JavaScript support
Although the driving force to create CBOT comes from the world of TypeScript, it is not exclusively for TypeScript. The format is language-agnostic and can be implemented on any platform.
However, when language support is added for other languages, developers need to understand that CBOT also inherits the same freedoms that TypeScript provides. There can be "anytype" objects, or arrays may contain different kinds of objects that do not relate to each other in any way. There are very few restrictions on how the model can be constructed.
Java Support
Currently, there is also a Java library that supports all capabilities that CBOT offers. However, it does not need any external meta-model to work because Java's reflection capabilities are sufficient.
Other languages are not yet supported.