Skip to content

Commit

Permalink
add option for trial run -> place for a user to "experiment" with the…
Browse files Browse the repository at this point in the history
… parsing
  • Loading branch information
MaJerle committed Mar 6, 2024
1 parent 5b83b11 commit f235fbd
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -18,6 +18,8 @@ else()
${CMAKE_CURRENT_LIST_DIR}/examples/example_minimal.c
${CMAKE_CURRENT_LIST_DIR}/examples/example_traverse.c
${CMAKE_CURRENT_LIST_DIR}/examples/example_stream.c
${CMAKE_CURRENT_LIST_DIR}/trial_env/trial_run.c

)

# Add key include paths
Expand Down
9 changes: 8 additions & 1 deletion dev/main.c
@@ -1,8 +1,8 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "windows.h"
#include "lwjson/lwjson.h"
#include "windows.h"

/* Classic parser */
static lwjson_token_t tokens[4096];
Expand All @@ -16,6 +16,9 @@ extern void example_minimal_run(void);
extern void example_traverse_run(void);
extern void example_stream_run(void);

/* Trial stream -> for a user to try its own code... */
extern void trial_stream_run(void);

static void jsp_stream_callback(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type);

int
Expand All @@ -27,6 +30,10 @@ main() {
const lwjson_token_t* tkn;

(void)token_cnt;
#if 1
trial_stream_run();
return 0;
#endif
#if 0
test_run();
example_minimal_run();
Expand Down
2 changes: 1 addition & 1 deletion examples/example_stream.c
Expand Up @@ -12,7 +12,7 @@ static lwjson_stream_parser_t stream_parser;
* \param jsp: JSON stream parser object
* \param type: Event type
*/
void
static void
prv_example_callback_func(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type) {
/* Get a value corresponsing to "k1" key */
if (jsp->stack_pos >= 2 /* Number of stack entries must be high */
Expand Down
18 changes: 10 additions & 8 deletions examples/example_stream_with_user_data.c
@@ -1,9 +1,10 @@
#include <stdio.h>
#include "lwjson/lwjson.h"
#include <string.h>
#include "lwjson/lwjson.h"

typedef struct example_data_struct_t {
uint8_t k1[10];
char *k2;
char* k2;
int k2_len;
int k2_pos;
} example_data_struct_t;
Expand All @@ -19,7 +20,7 @@ static lwjson_stream_parser_t stream_parser;
* \param jsp: JSON stream parser object
* \param type: Event type
*/
void
static void
prv_example_callback_func(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type) {
//Get the data struct from user data
example_data_struct_t* data = lwjson_stream_get_user_data(jsp);
Expand All @@ -29,15 +30,15 @@ prv_example_callback_func(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type
&& jsp->stack[1].type == LWJSON_STREAM_TYPE_KEY /* We need key to be before */
&& strcmp(jsp->stack[1].meta.name, "k1") == 0) {
printf("Got key '%s' with value '%s'\r\n", jsp->stack[1].meta.name, jsp->data.str.buff);
strncpy((char *)data->k1, jsp->data.str.buff, sizeof(data->k1) - 1);
strncpy((char*)data->k1, jsp->data.str.buff, sizeof(data->k1) - 1);
}
if (jsp->stack_pos >= 2 /* Number of stack entries must be high */
&& jsp->stack[0].type == LWJSON_STREAM_TYPE_OBJECT /* First must be object */
&& jsp->stack[1].type == LWJSON_STREAM_TYPE_KEY /* We need key to be before */
&& strcmp(jsp->stack[1].meta.name, "k2") == 0) {
printf("Got key '%s' with value '%s'\r\n", jsp->stack[1].meta.name, jsp->data.str.buff);
if (jsp->stack_pos >= 3
&& jsp->stack[2].type == LWJSON_STREAM_TYPE_ARRAY && jsp->stack[2].meta.index < data->k2_len) {
if (jsp->stack_pos >= 3 && jsp->stack[2].type == LWJSON_STREAM_TYPE_ARRAY
&& jsp->stack[2].meta.index < data->k2_len) {
printf("Got array value '%s' index = %d \r\n", jsp->data.str.buff, jsp->stack[2].meta.index);
data->k2[jsp->stack[2].meta.index] = (strncmp(jsp->data.str.buff, "true", 4) == 0);
data->k2_pos = jsp->stack[2].meta.index + 1;
Expand Down Expand Up @@ -73,12 +74,13 @@ example_stream_run(void) {
}
printf("Parsing completed\r\n");
printf("data: k1 = '%s'\r\n", data.k1);
for(int i = 0; i < data.k2_pos; i++) {
for (int i = 0; i < data.k2_pos; i++) {
printf("data: k2[%d] = %d\r\n", i, data.k2[i]);
}
}

int main(void) {
int
main(void) {
example_stream_run();
return 0;
}
79 changes: 79 additions & 0 deletions trial_env/trial_run.c
@@ -0,0 +1,79 @@
#include <stdio.h>
#include "lwjson/lwjson.h"
#include "windows.h"

/* LwJSON stream parser */
static lwjson_stream_parser_t stream_parser;

/**
* \brief Callback function for various events
* \param jsp: JSON stream parser object
* \param type: Event type
*/
static void
prv_example_callback_func(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type) {
/* IO device part of the parsing goes here */
if (jsp->stack_pos == 4 && lwjson_stack_seq_4(jsp, 0, OBJECT, KEY, OBJECT, KEY)) {
} else if (jsp->stack_pos == 7 && lwjson_stack_seq_7(jsp, 0, OBJECT, KEY, OBJECT, KEY, ARRAY, OBJECT, KEY)) {
}

(void)type;
/* ... */
}

/* Parse JSON */
void
trial_stream_run(void) {
lwjsonr_t res;
HANDLE f;
DWORD file_size;
char* json_text = NULL;

printf("\r\n\r\nTrial parsing test stream\r\n");
lwjson_stream_init(&stream_parser, prv_example_callback_func);

f = CreateFile(TEXT("trial_env/trial_run.json"),
GENERIC_READ, // open for reading
0, // do not share
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template

if (f == INVALID_HANDLE_VALUE) {
printf("Could not open file..\r\n");
goto exit;
}
if ((file_size = GetFileSize(f, NULL)) == INVALID_FILE_SIZE) {
printf("Invalid file size..\r\n");
goto exit;
} else if (file_size == 0) {
printf("File is empty..\r\n");
goto exit;
}
if ((json_text = calloc((size_t)(file_size + 1), sizeof(*json_text))) == NULL) {
printf("Could not allocate memory..\r\n");
goto exit;
}
if (ReadFile(f, json_text, file_size, NULL, NULL) == 0) {
printf("Could not read full file..\r\n");
goto exit;
}

/* Demonstrate as stream inputs */
for (const char* c = json_text; *c != '\0'; ++c) {
res = lwjson_stream_parse(&stream_parser, *c);
if (res == lwjsonSTREAMINPROG) {
} else if (res == lwjsonSTREAMWAITFIRSTCHAR) {
printf("Waiting first character\r\n");
} else if (res == lwjsonSTREAMDONE) {
printf("Done\r\n");
} else {
printf("Error\r\n");
break;
}
}
exit:
free(json_text);
printf("Parsing completed\r\n");
}
3 changes: 3 additions & 0 deletions trial_env/trial_run.json
@@ -0,0 +1,3 @@
{
"json": "value"
}

0 comments on commit f235fbd

Please sign in to comment.