Skip to main content

JSONParser

Latest Version: 1.0.1

This library parses JSON into Squirrel data types.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To include this library in your project, add

#require "JSONParser.class.nut:1.0.1"

at the top of your code.

Usage

JSONParser has no constructor and one public function, parse().

parse(jsonString[, converter])

This method converts the supplied JSON to a table.

Parameters

Parameter Type Required? Description
jsonString String Yes The JSON to parse
converter Function No A function used to convert custom types. See Custom Types Converter, below, for more information

Custom Types Converter

An optional converter function can be passed into parse() to de-serialize custom types. The function has two parameters:

  • value — String representation of a value.
  • type — String indicating conversion type: "string" or "number".

For example, the following code converts all numbers to floats and makes strings uppercase:

result <- JSONParser.parse(jsonString, function (value, type) {
    if (type == "number") {
        return value.tofloat();
    } else if (type == "string") {
        return value.toupper();
    }
});

Return Value

Table — the full de-serialized data.

Basic Example

local jsonString = "{\"one\" : 1}";
result <- JSONParser.parse(jsonString);
server.log(result.one);
// Displays '1'

Extended Example

class MyCustomType {
    _value = null;

    constructor(value) {
        this._value = value;
    }

    function _serialize() {
        return "@mycustomtype:" + this._value;
    }

    function getValue() {
        return this._value;
    }
}

o <- {a = 1, b = "Something", c = MyCustomType("100500") };
s <- JSONEncoder.encode(o);

server.log(s);
// Displays '{"a":1,"c":"@mycustomtype:100500","b":"Something"}'

result <- JSONParser.parse(s, function (value, type) {
    if (type == "number") {
        return value.tofloat();
    } else if (type == "string") {
        if (null != value.find("@mycustomtype")) {
            // Convert my custom type
            value = MyCustomType(value.slice(14))
        }

        return value;
    }
});

server.log(result.c instanceof MyCustomType);
// Displays 'true'

server.log(result.c.getValue());
// Displays '100500'

Testing

This repository contains automated tests that can be run on the command line using impt. For documentation on how to configure and run tests please see the impt testing guide.

Test configuration is stored in the .impt.test file. To run tests locally:

  • Update the deviceGroupId to a Device Group ID from your impCentral account.
  • Use impt commands to log into your impCentral account.
  • Run tests using the impt test run command.

Tests do not require any specific hardware or environment variables. Please do not include modified .impt.test configuration files when submitting pull requests to this repository.

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.0.0 GitHub Table keys with the same name as Table delegate methods no longer cause problems

License

This library is licensed under the MIT License.