Internet Object
Welcome to the official JavaScript repository for Internet Object, a lean, robust, and schema-first data interchange format designed for the Internet. As a well-structured alternative to JSON, Internet Object offers a user-friendly API for JavaScript and TypeScript, making it effortless to work with Internet Object documents.
For specification and more information, visit InternetObject.org Docs.
🚧 Work In Progress - API MAY CHANGE
Example Usage
The example below illustrates the usage of Internet Object for parsing a basic internet object document. Please be aware that the API is still in development and has not been officially released. This is purely for demonstration purposes.
Parsing strings into documents
import { ioDocument } from 'internet-object';
const doc = ioDocument`
name, age, gender, address: {street, city, state, zip}
---
~ John, 25, M, {123 Main St, Anytown, CA, 12345}
~ Jane, 30, F, {456 Main St, Anytown, CA, 12345}`;
console.log(doc);
Parsing with separate definitions
import { ioDefinitions, ioDocument } from 'internet-object';
const defs = ioDefinitions`
~ @red: 0xff0000
~ @blue: 0x0000ff
~ @green: 0x00ff00
~ $schema: {
name, age, gender, color, address: {street, city, state, zip}
}`;
const doc = ioDocument.with(defs)`
~ John, 25, M, @green, {123 Main St, Anytown, CA, 12345}
~ Jane, 30, F, @blue, {456 Main St, Anytown, CA, 12345}`;
Working with documents
import { Collection, Document, InternetObject } from 'internet-object';
const collection = new Collection();
collection.push(
new InternetObject("John Doe", 25, "M", "@green", new io.Object("123 Main St", "Anytown", "CA", "12345")),
new InternetObject("Jane Doe", 30, "F", "@blue", new io.Object("456 Main St", "Anytown", "CA", "12345")),
)
const doc = new Document();
doc.data.pushToFirst(collection);
Building Objects
In Internet Object, objects serve as the fundamental building blocks. There are numerous ways to create these objects. Below are a few examples.
const o1 = new InternetObject("John Doe", 25, "M", "@green", new io.Object("123 Main St", "Anytown", "CA", "12345"));
const arr = [ "John Doe", 25, "M", "@green", new io.Object("123 Main St", "Anytown", "CA", "12345") ]
const o2 = new io.Object(...arr);
const o3 = io.object`John Doe, 25, M, @green, {123 Main St, Anytown, CA, 12345}`;
const o4 = InternetObject.import({
name: "John Doe",
age: 25,
gender: "M",
color: "@green",
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345"
}
})
Validate Document with external schema
Often, before transmitting a document to a remote location, it's crucial to validate the document against a predefined schema or definition. If the validation process detects an invalid value, it will throw a ValidationError
.
try {
doc.validate(defs);
console.log("Document is valid");
} catch (e) {
console.log(e);
}
Core Tokenization and Parsing Interfaces
During the tokenization and parsing process, if an error is encountered, an exception is promptly thrown. This exception pinpoints the exact location of the error. Furthermore, the exception may also detail the specific token that instigated the error.
const tokens = io.parser.tokenize(code)
const ast = io.parser.parse(tokens)
const doc = io.parser.compile(ast)
const defs = io.parser.compileDefs(ast)
Work in Progress Status
Development Process
🚧 Pull requests are currently not accepted. Development is ongoing, and the API is still under finalization. Once the API is stable, we will welcome contributions. Please note that the following instructions are for reference only.
- Fork repository from https://github.com/maniartech/InternetObject-js
- Install dependencies
yarn install
- Make changes in
./src
- Update tests in
./tests/
- Run tests,
yarn test
- Send pull request(s)
For a comprehensive understanding of Internet Object, refer to the official specification available at docs.InternetObject.org.
ISC License:
© 2018-2024 ManiarTech®️. All rights reserved.