This addon extends kOS with simple JSON functionality, allowing you to serialize kOS data structures to basic JSON strings and parse plain JSON strings back into kOS structures. It uses the SimpleJson library internally for efficient JSON processing. It also exposes helper suffixes to validate JSON strings and return safe fallbacks when parsing fails, so scripts stay running.
While kOS provides its own READJSON and WRITEJSON functions which allow complete serialization and deserialization without loss of types, this addon provides the functionality to read and write plain JSON data without the need for custom types.
The only drawback is that when converting a structure to and from JSON, it might not be the same type anymore. See the section about supported type conversions for more info.
PARSEORELSE, PARSEORELSEGET, and ISPARSEABLEUse CKAN or install it manually:
Your directory structure should look like:
KSP_ROOT/
└─ GameData/
└─ kOS-simpleJson/
├─ kOS-simpleJson.dll
├─ kOS-simpleJson.version
├─ LICENSE
└─ README
The addon provides several functions accessible through the base path ADDONS:JSON:
Converts a kOS structure to a JSON string. Works with kOS-serializable structures (Lexicons, Lists, primitives, PID loops, ranges). Keys of object-like structures must be strings.
// Stringify a lexicon
SET myLex TO LEXICON("name", "Rocket", "altitude", 1000, "active", True).
SET jsonString TO ADDONS:JSON:STRINGIFY(myLex).
PRINT jsonString.
// Output: {"name":"Rocket","altitude":1000,"active":true}
// Stringify a list
SET myList TO LIST(1, 2, 3, "four").
SET jsonString TO ADDONS:JSON:STRINGIFY(myList).
PRINT jsonString.
// Output: [1,2,3,"four"]
// Stringify primitives
PRINT ADDONS:JSON:STRINGIFY(42). // Output: 42
PRINT ADDONS:JSON:STRINGIFY("hello"). // Output: "hello"
PRINT ADDONS:JSON:STRINGIFY(True). // Output: true
Checks if a structure can be serialized to JSON without throwing.
SET myLex TO LEXICON(0, "<-- invalid key", "name", "Rocket", "altitude", 1000).
IF ADDONS:JSON:ISSTRINGIFIABLE(myLex) {
SET jsonString TO ADDONS:JSON:STRINGIFY(myLex).
PRINT jsonString.
} ELSE {
PRINT "Structure cannot be serialized".
}
Parses a JSON string into a kOS structure. Throws if the JSON is invalid.
// Parse JSON object
SET jsonString TO "{""name"":""Rocket"",""altitude"":1000}".
SET myLex TO ADDONS:JSON:PARSE(jsonString).
PRINT myLex["name"]. // Output: Rocket
PRINT myLex["altitude"]. // Output: 1000
// Parse JSON array
SET jsonString TO "[1,2,3,4,5]".
SET myList TO ADDONS:JSON:PARSE(jsonString).
PRINT myList[0]. // Output: 1
// Parse primitives
PRINT ADDONS:JSON:PARSE("42"). // Output: 42
PRINT ADDONS:JSON:PARSE("true"). // Output: True
Parses a JSON string, or returns the provided fallback if parsing fails.
SET badJson TO "{""name"":""Rocket""".
SET fallback TO LEXICON("name", "Fallback", "active", False).
SET data TO ADDONS:JSON:PARSEORELSE(badJson, fallback).
PRINT data["name"]. // Output: Fallback
Parses a JSON string, or calls a delegate to produce a fallback when parsing fails. The delegate function is only called if parsing the given JSON fails.
DECLARE FUNCTION BuildDefault {
RETURN LEXICON("status", "unknown", "tries", 1).
}.
SET maybeJson TO "}not-json{".
SET data TO ADDONS:JSON:PARSEORELSEGET(maybeJson, BuildDefault@).
PRINT data["status"]. // Output: unknown
Checks if a string can be parsed as JSON without throwing in kOS.
SET candidate TO "{""value"":1}".
IF ADDONS:JSON:ISPARSEABLE(candidate) {
PRINT ADDONS:JSON:PARSE(candidate).
} ELSE {
PRINT "Invalid JSON".
}
| kOS Type | JSON Type |
|---|---|
| String | string |
| Number (integer) | number (integer) |
| Number (floating point) | number (float) |
| Boolean | boolean |
| any List-like | array |
| Lexicon | object |
| all others | object |
| JSON Type | kOS Type |
|---|---|
| string | String |
| number (integer) | Number |
| number (float) | Number |
| boolean | Boolean |
| array | List |
| object | Lexicon |
| null | empty String ("") |
// Simulating an API response
SET apiResponse TO "{""vessel"":{""name"":""Explorer 1"",""mass"":5000,""parts"":25}}".
SET data TO ADDONS:JSON:PARSE(apiResponse).
PRINT "Vessel: " + data["vessel"]["name"].
PRINT "Mass: " + data["vessel"]["mass"].
// Create configuration
SET config TO LEXICON(
"launchAzimuth", 90,
"targetAltitude", 80000,
"stages", LIST(
LEXICON("fuel", 100, "engines", 1),
LEXICON("fuel", 200, "engines", 2)
)
).
// Save to file
SET jsonConfig TO ADDONS:JSON:STRINGIFY(config).
LOG jsonConfig TO "0:/config.json".
// Load from file
SET loadedJson TO OPEN("0:/config.json"):READALL:STRING.
SET loadedConfig TO ADDONS:JSON:PARSE(loadedJson).
The addon implements the IFormatWriter interface from kOS.Safe.Serialization, using the SimpleJsonFormatter class to handle serialization. Deserialization is handled by the JsonDeserializer, skipping the conversion to dumps. The main entry point is the SimpleJsonAddon class, which is decorated with the [kOSAddon("JSON")] attribute to register it with kOS.
Raw stats are from the beginning of time until now. Each follower and download entry represents one hour of data. Uneventful hours are omitted.