JSON

Description

OSL provides powerful and flexible JSON handling with several unique features:

  • 1-based array indexing

  • Dynamic JSON creation with variables and expressions

  • Built-in merging operations

  • Support for all OSL data types

  • Copy-based operations for data safety

JSON Creation

Basic Syntax

// Simple object
data = {
    name: "John",
    age: 25 * 2,  // Expressions are evaluated
    items: ["a", "b", "c"]
}

// Object with computed values
multiplier = 10
obj = {
    base: 5,
    computed: self.base * multiplier,  // References other properties
    dynamic: "pre" ++ "fix"  // String concatenation
}

// Object with methods
calculator = {
    value: 100,
    double: def() -> (
        return self.value * 2
    )
}

Merging with ++

The ++ operator merges arrays, objects, or strings:

Type Support

OSL JSON can contain any valid OSL type:

  • Numbers (integer and float)

  • Strings

  • Booleans

  • Arrays (1-indexed)

  • Objects

  • Functions

  • null

Important Notes

  • Arrays are 1-indexed (first element is at index 1)

  • Most operations create new copies rather than modifying in place

  • The ++ operator preserves values from the left operand on key conflicts

  • JSON objects can contain functions and reference their own properties with self

  • Expressions and variables in JSON are evaluated when the object is created

  • Nested objects and arrays are supported to any depth

  • All OSL data types can be stored in JSON structures

Last updated

Was this helpful?