The encoding module provides functions for working with encoding formats like JSON and YAML.

Allowed in

All functions

Usage

Load the module:

load("github.com/ocuroot/sdk/v0/encoding.star", "json", "yaml")

Example

# JSON encoding/decoding
json_str = json.encode({"foo": "bar"})
json_data = json.decode(json_str)
print(json_data)  # {"foo": "bar"}

# YAML encoding/decoding
yaml_str = yaml.encode({"foo": "bar"})
yaml_data = yaml.decode(yaml_str)
print(yaml_data)  # {"foo": "bar"}

API Reference

json.encode(data)

Encodes the given value to JSON and returns the result as a string.

Arguments

  • data: The value to encode (can be dict, list, string, number, bool, or None)

Returns

  • JSON string representation of the data

json.decode(data)

Decodes the provided JSON string and returns the resulting value.

Arguments

  • data: JSON string to decode

Returns

  • Decoded value (dict, list, string, number, bool, or None)

yaml.encode(data)

Encodes the given value to YAML and returns the result as a string.

Arguments

  • data: The value to encode (can be dict, list, string, number, bool, or None)

Returns

  • YAML string representation of the data

yaml.decode(data)

Decodes the provided YAML string and returns the resulting value.

Arguments

  • data: YAML string to decode

Returns

  • Decoded value (dict, list, string, number, bool, or None)

Stubs

def make_json():
    def encode(data):
        pass   

    def decode(data):
        pass

    return struct(
        encode = encode,
        decode = decode,
    )

def make_yaml():
    def encode(data):
        pass   

    def decode(data):
        pass

    return struct(
        encode = encode,
        decode = decode,
    )

json = make_json()
yaml = make_yaml()