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

Was this helpful?

  1. Book
  2. Data Interchange

JSON to Stof

Turning a JSON document into a Stof document.

For defining data, Stof is a superset of JSON - valid JSON will always be valid Stof.

This is a kind of funny page to create since Stof can parse JSON. However, explicitly showing how Stof ties into an existing interchange format is helpful for later, when we add more complex custom interfaces and unify data.

{
    "resourceType" : "Patient",
    "id" : "example",
    "active" : true,
    "name" : [
        {
            "use" : "official",
            "family" : "Chalmers",
            "given" : ["Peter", "James"]
        },
        {
            "use" : "usual",
            "given" : ["Jim"]
        },
        {
            "use" : "maiden",
            "family" : "Windsor",
            "given" : ["Peter", "James"],
            "period" : {
                "end" : "2002"
            }
        }
    ]
}

To start, the outer braces are irrelevant in Stof, where there is always an object/scope. The outermost object is named "root" and is the main root of the Stof document.

"resourceType" : "Patient",
"id" : "example",
"active" : true,
"name" : [
    {
        "use" : "official",
        "family" : "Chalmers",
        "given" : ["Peter", "James"]
    },
    {
        "use" : "usual",
        "given" : ["Jim"]
    },
    {
        "use" : "maiden",
        "family" : "Windsor",
        "given" : ["Peter", "James"],
        "period" : {
            "end" : "2002"
        }
    }
]

Field declarations in Stof can optionally be double-quoted, single-quoted, or without quotes altogether.

Also, declarations can end with a comma, semi-colon, or some whitespace (space or newline).

resourceType: 'Patient', // comma
id: 'example';           // semi-colon
active: true             // whitespace
name: [
    {
        use: "official",
        family: "Chalmers",
        given: ["Peter", "James"]
    },
    {
        use: "usual",
        given: ["Jim"]
    },
    {
        use: "maiden",
        family: "Windsor",
        given: ["Peter", "James"],
        period: {
            end: "2002"
        }
    }
]
str resourceType: "Patient"
str id: "example"
bool active: true

type Name {
    use: str; // semi-colon or comma optional
    family: str = "";
    given: vec = [];
    period: obj = null; // could add more complex types, etc.
}

vec name: [
    {
        use: "official",
        family: "Chalmers",
        given: ["Peter", "James"]
    } as Name, // would otherwise be "obj"
    {
        use: "usual",
        given: ["Jim"]
    } as Name,
    {
        use: "maiden",
        family: "Windsor",
        given: ["Peter", "James"],
        period: {
            end: "2002"
        }
    } as Name,
]

Now let us turn this example into an interface that can do something by adding a function. We'll add a #[main] function and use the CLI to test that this is working as expected.

/**
 * Data definition.
 */
resourceType: "Patient"
id: "example"
active: true
name: [
    {
        use: "official",
        family: "Chalmers",
        given: ["Peter", "James"]
    },
    {
        use: "usual",
        given: ["Jim"]
    },
    {
        use: "maiden",
        family: "Windsor",
        given: ["Peter", "James"],
        period: {
            end: "2002"
        }
    }
]

/**
 * Interface definition.
 */
type Name {
    use: str; // semi-colon or comma optional
    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 example.stof
Peter James Chalmers
Jim
Peter James Windsor

The CLI is for testing and using Stof in a non-embedded capacity. This functionality also exists by simply parsing your Stof into TypeScript or wherever you like to work with data. Keep in mind though, that in different contexts, Stof might not have access to the system, network, or outside world in general unless the host environment allows/enables it.

PreviousData InterchangeNextImporting and Parsing Data

Last updated 3 months ago

Was this helpful?

Copy this code and place it in a file called "example.stof". Next, make sure to install the , and then invoke it using the "run" command. You will see the following printed to your console:

Stof CLI