#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "v1_selectable_field.h"



v1_selectable_field_t *v1_selectable_field_create(
    char *json_path
    ) {
    v1_selectable_field_t *v1_selectable_field_local_var = malloc(sizeof(v1_selectable_field_t));
    if (!v1_selectable_field_local_var) {
        return NULL;
    }
    v1_selectable_field_local_var->json_path = json_path;

    return v1_selectable_field_local_var;
}


void v1_selectable_field_free(v1_selectable_field_t *v1_selectable_field) {
    if(NULL == v1_selectable_field){
        return ;
    }
    listEntry_t *listEntry;
    if (v1_selectable_field->json_path) {
        free(v1_selectable_field->json_path);
        v1_selectable_field->json_path = NULL;
    }
    free(v1_selectable_field);
}

cJSON *v1_selectable_field_convertToJSON(v1_selectable_field_t *v1_selectable_field) {
    cJSON *item = cJSON_CreateObject();

    // v1_selectable_field->json_path
    if (!v1_selectable_field->json_path) {
        goto fail;
    }
    if(cJSON_AddStringToObject(item, "jsonPath", v1_selectable_field->json_path) == NULL) {
    goto fail; //String
    }

    return item;
fail:
    if (item) {
        cJSON_Delete(item);
    }
    return NULL;
}

v1_selectable_field_t *v1_selectable_field_parseFromJSON(cJSON *v1_selectable_fieldJSON){

    v1_selectable_field_t *v1_selectable_field_local_var = NULL;

    // v1_selectable_field->json_path
    cJSON *json_path = cJSON_GetObjectItemCaseSensitive(v1_selectable_fieldJSON, "jsonPath");
    if (!json_path) {
        goto end;
    }

    
    if(!cJSON_IsString(json_path))
    {
    goto end; //String
    }


    v1_selectable_field_local_var = v1_selectable_field_create (
        strdup(json_path->valuestring)
        );

    return v1_selectable_field_local_var;
end:
    return NULL;

}
