Json in Scilab

// Scilab offers built-in support for JSON encoding and decoding,
// including to and from built-in and custom data types.

// First, let's import the necessary JSON module
jsonlib = atomsLoad("json");

// We'll use these two structs to demonstrate encoding and
// decoding of custom types below.
function response1 = createResponse1(page, fruits)
    response1 = struct('Page', page, 'Fruits', fruits);
endfunction

// Only exported fields will be encoded/decoded in JSON.
// In Scilab, all struct fields are accessible.
function response2 = createResponse2(page, fruits)
    response2 = struct('page', page, 'fruits', fruits);
endfunction

// Let's look at encoding basic data types to JSON strings.
// Here are some examples for atomic values.
mprintf("%s\n", jsonlib.encode(%t));
mprintf("%s\n", jsonlib.encode(1));
mprintf("%s\n", jsonlib.encode(2.34));
mprintf("%s\n", jsonlib.encode("gopher"));

// And here are some for arrays and structs, which encode
// to JSON arrays and objects as you'd expect.
slcD = ["apple", "peach", "pear"];
mprintf("%s\n", jsonlib.encode(slcD));

mapD = struct("apple", 5, "lettuce", 7);
mprintf("%s\n", jsonlib.encode(mapD));

// The JSON module can automatically encode your
// custom data types.
res1D = createResponse1(1, ["apple", "peach", "pear"]);
mprintf("%s\n", jsonlib.encode(res1D));

// In Scilab, we don't have direct control over the JSON key names
// like in Go with struct tags. The field names will be used as is.
res2D = createResponse2(1, ["apple", "peach", "pear"]);
mprintf("%s\n", jsonlib.encode(res2D));

// Now let's look at decoding JSON data into Scilab values.
// Here's an example for a generic data structure.
byt = '{"num":6.13,"strs":["a","b"]}';

// We can decode JSON directly into a Scilab struct
dat = jsonlib.decode(byt);
disp(dat);

// Accessing the decoded data is straightforward
mprintf("%f\n", dat.num);
mprintf("%s\n", dat.strs(1));

// We can also decode JSON into custom data types.
str = '{"page": 1, "fruits": ["apple", "peach"]}';
res = jsonlib.decode(str);
disp(res);
mprintf("%s\n", res.fruits(1));

// In Scilab, we don't have direct streaming capabilities for JSON
// like in Go. However, we can write the JSON string to a file.
d = struct("apple", 5, "lettuce", 7);
json_str = jsonlib.encode(d);
fd = mopen('output.json', 'wt');
mputl(json_str, fd);
mclose(fd);

// Read back the JSON from the file
fd = mopen('output.json', 'rt');
json_str = mgetl(fd, 1);
mclose(fd);
mprintf("%s\n", json_str);

This Scilab code demonstrates JSON encoding and decoding, mirroring the functionality of the original Go example as closely as possible. Here are some key points:

  1. Scilab doesn’t have a built-in JSON library, so we use the json module from ATOMS (Scilab’s package manager).

  2. Structs in Scilab are used to represent complex data structures, similar to Go’s structs.

  3. Scilab doesn’t have the concept of unexported fields or struct tags for JSON customization. All struct fields are accessible and their names are used as JSON keys.

  4. The jsonlib.encode() and jsonlib.decode() functions are used for JSON encoding and decoding, respectively.

  5. Scilab doesn’t have a direct equivalent to Go’s streaming JSON capabilities. Instead, we demonstrate writing JSON to a file and reading it back.

  6. Error handling in Scilab is different from Go. In this example, we’ve omitted error checking for simplicity, but in a real application, you would want to add appropriate error handling.

  7. The output of this script will be similar to the Go version, showing various JSON encoding and decoding operations.

Remember to install the JSON module using ATOMS before running this script:

atomsInstall("json");

This example covers the basics of JSON in Scilab, demonstrating encoding, decoding, and working with various data types.