Search code examples
cazure-iot-hubazure-iot-sdk

azure-iot-sdk-c deserialize JSON payload


I'm getting started with the Azure IoT SDK C-Library by using the IoT_Hub Device Method samples.

Does anybody know if the SDK provides a function to deserialize the JSON message payload? There are some good examples for payload serialization to JSON, but I wasn't able to find the counterpart.

I'm quite sure the SDK has some functionalities for JSON deserialization, e.g. something like:

BEGIN_NAMESPACE(WeatherStation);

DECLARE_MODEL(ContosoAnemometer, 
  WITH_DATA(ascii_char_ptr, DeviceId), 
  WITH_DATA(int, WindSpeed), 
  WITH_DATA(float, Temperature), 
  WITH_DATA(float, Humidity), 
  WITH_ACTION(TurnFanOn)
); 

END_NAMESPACE(WeatherStation);


static int DeviceMethodCallback(const char* method_name, const unsigned char* payload, ...) 
{ 
    (void)userContextCallback; 

    ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
    DESERIALIZE(myWeather, payload)

    printf("Device Id: %i\r\n", myWeather->DeviceId);

    // etc. etc.
}

Solution

  • In the Azure IoT SDK for C,it uses serializer library to serialize messages to send, but there is no couterpart(DESERIALIZE) for receiving messages.Please see more about serializer. In serializer component, you will find jsondecoder.c, it defines some method for decode json data.

    And more, you will find the parson component. It can be used to deserialize the object from json string.You can refer to the following code.

    jsonstr

    {"desString":"P7svG%2BLoUZEbB8Le5jP1%2BpuX2OVWLE4xWMzFcYFCRvNvDlcFVm%2B8z2VFO%2F%2BaEB8UAMa%2FZ0GGhooGNMWFE98Zmw%3D%3D","caller":{"mobile":"13996130361"},"accountId":"b5091dc91d700c6bf714e5fc446797d3","tmpTime":1428545890,"otherParam":"{id:12}","participant":[{"mobile":"18623582801"}],"servletUrl":"http://192.168.1.1:8090/xdr"}
    

    C Code

    JSON_Value *root_value=NULL;
    root_value = json_parse_string(jsonstr);
    JSON_Object *root_object;
    if(json_value_get_type(root_value) == JSONObject)
    {
        root_object = json_value_get_object(root_value);
        const char *desString = json_object_get_string(root_object, "desString");
        if(desString!=NULL)strncpy(pPost->desString,desString,LENGTH128-1);
        const char *accountId = json_object_get_string(root_object, "accountId");
        if(accountId!=NULL)strncpy(pPost->accountId,accountId,LENGTH32);
        double tmpTime = json_object_get_number(root_object, "tmpTime");
        const char *caller = json_object_dotget_string(root_object,"caller.mobile");
        if(caller!=NULL)strncpy(pPost->caller,caller,LENGTH16-1);
        const char *servletUrl = json_object_dotget_string(root_object,"servletUrl");
        if(servletUrl!=NULL)strncpy(pPost->servletUrl,servletUrl,LENGTH128-1);
        const char *otherParam = json_object_dotget_string(root_object,"otherParam");
        if(otherParam!=NULL)strncpy(pPost->otherParam,otherParam,LENGTH128-1);
    
        JSON_Array *array;
        array = json_object_get_array(root_object, "participant");
        int i =0;
        for(i;i<json_array_get_count(array);i++)
        {
            JSON_Object *tmp = json_array_get_object(array,i);
            if(tmp!=NULL)
            {
                const char *mobile = json_object_get_string(tmp,"mobile");
                if(mobile!=NULL)strncpy(pPost->callee,mobile,LENGTH16-1);
            }
        }
    }
    if(root_value)json_value_free(root_value);