/*
 Parson ( http://kgabis.github.com/parson/ )
 Copyright (c) 2012 - 2015 Krzysztof Gabis

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "parson.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "TZ_Vendor_tl.h"
//#include "paypal_api.h"
//#include "mc_utils_tl.h"
#define DBG_DUMP(...)  //invalidate DUMPSTRING.
#define DBG_LOG(...)  //invalidate DUMPSTRING.

#define STARTING_CAPACITY       30
#define ARRAY_MAX_CAPACITY    122880 /* 15*(2^13) */
#define OBJECT_MAX_CAPACITY      960 /* 15*(2^6)  */
#define MAX_NESTING               19
#define DOUBLE_SERIALIZATION_FORMAT "%f"

#define SIZEOF_TOKEN(a)       (sizeof(a) - 1)
#define SKIP_CHAR(str)        ((*str)++)
#define SKIP_WHITESPACES(str) while (my_isspace(**str)) { SKIP_CHAR(str); }
#define PARSON_MAX(a, b)             ((a) > (b) ? (a) : (b))

#undef malloc
#undef free

#define MAX_JSON_PARSER_BUF_SIZE  60*1024 //Keep it around 1.5*DC_TA_PROVISION_CMD_TOKEN_DATA_LEN

static int my_isspace(int c) {
    if((c>= 9 && c<13) || c == 32) {
        return 8;
    }
    return 0;
}

static unsigned char g_buf[MAX_JSON_PARSER_BUF_SIZE];
static unsigned int  g_buf_idx = 0;

void
json_parser_init(void)
{
    //TZ_bzero(g_buf, sizeof(g_buf)); //pebble doesn't have TZ_bzero
    memset(g_buf, 0, sizeof(g_buf));
    g_buf_idx = 0;
}

void
json_parser_reset(void)
{
    json_parser_init();
}

void *
dcpay_dummy_malloc (size_t size)
{
    void * ret = NULL;

    if (!size)
    {
        TTY_LOG("Invalid input params");
        return NULL;
    }

    /* Keep it 4-byte aligned */
    if (size % 4)
    {
        uint32_t extra_bytes = 4 - (size % 4);
        if (size + extra_bytes < size)
        {
            TTY_LOG("Integer Overflow in size");
            return NULL;
        }
        size += extra_bytes;
    }

    if (g_buf_idx + size < g_buf_idx)
    {
        TTY_LOG("Integer Overflow");
        return NULL;
    }

    if (g_buf_idx + size >= sizeof(g_buf))
    {
        TTY_LOG("Max bufsize reached, size = %d, g_buf_idx = %d, g_buf_size = %d", size, g_buf_idx, sizeof(g_buf));
        return NULL;
    }
    else
    {
        ret = g_buf + g_buf_idx;
        g_buf_idx += size;
    }

    return ret;
}

#if 0
void
dcpay_dummy_free (void* ptr)
{
    /*No OP */
}
#endif

static JSON_Malloc_Function parson_malloc = dcpay_dummy_malloc;
//static JSON_Free_Function parson_free = dcpay_dummy_free;

#define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */

/* Type definitions */
typedef union json_value_value {
    char        *string;
    double       number;
    JSON_Object *object;
    JSON_Array  *array;
    int          boolean;
    int          null;
} JSON_Value_Value;

struct json_value_t {
    JSON_Value_Type     type;
    JSON_Value_Value    value;
};

struct json_object_t {
    char       **names;
    JSON_Value **values;
    size_t       count;
    size_t       capacity;
};

struct json_array_t {
    JSON_Value **items;
    size_t       count;
    size_t       capacity;
};

/* Various */
static char * parson_strndup(const char *string, size_t n);
static char * parson_strdup(const char *string);
static int    is_utf16_hex(const unsigned char *string);
static int    num_bytes_in_utf8_sequence(unsigned char c);
static int    verify_utf8_sequence(const unsigned char *string, int *len);
static int    is_valid_utf8(const char *string, size_t string_len);
static int    is_decimal(const char *string, size_t length);

/* JSON Object */
static JSON_Object * json_object_init(void);
static JSON_Status   json_object_add(JSON_Object *object, const char *name, JSON_Value *value, bool use_strdup);
static JSON_Status   json_object_resize(JSON_Object *object, size_t new_capacity);
static JSON_Value  * json_object_nget_value(const JSON_Object *object, const char *name, size_t n);
static void          json_object_free(JSON_Object *object);

/* JSON Array */
static JSON_Array * json_array_init(void);
static JSON_Status  json_array_add(JSON_Array *array, JSON_Value *value);
static JSON_Status  json_array_resize(JSON_Array *array, size_t new_capacity);
static void         json_array_free(JSON_Array *array);

/* JSON Value */
static JSON_Value * json_value_init_string_no_copy(char *string);

/* Parser */
static JSON_Status  skip_quotes(const char **string);
static int          parse_utf_16(const char **unprocessed, char **processed);
static char *       process_string(const char *input, size_t len);
static char *       get_quoted_string(const char **string);
static JSON_Value * parse_object_value(const char **string, size_t nesting);
static JSON_Value * parse_array_value(const char **string, size_t nesting);
static JSON_Value * parse_string_value(const char **string);
static JSON_Value * parse_boolean_value(const char **string);
static JSON_Value * parse_number_value(const char **string);
static JSON_Value * parse_null_value(const char **string);
static JSON_Value * parse_value(const char **string, size_t nesting);

/* Serialization */
static int    json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf);
static int    json_serialize_string(const char *string, char *buf);
static int    append_indent(char *buf, int level);
static int    append_string(char *buf, const char *string);

/* Various */
static char * parson_strndup(const char *string, size_t n) {
    char *output_string = (char*)parson_malloc(n + 1);
    if (!output_string) {
        TTY_LOG("parson_malloc failed for output_string, %d", n+1);
        return NULL;
    }
    output_string[n] = '\0';
    strncpy(output_string, string, n);
    return output_string;
}

static char * parson_strdup(const char *string) {
    return parson_strndup(string, strlen(string));
}

static int is_utf16_hex(const unsigned char *s) {
    return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]);
}

static int num_bytes_in_utf8_sequence(unsigned char c) {
    if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) {
        return 0;
    } else if ((c & 0x80) == 0) {    /* 0xxxxxxx */
        return 1;
    } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */
        return 2;
    } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */
        return 3;
    } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */
        return 4;
    }
    return 0; /* won't happen */
}

static int verify_utf8_sequence(const unsigned char *string, int *len) {
    unsigned int cp = 0;
    *len = num_bytes_in_utf8_sequence(string[0]);

    if (*len == 1) {
        cp = string[0];
    } else if (*len == 2 && IS_CONT(string[1])) {
        cp = string[0] & 0x1F;
        cp = (cp << 6) | (string[1] & 0x3F);
    } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) {
        cp = ((unsigned char)string[0]) & 0xF;
        cp = (cp << 6) | (string[1] & 0x3F);
        cp = (cp << 6) | (string[2] & 0x3F);
    } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) {
        cp = string[0] & 0x7;
        cp = (cp << 6) | (string[1] & 0x3F);
        cp = (cp << 6) | (string[2] & 0x3F);
        cp = (cp << 6) | (string[3] & 0x3F);
    } else {
        TTY_LOG("Invalid length");
        return 0;
    }

    /* overlong encodings */
    if ((cp < 0x80    && *len > 1) ||
            (cp < 0x800   && *len > 2) ||
            (cp < 0x10000 && *len > 3)) {
        TTY_LOG("overlong encodings");
        return 0;
    }

    /* invalid unicode */
    if (cp > 0x10FFFF) {
        TTY_LOG("Invalid Unicode");
        return 0;
    }

    /* surrogate halves */
    if (cp >= 0xD800 && cp <= 0xDFFF) {
        TTY_LOG("Surrogate Havles");
        return 0;
    }

    return 1;
}

static int is_valid_utf8(const char *string, size_t string_len) {
    int len = 0;
    const char *string_end =  string + string_len;
    while (string < string_end) {
        if (!verify_utf8_sequence((const unsigned char*)string, &len)) {
            TTY_LOG("verify_utf8_sequence failed");
            return 0;
        }
        string += len;
    }
    return 1;
}

static int is_decimal(const char *string, size_t length) {
    if (length > 1 && string[0] == '0' && string[1] != '.')
        return 0;
    if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.')
        return 0;
    while (length--)
        if (strchr("xX", string[length]))
            return 0;
    return 1;
}

/* JSON Object */
static JSON_Object * json_object_init(void) {
    JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
    if (!new_obj) {
        TTY_LOG("parson_malloc failed for new_object, %d", sizeof(JSON_Object));
        return NULL;
    }
    new_obj->names = (char**)NULL;
    new_obj->values = (JSON_Value**)NULL;
    new_obj->capacity = 0;
    new_obj->count = 0;
    DBG_LOG("json_object_init: returned new_obj 0x%x",new_obj);
    return new_obj;
}

static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value, bool use_strdup) {
    size_t index = 0;
    if (object == NULL || name == NULL || value == NULL) {
        TTY_LOG("Invalid arguments");
        return JSONFailure;
    }
    if (json_object_get_value(object, name) != NULL) {
        TTY_LOG("json_object_get_value didn't fail. duplicate entry %s", name);
        return JSONFailure;
    }
    if (object->count >= object->capacity) {
        size_t new_capacity = PARSON_MAX(object->capacity * 2, STARTING_CAPACITY);
        if (new_capacity > OBJECT_MAX_CAPACITY) {
            TTY_LOG("new_capacity > OBJECT_MAX_CAPACITY");
            return JSONFailure;
        }
        if (json_object_resize(object, new_capacity) == JSONFailure) {
            TTY_LOG("json_object_resize failed");
            return JSONFailure;
        }
    }
    index = object->count;
    if (use_strdup) {
        char *temp = parson_strdup(name);
        if (temp == NULL)
        {
            TTY_LOG("parson_strdup failed");
            return JSONFailure;
        }
        object->names[index] = temp;
    }
    else
        object->names[index] = (char *)name;
    if (object->names[index] == NULL) {
        TTY_LOG("object->names[%d] == NULL", index);
        return JSONFailure;
    }
    object->values[index] = value;
    object->count++;
    return JSONSuccess;
}

static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) {
    char **temp_names = NULL;
    JSON_Value **temp_values = NULL;

    if ((object->names == NULL && object->values != NULL) ||
            (object->names != NULL && object->values == NULL) ||
            new_capacity == 0) {
        TTY_LOG("Invalid arguments");
        return JSONFailure; /* Shouldn't happen */
    }

    temp_names = (char**)parson_malloc(new_capacity * sizeof(char*));
    if (temp_names == NULL) {
        TTY_LOG("parson_malloc failed for temp_names, %d", new_capacity * sizeof(char*));
        return JSONFailure;
    }

    temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
    if (temp_values == NULL) {
        TTY_LOG("parson_malloc failed for temp_values, %d", new_capacity * sizeof(JSON_Value*));
        //parson_free(temp_names);
        return JSONFailure;
    }

    if (object->names != NULL && object->values != NULL && object->count > 0) {
        memcpy(temp_names, object->names, object->count * sizeof(char*));
        memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*));
    }
    //parson_free(object->names);
    //parson_free(object->values);
    object->names = temp_names;
    object->values = temp_values;
    object->capacity = new_capacity;
    return JSONSuccess;
}

static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) {
    size_t i, name_length;
    for (i = 0; i < json_object_get_count(object); i++) {
        name_length = strlen(object->names[i]);
        if (name_length != n)
            continue;
        if (strncmp(object->names[i], name, n) == 0)
            return object->values[i];
    }
    return NULL;
}

static void json_object_free(JSON_Object *object) {
    while(object->count--) {
        //parson_free(object->names[object->count]);
        json_value_free(object->values[object->count]);
    }
    //parson_free(object->names);
    //parson_free(object->values);
    //parson_free(object);
}

/* JSON Array */
static JSON_Array * json_array_init(void) {
    JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
    if (!new_array) {
        TTY_LOG("parson_malloc failed for new_array, %d", sizeof(JSON_Array));
        return NULL;
    }
    new_array->items = (JSON_Value**)NULL;
    new_array->capacity = 0;
    new_array->count = 0;
    return new_array;
}

static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
    if (array->count >= array->capacity) {
        size_t new_capacity = PARSON_MAX(array->capacity * 2, STARTING_CAPACITY);
        if (new_capacity > ARRAY_MAX_CAPACITY) {
            TTY_LOG("new_capacity > ARRAY_MAX_CAPACITY");
            return JSONFailure;
        }
        if (json_array_resize(array, new_capacity) == JSONFailure) {
            TTY_LOG("json_array_resize failed");
            return JSONFailure;
        }
    }
    array->items[array->count] = value;
    array->count++;
    return JSONSuccess;
}

static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) {
    JSON_Value **new_items = NULL;
    if (new_capacity == 0) {
        TTY_LOG("Invalid arguments");
        return JSONFailure;
    }
    new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
    if (new_items == NULL) {
        TTY_LOG("parson_malloc failed for new_items, %d", new_capacity * sizeof(JSON_Value*));
        return JSONFailure;
    }
    if (array->items != NULL && array->count > 0) {
        memcpy(new_items, array->items, array->count * sizeof(JSON_Value*));
    }
    //parson_free(array->items);
    array->items = new_items;
    array->capacity = new_capacity;
    return JSONSuccess;
}

static void json_array_free(JSON_Array *array) {
    while (array->count--)
        json_value_free(array->items[array->count]);
    //parson_free(array->items);
    //parson_free(array);
}

/* JSON Value */
static JSON_Value * json_value_init_string_no_copy(char *string) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) {
        TTY_LOG("parson_malloc failed for new_value, %d", sizeof(JSON_Value));
        return NULL;
    }
    new_value->type = JSONString;
    new_value->value.string = string;
    return new_value;
}

/* Parser */
static JSON_Status skip_quotes(const char **string) {
    if (**string != '\"') {
        return JSONFailure;
    }
    SKIP_CHAR(string);
    while (**string != '\"') {
        if (**string == '\0') {
            return JSONFailure;
        } else if (**string == '\\') {
            SKIP_CHAR(string);
            if (**string == '\0') {
                return JSONFailure;
            }
        }
        SKIP_CHAR(string);
    }
    SKIP_CHAR(string);
    return JSONSuccess;
}

static int parse_utf_16(const char **unprocessed, char **processed) {
    unsigned int cp, lead, trail;
    char *processed_ptr = *processed;
    const char *unprocessed_ptr = *unprocessed;
    unprocessed_ptr++; /* skips u */
    if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) {
        TTY_LOG("is_utf16_hex failed or EOF");
        return JSONFailure;
    }
    if (cp < 0x80) {
        *processed_ptr = cp; /* 0xxxxxxx */
    } else if (cp < 0x800) {
        *processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */
        *processed_ptr   = ((cp     ) & 0x3F) | 0x80; /* 10xxxxxx */
    } else if (cp < 0xD800 || cp > 0xDFFF) {
        *processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */
        *processed_ptr++ = ((cp >> 6)  & 0x3F) | 0x80; /* 10xxxxxx */
        *processed_ptr   = ((cp     )  & 0x3F) | 0x80; /* 10xxxxxx */
    } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */
        lead = cp;
        unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
        if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */
                !is_utf16_hex((const unsigned char*)unprocessed_ptr)          ||
                sscanf(unprocessed_ptr, "%4x", &trail) == EOF           ||
                trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
            TTY_LOG("Invalid trail surrogate");
            return JSONFailure;
        }
        cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000;
        *processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */
        *processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */
        *processed_ptr++ = (((cp >> 6)  & 0x3F) | 0x80); /* 10xxxxxx */
        *processed_ptr   = (((cp     )  & 0x3F) | 0x80); /* 10xxxxxx */
    } else { /* trail surrogate before lead surrogate */
        TTY_LOG("trail surrogate before lead surrogate");
        return JSONFailure;
    }
    unprocessed_ptr += 3;
    *processed = processed_ptr;
    *unprocessed = unprocessed_ptr;
    return JSONSuccess;
}


/* Copies and processes passed string up to supplied length.
Example: "\u006Corem ipsum" -> lorem ipsum */
static char* process_string(const char *input, size_t len) {
    const char *input_ptr = input;
    size_t initial_size = (len + 1) * sizeof(char);
    size_t final_size = 0;
    char *output = (char*)parson_malloc(initial_size);
    char *output_ptr = output;
    char *resized_output = NULL;
    while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) {
        if (*input_ptr == '\\') {
            input_ptr++;
            switch (*input_ptr) {
            case '\"':
                *output_ptr = '\"';
                break;
            case '\\':
                *output_ptr = '\\';
                break;
            case '/':
                *output_ptr = '/';
                break;
            case 'b':
                *output_ptr = '\b';
                break;
            case 'f':
                *output_ptr = '\f';
                break;
            case 'n':
                *output_ptr = '\n';
                break;
            case 'r':
                *output_ptr = '\r';
                break;
            case 't':
                *output_ptr = '\t';
                break;
            case 'u':
                if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure)
                    goto error;
                break;
            default:
                TTY_LOG("Unhandled case");
                goto error;
            }
        } else if ((unsigned char)*input_ptr < 0x20) {
            TTY_LOG("Invalid characters between 0x00-0x19");
            goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */
        } else {
            *output_ptr = *input_ptr;
        }
        output_ptr++;
        input_ptr++;
    }
    *output_ptr = '\0';
    /* resize to new length */
    final_size = (size_t)(output_ptr-output) + 1;
    if (final_size != initial_size) {
        resized_output = (char*)parson_malloc(final_size);
        if (resized_output == NULL) {
            TTY_LOG("parson_malloc failed for resized_output, %d", final_size);
            goto error;
        }
        memcpy(resized_output, output, final_size);
        //parson_free(output);
        return resized_output;
    } else {
        return output;
    }
error:
    //parson_free(output);
    return NULL;
}

/* Return processed contents of a string between quotes and
   skips passed argument to a matching quote. */
static char * get_quoted_string(const char **string) {
    const char *string_start = *string;
    size_t string_len = 0;
    JSON_Status status = skip_quotes(string);
    if (status != JSONSuccess) {
        TTY_LOG("skip_quotes failed");
        return NULL;
    }
    string_len = *string - string_start - 2; /* length without quotes */
    return process_string(string_start + 1, string_len);
}

static JSON_Value * parse_value(const char **string, size_t nesting) {
    if (nesting > MAX_NESTING) {
        TTY_LOG("nesting > MAX_NESTING");
        return NULL;
    }
    SKIP_WHITESPACES(string);
    switch (**string) {
    case '{':
        return parse_object_value(string, nesting + 1);
    case '[':
        return parse_array_value(string, nesting + 1);
    case '\"':
        return parse_string_value(string);
    case 'f':
    case 't':
        return parse_boolean_value(string);
    case '-':
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        return parse_number_value(string);
    case 'n':
        return parse_null_value(string);
    default:
        TTY_LOG("Unhandled case");
        return NULL;
    }
}

static JSON_Value * parse_object_value(const char **string, size_t nesting) {
    JSON_Value *output_value = json_value_init_object(), *new_value = NULL;
    JSON_Object *output_object = json_value_get_object(output_value);
    char *new_key = NULL;
    if (output_value == NULL  || **string != '{') {
        TTY_LOG("json_value_init_object failed");
        return NULL;
    }
    SKIP_CHAR(string);
    SKIP_WHITESPACES(string);
    if (**string == '}') { /* empty object */
        SKIP_CHAR(string);
        return output_value;
    }
    while (**string != '\0') {
        new_key = get_quoted_string(string);
        SKIP_WHITESPACES(string);
        if (new_key == NULL || **string != ':') {
            TTY_LOG("new_key == NULL or ! :");
            json_value_free(output_value);
            return NULL;
        }
        SKIP_CHAR(string);
        new_value = parse_value(string, nesting);
        if (new_value == NULL) {
            TTY_LOG("parse_value failed");
            //parson_free(new_key);
            json_value_free(output_value);
            return NULL;
        }
        if(json_object_add(output_object, new_key, new_value, false) == JSONFailure) {
            TTY_LOG("json_object_add failed for %s", new_key);
            //parson_free(new_key);
            //parson_free(new_value);
            json_value_free(output_value);
            return NULL;
        }
        //parson_free(new_key);
        SKIP_WHITESPACES(string);
        if (**string != ',')
            break;
        SKIP_CHAR(string);
        SKIP_WHITESPACES(string);
    }
    SKIP_WHITESPACES(string);
    if (**string != '}') {
        //    || /* Trim object after parsing is over */
        //    json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure)
        TTY_LOG("json_object_resize failed");
        json_value_free(output_value);
        return NULL;
    }
    SKIP_CHAR(string);
    return output_value;
}

static JSON_Value * parse_array_value(const char **string, size_t nesting) {
    JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL;
    JSON_Array *output_array = json_value_get_array(output_value);
    if (!output_value || **string != '[') {
        TTY_LOG("json_value_init_array failed");
        return NULL;
    }
    SKIP_CHAR(string);
    SKIP_WHITESPACES(string);
    if (**string == ']') { /* empty array */
        SKIP_CHAR(string);
        return output_value;
    }
    while (**string != '\0') {
        new_array_value = parse_value(string, nesting);
        if (!new_array_value) {
            TTY_LOG("parse_value failed");
            json_value_free(output_value);
            return NULL;
        }
        if(json_array_add(output_array, new_array_value) == JSONFailure) {
            TTY_LOG("json_array_add failed");
            //parson_free(new_array_value);
            json_value_free(output_value);
            return NULL;
        }
        SKIP_WHITESPACES(string);
        if (**string != ',')
            break;
        SKIP_CHAR(string);
        SKIP_WHITESPACES(string);
    }
    SKIP_WHITESPACES(string);
    if (**string != ']') {
        //|| /* Trim array after parsing is over */
        //json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) {
        TTY_LOG("! ] or json_array_resize failed");
        json_value_free(output_value);
        return NULL;
    }
    SKIP_CHAR(string);
    return output_value;
}

static JSON_Value * parse_string_value(const char **string) {
    JSON_Value *value = NULL;
    char *new_string = get_quoted_string(string);
    if (new_string == NULL) {
        TTY_LOG("get_quoted_string failed");
        return NULL;
    }
    value = json_value_init_string_no_copy(new_string);
    if (value == NULL) {
        TTY_LOG("json_value_init_string_no_copy failed");
        //parson_free(new_string);
        return NULL;
    }
    return value;
}

static JSON_Value * parse_boolean_value(const char **string) {
    size_t true_token_size = SIZEOF_TOKEN("true");
    size_t false_token_size = SIZEOF_TOKEN("false");
    if (strncmp("true", *string, true_token_size) == 0) {
        *string += true_token_size;
        return json_value_init_boolean(1);
    } else if (strncmp("false", *string, false_token_size) == 0) {
        *string += false_token_size;
        return json_value_init_boolean(0);
    }

    TTY_LOG("!true or !false");
    return NULL;
}

static JSON_Value * parse_number_value(const char **string) {
    char *end;
    double number = strtod(*string, &end);
    JSON_Value *output_value;
    if (is_decimal(*string, end - *string)) {
        *string = end;
        output_value = json_value_init_number(number);
    } else {
        TTY_LOG("!is_decimal");
        output_value = NULL;
    }
    return output_value;
}

static JSON_Value * parse_null_value(const char **string) {
    size_t token_size = SIZEOF_TOKEN("null");
    if (strncmp("null", *string, token_size) == 0) {
        *string += token_size;
        return json_value_init_null();
    }
    TTY_LOG("!null");
    return NULL;
}

/* Serialization */
#define APPEND_STRING(str) do { written = append_string(buf, (str)); \
                                if (written < 0) { return -1; } \
                                if (buf != NULL) { buf += written; } \
                                written_total += written; } while(0)

#define APPEND_INDENT(level) do { written = append_indent(buf, (level)); \
                                  if (written < 0) { return -1; } \
                                  if (buf != NULL) { buf += written; } \
                                  written_total += written; } while(0)

static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf)
{
    const char *key = NULL, *string = NULL;
    JSON_Value *temp_value = NULL;
    JSON_Array *array = NULL;
    JSON_Object *object = NULL;
    size_t i = 0, count = 0;
    double num = 0.0;
    int written = -1, written_total = 0;

    switch (json_value_get_type(value)) {
    case JSONArray:
        array = json_value_get_array(value);
        count = json_array_get_count(array);
        APPEND_STRING("[");
        if (count > 0 && is_pretty)
            APPEND_STRING("\n");
        for (i = 0; i < count; i++) {
            if (is_pretty)
                APPEND_INDENT(level+1);
            temp_value = json_array_get_value(array, i);
            written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
            if (written < 0)
                return -1;
            if (buf != NULL)
                buf += written;
            written_total += written;
            if (i < (count - 1))
                APPEND_STRING(",");
            if (is_pretty)
                APPEND_STRING("\n");
        }
        if (count > 0 && is_pretty)
            APPEND_INDENT(level);
        APPEND_STRING("]");
        return written_total;
    case JSONObject:
        object = json_value_get_object(value);
        count  = json_object_get_count(object);
        APPEND_STRING("{");
        if (count > 0 && is_pretty)
            APPEND_STRING("\n");
        for (i = 0; i < count; i++) {
            key = json_object_get_name(object, i);
            if (is_pretty)
                APPEND_INDENT(level+1);
            written = json_serialize_string(key, buf);
            if (written < 0)
                return -1;
            if (buf != NULL)
                buf += written;
            written_total += written;
            APPEND_STRING(":");
            if (is_pretty)
                APPEND_STRING(" ");
            temp_value = json_object_get_value(object, key);
            written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
            if (written < 0)
                return -1;
            if (buf != NULL)
                buf += written;
            written_total += written;
            if (i < (count - 1))
                APPEND_STRING(",");
            if (is_pretty)
                APPEND_STRING("\n");
        }
        if (count > 0 && is_pretty)
            APPEND_INDENT(level);
        APPEND_STRING("}");
        return written_total;
    case JSONString:
        string = json_value_get_string(value);
        written = json_serialize_string(string, buf);
        if (written < 0)
            return -1;
        if (buf != NULL)
            buf += written;
        written_total += written;
        return written_total;
    case JSONBoolean:
        if (json_value_get_boolean(value))
            APPEND_STRING("true");
        else
            APPEND_STRING("false");
        return written_total;
    case JSONNumber:
        num = json_value_get_number(value);
        if (buf != NULL)
            num_buf = buf;
        if (num == ((double)(int)num)) /*  check if num is integer */
            written = sprintf(num_buf, "%d", (int)num);
        else
            written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
        if (written < 0)
            return -1;
        if (buf != NULL)
            buf += written;
        written_total += written;
        return written_total;
    case JSONNull:
        APPEND_STRING("null");
        return written_total;
    case JSONError:
        return -1;
    default:
        return -1;
    }
}

static int json_serialize_string(const char *string, char *buf) {
    size_t i = 0, len = strlen(string);
    char c = '\0';
    int written = -1, written_total = 0;
    APPEND_STRING("\"");
    for (i = 0; i < len; i++) {
        c = string[i];
        switch (c) {
        case '\"':
            APPEND_STRING("\\\"");
            break;
        case '\\':
            APPEND_STRING("\\\\");
            break;
#ifdef ENABLE_SPECIAL_CHAR
        case '/':
            APPEND_STRING("\\/");
            break; /* to make json embeddable in xml\/html */
#endif
        case '\b':
            APPEND_STRING("\\b");
            break;
        case '\f':
            APPEND_STRING("\\f");
            break;
        case '\n':
            APPEND_STRING("\\n");
            break;
        case '\r':
            APPEND_STRING("\\r");
            break;
        case '\t':
            APPEND_STRING("\\t");
            break;
        default:
            if (buf != NULL) {
                buf[0] = c;
                buf += 1;
            }
            written_total += 1;
            break;
        }
    }
    APPEND_STRING("\"");
    return written_total;
}

static int append_indent(char *buf, int level) {
    int i;
    int written = -1, written_total = 0;
    for (i = 0; i < level; i++) {
        APPEND_STRING("    ");
    }
    return written_total;
}

static int append_string(char *buf, const char *string) {
    if (buf == NULL) {
        return (int)strlen(string);
    }
    return sprintf(buf, "%s", string);
}

#undef APPEND_STRING
#undef APPEND_INDENT


/* Parser API */

JSON_Value * json_parse_string(const char *string) {
    if (string == NULL) {
        TTY_LOG("Invalid arguments");
        return NULL;
    }
    SKIP_WHITESPACES(&string);
    if (*string != '{' && *string != '[') {
        TTY_LOG("*string != { or [");
        return NULL;
    }
    return parse_value((const char**)&string, 0);
}

/* JSON Object API */

JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
    if (object == NULL || name == NULL)
        return NULL;
    return json_object_nget_value(object, name, strlen(name));
}

const char * json_object_get_string(const JSON_Object *object, const char *name) {
    return json_value_get_string(json_object_get_value(object, name));
}

double json_object_get_number(const JSON_Object *object, const char *name) {
    return json_value_get_number(json_object_get_value(object, name));
}

JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) {
    return json_value_get_object(json_object_get_value(object, name));
}

JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) {
    return json_value_get_array(json_object_get_value(object, name));
}

int json_object_get_boolean(const JSON_Object *object, const char *name) {
    return json_value_get_boolean(json_object_get_value(object, name));
}

JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
    const char *dot_position = strchr(name, '.');
    if (!dot_position)
        return json_object_get_value(object, name);
    object = json_value_get_object(json_object_nget_value(object, name, dot_position - name));
    return json_object_dotget_value(object, dot_position + 1);
}

const char * json_object_dotget_string(const JSON_Object *object, const char *name) {
    return json_value_get_string(json_object_dotget_value(object, name));
}

double json_object_dotget_number(const JSON_Object *object, const char *name) {
    return json_value_get_number(json_object_dotget_value(object, name));
}

JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) {
    return json_value_get_object(json_object_dotget_value(object, name));
}

JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) {
    return json_value_get_array(json_object_dotget_value(object, name));
}

int json_object_dotget_boolean(const JSON_Object *object, const char *name) {
    return json_value_get_boolean(json_object_dotget_value(object, name));
}

size_t json_object_get_count(const JSON_Object *object) {
    return object ? object->count : 0;
}

const char * json_object_get_name(const JSON_Object *object, size_t index) {
    if (index >= json_object_get_count(object))
        return NULL;
    return object->names[index];
}

/* JSON Array API */
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
    if (array == NULL || index >= json_array_get_count(array))
        return NULL;
    return array->items[index];
}

const char * json_array_get_string(const JSON_Array *array, size_t index) {
    return json_value_get_string(json_array_get_value(array, index));
}

double json_array_get_number(const JSON_Array *array, size_t index) {
    return json_value_get_number(json_array_get_value(array, index));
}

JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) {
    return json_value_get_object(json_array_get_value(array, index));
}

JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) {
    return json_value_get_array(json_array_get_value(array, index));
}

int json_array_get_boolean(const JSON_Array *array, size_t index) {
    return json_value_get_boolean(json_array_get_value(array, index));
}

size_t json_array_get_count(const JSON_Array *array) {
    return array ? array->count : 0;
}

/* JSON Value API */
JSON_Value_Type json_value_get_type(const JSON_Value *value) {
    return value ? value->type : JSONError;
}

JSON_Object * json_value_get_object(const JSON_Value *value) {
    return json_value_get_type(value) == JSONObject ? value->value.object : NULL;
}

JSON_Array * json_value_get_array(const JSON_Value *value) {
    return json_value_get_type(value) == JSONArray ? value->value.array : NULL;
}

const char * json_value_get_string(const JSON_Value *value) {
    return json_value_get_type(value) == JSONString ? value->value.string : NULL;
}

double json_value_get_number(const JSON_Value *value) {
    return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
}

int json_value_get_boolean(const JSON_Value *value) {
    return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1;
}

void json_value_free(JSON_Value *value) {
    switch (json_value_get_type(value)) {
    case JSONObject:
        json_object_free(value->value.object);
        break;
    case JSONString:
        if (value->value.string) {
            //parson_free(value->value.string);
        }
        break;
    case JSONArray:
        json_array_free(value->value.array);
        break;
    default:
        break;
    }
    //parson_free(value);
}

JSON_Value * json_value_init_object(void) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) {
        TTY_LOG("parson_malloc failed for new_value, %d", sizeof(JSON_Value));
        return NULL;
    }
    new_value->type = JSONObject;
    new_value->value.object = json_object_init();
    if (!new_value->value.object) {
        TTY_LOG("json_object_init failed");
        //parson_free(new_value);
        return NULL;
    }
    return new_value;
}

JSON_Value * json_value_init_array(void) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) {
        TTY_LOG("parson_malloc failed for new_value, %d", sizeof(JSON_Value));
        return NULL;
    }
    new_value->type = JSONArray;
    new_value->value.array = json_array_init();
    if (!new_value->value.array) {
        TTY_LOG("json_array_init failed");
        //parson_free(new_value);
        return NULL;
    }
    return new_value;
}

JSON_Value * json_value_init_string(const char *string) {
    char *copy = NULL;
    JSON_Value *value;
    size_t string_len = 0;
    if (string == NULL) {
        TTY_LOG("Invalid arguments");
        return NULL;
    }
    string_len = strlen(string);
    if (!is_valid_utf8(string, string_len)) {
        TTY_LOG("is_valid_utf8 failed");
        return NULL;
    }
    copy = parson_strndup(string, string_len);
    if (copy == NULL) {
        TTY_LOG("parson_strndup failed");
        return NULL;
    }
    value = json_value_init_string_no_copy(copy);
    if (value == NULL) {
        TTY_LOG("json_value_init_string_no_copy failed");
        //parson_free(copy);
    }
    return value;
}

JSON_Value * json_value_init_number(double number) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) {
        TTY_LOG("parson_malloc failed for new_value, %d", sizeof(JSON_Value));
        return NULL;
    }
    new_value->type = JSONNumber;
    new_value->value.number = number;
    return new_value;
}

JSON_Value * json_value_init_boolean(int boolean) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) {
        TTY_LOG("parson_malloc failed for new_value, %d", sizeof(JSON_Value));
        return NULL;
    }
    new_value->type = JSONBoolean;
    new_value->value.boolean = boolean ? 1 : 0;
    return new_value;
}

JSON_Value * json_value_init_null(void) {
    JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
    if (!new_value) {
        TTY_LOG("parson_malloc failed for new_value, %d", sizeof(JSON_Value));
        return NULL;
    }
    new_value->type = JSONNull;
    return new_value;
}

size_t json_serialization_size(const JSON_Value *value) {
    char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
    int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf);
    return res < 0 ? 0 : (size_t)(res + 1);
}

JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
    int written = -1;
    size_t needed_size_in_bytes = json_serialization_size(value);
    if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
        TTY_LOG("Insufficient buffer");
        return JSONFailure;
    }
    written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL);
    if (written < 0) {
        TTY_LOG("json_serialize_to_buffer_r failed");
        return JSONFailure;
    }
    return JSONSuccess;
}

char * json_serialize_to_string(const JSON_Value *value) {
    JSON_Status serialization_result = JSONFailure;
    size_t buf_size_bytes = json_serialization_size(value);
    char *buf = NULL;
    if (buf_size_bytes == 0) {
        TTY_LOG("json_serialization_size failed");
        return NULL;
    }
    buf = (char*)parson_malloc(buf_size_bytes);
    if (buf == NULL) {
        TTY_LOG("parson_malloc failed for buf, %d", buf_size_bytes);
        return NULL;
    }
    serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
    if (serialization_result == JSONFailure) {
        TTY_LOG("json_serialize_to_buffer failed");
        json_free_serialized_string(buf);
        return NULL;
    }
    return buf;
}


size_t json_serialization_size_pretty(const JSON_Value *value) {
    char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */
    int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf);
    return res < 0 ? 0 : (size_t)(res + 1);
}


JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
    int written = -1;
    size_t needed_size_in_bytes = json_serialization_size_pretty(value);
    if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes)
        return JSONFailure;
    written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL);
    if (written < 0)
        return JSONFailure;
    return JSONSuccess;
}

char * json_serialize_to_string_pretty(const JSON_Value *value) {
    JSON_Status serialization_result = JSONFailure;
    size_t buf_size_bytes = json_serialization_size_pretty(value);
    char *buf = NULL;
    if (buf_size_bytes == 0) {
        TTY_LOG("json_serialization_size_pretty failed");
        return NULL;
    }
    buf = (char*)parson_malloc(buf_size_bytes);
    if (buf == NULL) {
        TTY_LOG("parson_malloc failed for buf, %d", buf_size_bytes);
        return NULL;
    }
    serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
    if (serialization_result == JSONFailure) {
        TTY_LOG("json_serialize_to_buffer_pretty failed");
        json_free_serialized_string(buf);
        return NULL;
    }
    return buf;
}


void json_free_serialized_string(char *string) {
    //parson_free(string);
}

JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
    size_t i = 0;
    JSON_Value *old_value;
    if (object == NULL || name == NULL || value == NULL)
        return JSONFailure;
    old_value = json_object_get_value(object, name);
    if (old_value != NULL) { /* free and overwrite old value */
        json_value_free(old_value);
        for (i = 0; i < json_object_get_count(object); i++) {
            if (strcmp(object->names[i], name) == 0) {
                object->values[i] = value;
                return JSONSuccess;
            }
        }
    }
    /* add new key value pair */
    return json_object_add(object, name, value, true);
}

JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) {
    return json_object_set_value(object, name, json_value_init_string(string));
}

JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) {
    const char *dot_pos = NULL;
    char *current_name = NULL;
    JSON_Object *temp_obj = NULL;
    JSON_Value *new_value = NULL;
    if (value == NULL || name == NULL || value == NULL)
        return JSONFailure;
    dot_pos = strchr(name, '.');
    if (dot_pos == NULL) {
        return json_object_set_value(object, name, value);
    } else {
        current_name = parson_strndup(name, dot_pos - name);
        temp_obj = json_object_get_object(object, current_name);
        if (temp_obj == NULL) {
            new_value = json_value_init_object();
            if (new_value == NULL) {
                //parson_free(current_name);
                return JSONFailure;
            }
            if (json_object_add(object, current_name, new_value, true) == JSONFailure) {
                json_value_free(new_value);
                //parson_free(current_name);
                return JSONFailure;
            }
            temp_obj = json_object_get_object(object, current_name);
        }
        //parson_free(current_name);
        return json_object_dotset_value(temp_obj, dot_pos + 1, value);
    }
}

JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) {
    JSON_Value *value = json_value_init_string(string);
    if (value == NULL)
        return JSONFailure;
    if (json_object_dotset_value(object, name, value) == JSONFailure) {
        json_value_free(value);
        return JSONFailure;
    }
    return JSONSuccess;
}

JSON_Status json_object_remove(JSON_Object *object, const char *name) {
    size_t i = 0, last_item_index = 0;
    if (object == NULL || json_object_get_value(object, name) == NULL)
        return JSONFailure;
    last_item_index = json_object_get_count(object) - 1;
    for (i = 0; i < json_object_get_count(object); i++) {
        if (strcmp(object->names[i], name) == 0) {
            //parson_free(object->names[i]);
            json_value_free(object->values[i]);
            if (i != last_item_index) { /* Replace key value pair with one from the end */
                object->names[i] = object->names[last_item_index];
                object->values[i] = object->values[last_item_index];
            }
            object->count -= 1;
            return JSONSuccess;
        }
    }
    return JSONFailure; /* No execution path should end here */
}

JSON_Status json_object_dotremove(JSON_Object *object, const char *name) {
    const char *dot_pos = strchr(name, '.');
    char *current_name = NULL;
    JSON_Object *temp_obj = NULL;
    if (dot_pos == NULL) {
        return json_object_remove(object, name);
    } else {
        current_name = parson_strndup(name, dot_pos - name);
        temp_obj = json_object_get_object(object, current_name);
        if (temp_obj == NULL) {
            //parson_free(current_name);
            return JSONFailure;
        }
        //parson_free(current_name);
        return json_object_dotremove(temp_obj, dot_pos + 1);
    }
}

JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) {
    if (array == NULL || value == NULL) {
        return JSONFailure;
    }
    return json_array_add(array, value);
}
