int luna_service_message_get_int(jvalue_ref parsed_obj, const char *name, int default_value) { jvalue_ref int_obj = NULL; int value = 0; if (!jobject_get_exists(parsed_obj, j_str_to_buffer(name, strlen(name)), &int_obj) || !jis_number(int_obj)) return default_value; jnumber_get_i32(int_obj, &value); return value; }
static bool set_volume_cb(LSHandle *handle, LSMessage *message, void *user_data) { struct audio_service *service = user_data; const char *payload; jvalue_ref parsed_obj = NULL; jvalue_ref volume_obj = NULL; struct luna_service_req_data *req; int new_volume = 0; if (!service->context_initialized) { luna_service_message_reply_custom_error(handle, message, "Not yet initialized"); return true; } payload = LSMessageGetPayload(message); parsed_obj = luna_service_message_parse_and_validate(payload); if (jis_null(parsed_obj)) { luna_service_message_reply_error_bad_json(handle, message); goto cleanup; } if (!jobject_get_exists(parsed_obj, J_CSTR_TO_BUF("volume"), &volume_obj) || !jis_number(volume_obj)) { luna_service_message_reply_error_bad_json(handle, message); goto cleanup; } jnumber_get_i32(volume_obj, &new_volume); if (new_volume < 0 || new_volume > 100) { luna_service_message_reply_custom_error(handle, message, "Volume out of range. Must be in [0;100]"); goto cleanup; } if (service->new_volume == service->volume) { luna_service_message_reply_custom_error(handle, message, "Provided volume doesn't differ from current one"); goto cleanup; } req = luna_service_req_data_new(handle, message); req->user_data = service; set_volume(service, service->new_volume, req); cleanup: if (!jis_null(parsed_obj)) j_release(&parsed_obj); return true; }
bool _CatalogHandleCancel(_Catalog *catalog, LSMessage *cancelMsg, LSError *lserror) { JSchemaInfo schemaInfo; jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL); const char *sender; int token; jvalue_ref tokenObj = NULL; const char *payload = LSMessageGetPayload(cancelMsg); jvalue_ref object = jdom_parse(j_cstr_to_buffer(payload), DOMOPT_NOOPT, &schemaInfo); if (jis_null(object)) { _LSErrorSet(lserror, MSGID_LS_INVALID_JSON, -EINVAL, "Invalid json"); goto error; } sender = LSMessageGetSender(cancelMsg); if (!jobject_get_exists(object, J_CSTR_TO_BUF("token"), &tokenObj) || tokenObj == NULL || !jis_number(tokenObj)) { _LSErrorSet(lserror, MSGID_LS_INVALID_JSON, -EINVAL, "Invalid json"); goto error; } (void)jnumber_get_i32(tokenObj, &token);/* TODO: handle appropriately */ char *uniqueToken = g_strdup_printf("%s.%d", sender, token); _CatalogCallCancelNotifications(catalog, uniqueToken); _CatalogRemoveToken(catalog, uniqueToken, true); g_free(uniqueToken); j_release(&object); return true; error: j_release(&object); return false; }
ConversionResultFlags JValue::asNumber<int32_t>(int32_t& number) const { return jnumber_get_i32(m_jval, &number); }