/******************************************************* 
* File name: simple_json.c
* Author   :  wfq
* Versions :  1.0
* Description: simple json parse function.
*               Do not solve the completed json_string or string with json-string.
*               Completed: like array.
*               Must satisfy the yipai dev-protocol, and test by yipai dev-protocol. 
* History: 
*   1.Date:    2016-10-10 
*     Author:  wfq
*     Action:  create
*********************************************************/ 


#include "simple_json.h"
#include <string.h>
#include <stdio.h>


void simple_parse_json(const char *data, char *key_str, char *value_str)
{
    const char *p, *q;
    int len;

    if (data == NULL || key_str == NULL || value_str == NULL) {
        return;
    }

    // Clear output buffer safely
    value_str[0] = '\0';

    // Find key in data
    p = strstr(data, key_str);
    if (p == NULL) return;

    // Skip key, colon, and whitespace:  "key" : value
    p += strlen(key_str);
    while (*p == ' ') p++;
    if (*p == ':') p++;
    while (*p == ' ') p++;

    if (*p == '\0') return;

    // Dispatch by value type (first character after colon)
    if (*p == '"') {
        // --- Quoted string: "..." -> extract content between quotes ---
        const char *start = p + 1;
        q = strchr(start, '"');
        len = (q != NULL) ? (int)(q - start) : (int)strlen(start);
        memcpy(value_str, start, len);
        value_str[len] = '\0';

    } else if (*p == '{') {
        // --- JSON object: { ... } -> extract content between braces ---
        const char *start = p + 1;
        q = strstr(start, "},");
        if (q == NULL) q = strstr(start, "}\r\n");
        if (q == NULL) q = strstr(start, "}\n");
        if (q == NULL) q = strchr(start, '}');
        len = (q != NULL) ? (int)(q - start) : (int)strlen(start);
        memcpy(value_str, start, len);
        value_str[len] = '\0';

    } else if (*p == '[') {
        // --- JSON array: [ ... ] -> extract content between brackets ---
        const char *start = p + 1;
        q = strstr(start, "],");
        if (q == NULL) q = strstr(start, "]\r\n");
        if (q == NULL) q = strstr(start, "]\n");
        if (q == NULL) q = strchr(start, ']');
        len = (q != NULL) ? (int)(q - start) : (int)strlen(start);
        memcpy(value_str, start, len);
        value_str[len] = '\0';

    } else {
        // --- Plain value: number, true, false, null ---
        // Terminated by ',' | '}' | '\\r\\n' | '\\n'
        q = strchr(p, ',');
        if (q == NULL) q = strchr(p, '}');
        if (q == NULL) q = strstr(p, "\r\n");
        if (q == NULL) q = strstr(p, "\n");
        len = (q != NULL) ? (int)(q - p) : (int)strlen(p);
        memcpy(value_str, p, len);
        value_str[len] = '\0';
    }
}


char * simpleJsonGetObject(const char * data, const char * key)
{
    if(data == NULL)
    {
        return NULL;
    }
    
    
    return NULL;
}

void simple_json_getarray_item(const char *data, char *key_str, char *value_str)
{
    const char *p, *q;
    int len;

    if (data == NULL || key_str == NULL || value_str == NULL) {
        return;
    }

    value_str[0] = '\0';

    p = strstr(data, key_str);
    if (p == NULL) return;

    p += strlen(key_str);
    while (*p == ' ') p++;
    if (*p == ':') p++;
    while (*p == ' ') p++;

    if (*p == '\0') return;

    if (*p == '"') {
        const char *start = p + 1;
        q = strchr(start, '"');
        len = (q != NULL) ? (int)(q - start) : (int)strlen(start);
        memcpy(value_str, start, len);
        value_str[len] = '\0';

    } else if (*p == '{') {
        const char *start = p + 1;
        q = strstr(start, "},");
        if (q == NULL) q = strstr(start, "}\r\n");
        if (q == NULL) q = strstr(start, "}\n");
        if (q == NULL) q = strchr(start, '}');
        len = (q != NULL) ? (int)(q - start) : (int)strlen(start);
        memcpy(value_str, start, len);
        value_str[len] = '\0';

    } else if (*p == '[') {
        const char *start = p + 1;
        q = strstr(start, "],");
        if (q == NULL) q = strstr(start, "]\r\n");
        if (q == NULL) q = strstr(start, "]\n");
        if (q == NULL) q = strchr(start, ']');
        len = (q != NULL) ? (int)(q - start) : (int)strlen(start);
        memcpy(value_str, start, len);
        value_str[len] = '\0';

    } else {
        q = strchr(p, ',');
        if (q == NULL) q = strchr(p, '}');
        if (q == NULL) q = strstr(p, "\r\n");
        if (q == NULL) q = strstr(p, "\n");
        len = (q != NULL) ? (int)(q - p) : (int)strlen(p);
        memcpy(value_str, p, len);
        value_str[len] = '\0';
    }
}
