Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page
  • Import Statement
  • Parse Function

Was this helpful?

  1. Book
  2. Data Interchange

Importing and Parsing Data

Using data defined outside of Stof.

PreviousJSON to StofNextExporting Data

Last updated 4 months ago

Was this helpful?

The vast majority of data in the world is not defined as Stof (shocking, I know). Stof is not the standard and never will be. Rather, Stof works with all other data formats so that you can work with data in the form you prefer (or the form it's already in).

Stof Formats are dynamic and extensible, allowing users to import, export, and parse data in a wide variety of forms.

The simplest way to show this is with the . Any format that is loaded into the document can be used to parse data into a document.

Import Statement

is a popular configuration file format and comes baked into Stof by default. In this example, we'll import a TOML document and add an interface to it with Stof.

The same import capability exists when embedding Stof in other host environments and contexts, however, Stof is sandboxed by default and does not have access to importing files unless explicitly enabled.

Using the same interface we defined in JSON to Stof, we can use the same data in a different form (TOML instead of JSON).

import "./file.toml";

type Name {
    use: str;
    family: str = "";
    given: vec = [];
    period: obj = null;

    fn name(): str {
        return `${self.given.join(" ")} ${self.family}`;
    }
}

#[main]
fn main() {
    for (Name name in self.name) {
        pln(name.name());
    }
}
> stof run ./interface.stof
Peter James Chalmers
Jim
Peter James Windsor
active = true
id = "example"
resourceType = "Patient"

[[name]]
family = "Chalmers"
given = ["Peter", "James"]
use = "official"

[[name]]
given = ["Jim"]
use = "usual"

[[name]]
family = "Windsor"
given = ["Peter", "James"]
use = "maiden"

[name.period]
end = "2002"

Parse Function

type Name {
    use: str;
    family: str = "";
    given: vec = [];
    period: obj = null;

    fn name(): str {
        return `${self.given.join(" ")} ${self.family}`;
    }
}

#[main]
fn main() {
    let json = '{"active":true,"id":"example","name":[{"family":"Chalmers","given":["Peter","James"],"use":"official"},{"given":["Jim"],"use":"usual"},{"family":"Windsor","given":["Peter","James"],"period":{"end":"2002"},"use":"maiden"}],"resourceType":"Patient"}';
    
    // parses this JSON string into the current object (self) using the 'json' format
    parse(json, 'json');

    for (Name name in name) {
        pln(name.name());
    }
}

Using the , we can run this document, resulting in the same output as before:

Additional files and the import statement are not the only way data can enter a document. The parse function in the is a very powerful additional way to do this. Stof can even parse in additional Stof programmatically with the parse function, enabling documents to parse in remote data interfaces.

import statement
TOML
Stof CLI
Standard Library