JSON on MangOH from file


#1

Hi,

I’m trying to read/parse a json document that i’ve download to the mangOH, does someone have an example of this for legato?

Also, which fd i’m supposed to use?

Thanks,

Pau


#2

I have written a simple Legato app which parses a JSON string and prints out the content. You can see from the filenames below that the app is named “JsonReadWrite”. I didn’t bother implementing the writing part, but it should be fairly straightforward to figure out if you follow the jansson documentation.

JsonReadWrite/jsonReadWrite.adef

version: 0.1.0
sandboxed: true
start: manual

executables:
{
    jsonReadWrite = (jsonReadWrite)
}

processes:
{
    run:
    {
        (jsonReadWrite)
    }
}

JsonReadWrite/jsonReadWrite/Component.cdef

cflags:
{
    -I$LEGATO_BUILD/framework/libjansson/include
}

ldflags:
{
    -ljansson
}

requires:
{
    file:
    {
        /legato/systems/current/lib/libjansson.so /lib/
        /legato/systems/current/lib/libjansson.so.4 /lib/
        /legato/systems/current/lib/libjansson.so.4.6.0 /lib/
    }
}

sources:
{
    main.c
}

JsonReadWrite/jsonReadWrite/main.c

#include <jansson.h>
#include "legato.h"

const char jsonText[] =
    "{\"i2cDevices\":[{\"name\":\"eeprom\",\"bus\":0,\"address\":64},{\"name\":\"temperature sensor\",\"bus\":1,\"address\":20}]}";

COMPONENT_INIT
{
    json_error_t jsonError;
    json_t* root = json_loads(jsonText, 0, &jsonError);
    // NOTE: To read from a file instead, use:
    //       json_t* root = json_load_file("/path/to/file.json", 0, &jsonError);
    LE_FATAL_IF(!root, "JSON decoding error on line (%d): %s", jsonError.line, jsonError.text);

    LE_FATAL_IF(!json_is_object(root), "JSON is not an object");

    json_t* i2cDevices = json_object_get(root, "i2cDevices");
    LE_FATAL_IF(!i2cDevices, "Couldn't get i2cDevices object from JSON");
    LE_FATAL_IF(!json_is_array(i2cDevices), "i2cDevices is not an array");

    size_t index;
    json_t* value;
    json_array_foreach(i2cDevices, index, value)
    {
        LE_FATAL_IF(!json_is_object(value), "i2cDevices index %d is not an object", index);

        json_t* i2cName = json_object_get(value, "name");
        LE_FATAL_IF(
            !i2cName || !json_is_string(i2cName), "name either doesn't exist or is not a string");
        const char* name = json_string_value(i2cName);

        json_t* i2cBus = json_object_get(value, "bus");
        LE_FATAL_IF(
            !i2cBus || !json_is_integer(i2cBus), "bus either doesn't exist or is not an integer");
        const json_int_t bus = json_integer_value(i2cBus);

        json_t* i2cAddress = json_object_get(value, "address");
        LE_FATAL_IF(
            !i2cAddress || !json_is_integer(i2cAddress),
            "address either doesn't exist or is not an integer");
        const json_int_t address = json_integer_value(i2cAddress);

        LE_INFO("I2C device #%d: name=\"%s\", bus=%lld, address=%lld", index, name, bus, address);
    }

    // Reduce the reference count of the root object which should cause the memory to be freed
    json_decref(root);
}