# cp.json

A collection of handy JSON tools.


# API Overview

Functions - API calls offered directly by the extension

  • decode
  • encode
  • prop
  • read
  • write

# API Documentation

# Functions

# decode

Signature cp.json.decode(jsonString) -> table
Type Function
Description Decodes JSON into a table
Parameters
  • jsonString - A string containing some JSON data
Returns
  • A table representing the supplied JSON data
Notes
  • This is useful for retrieving some of the more complex lua table structures as a persistent setting (see hs.settings)
Examples None
Source src/extensions/cp/json/init.lua line 83

# encode

Signature cp.json.encode(val[, prettyprint]) -> string
Type Function
Description Encodes a table as JSON
Parameters
  • val - A table containing data to be encoded as JSON
  • prettyprint - An optional boolean, true to format the JSON for human readability, false to format the JSON for size efficiency. Defaults to false
Returns
  • A string containing a JSON representation of the supplied table
Notes
  • This is useful for storing some of the more complex lua table structures as a persistent setting (see hs.settings)
Examples None
Source src/extensions/cp/json/init.lua line 66

# prop

Signature cp.json.prop(path, folder, filename, defaultValue[, errorCallbackFn]) -> cp.prop
Type Function
Description Returns a cp.prop instance for a JSON file.
Parameters
  • path - The path to the JSON folder (i.e. "~/Library/Caches")
  • folder - The folder containing the JSON file (i.e. "Final Cut Pro")
  • filename - The filename of the JSON file (i.e. "Test.json")
  • defaultValue - The default value if the JSON file doesn't exist yet.
  • errorCallbackFn - An optional function that's triggered if something goes wrong.
Returns
  • A cp.prop instance.
Notes
  • The optional errorCallbackFn should accept one parameter, a string with
  • the error message.
Examples None
Source src/extensions/cp/json/init.lua line 99

# read

Signature cp.json.read(path) -> table | nil
Type Function
Description Attempts to read the specified path as a JSON file. If the file cannot be found, nil is returned. If the file is not a JSON file, an error will occur.
Parameters
  • path - The JSON file path.
Returns
  • The JSON file converted into table, or nil.
Notes None
Examples None
Source src/extensions/cp/json/init.lua line 20

# write

Signature cp.json.write(path, data) -> boolean
Type Function
Description Writes data to a JSON file.
Parameters
  • path - The path to where you want to save the JSON file.
  • data - A table containing data to be encoded as JSON.
Returns
  • true if successfully saved, otherwise false.
Notes None
Examples None
Source src/extensions/cp/json/init.lua line 40