example_stream.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "lwjson.h"
  4. typedef struct example_data_struct_t {
  5. uint8_t k1[10];
  6. char* k2;
  7. int k2_len;
  8. int k2_pos;
  9. } example_data_struct_t;
  10. /* Test string to parser */
  11. static const char* json_str = "{\"k1\":\"v1\",\"k2\":[true, false, true]}";
  12. /* LwJSON stream parser */
  13. static lwjson_stream_parser_t stream_parser;
  14. /**
  15. * \brief Callback function for various events
  16. * \param jsp: JSON stream parser object
  17. * \param type: Event type
  18. */
  19. static void
  20. prv_example_callback_func(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type) {
  21. //Get the data struct from user data
  22. example_data_struct_t* data = lwjson_stream_get_user_data(jsp);
  23. if (jsp->stack_pos >= 2 /* Number of stack entries must be high */
  24. && jsp->stack[0].type == LWJSON_STREAM_TYPE_OBJECT /* First must be object */
  25. && jsp->stack[1].type == LWJSON_STREAM_TYPE_KEY /* We need key to be before */
  26. && strcmp(jsp->stack[1].meta.name, "k1") == 0) {
  27. printf("Got key '%s' with value '%s'\r\n", jsp->stack[1].meta.name, jsp->data.str.buff);
  28. strncpy((char*)data->k1, jsp->data.str.buff, sizeof(data->k1) - 1);
  29. }
  30. if (jsp->stack_pos >= 2 /* Number of stack entries must be high */
  31. && jsp->stack[0].type == LWJSON_STREAM_TYPE_OBJECT /* First must be object */
  32. && jsp->stack[1].type == LWJSON_STREAM_TYPE_KEY /* We need key to be before */
  33. && strcmp(jsp->stack[1].meta.name, "k2") == 0) {
  34. printf("Got key '%s' with value '%s'\r\n", jsp->stack[1].meta.name, jsp->data.str.buff);
  35. if (jsp->stack_pos >= 3 && jsp->stack[2].type == LWJSON_STREAM_TYPE_ARRAY
  36. && jsp->stack[2].meta.index < data->k2_len) {
  37. printf("Got array value '%s' index = %d \r\n", jsp->data.str.buff, jsp->stack[2].meta.index);
  38. data->k2[jsp->stack[2].meta.index] = (strncmp(jsp->data.str.buff, "true", 4) == 0);
  39. data->k2_pos = jsp->stack[2].meta.index + 1;
  40. }
  41. }
  42. (void)type;
  43. }
  44. /* Parse JSON */
  45. void
  46. example_stream_run(void) {
  47. lwjsonr_t res;
  48. printf("\r\n\r\nParsing stream\r\n");
  49. example_data_struct_t data;
  50. char k2_buff[10];
  51. data.k2 = k2_buff;
  52. data.k2_len = sizeof(k2_buff);
  53. data.k2_pos = 0;
  54. lwjson_stream_init(&stream_parser, prv_example_callback_func);
  55. lwjson_stream_set_user_data(&stream_parser, &data);
  56. /* Demonstrate as stream inputs */
  57. for (const char* c = json_str; *c != '\0'; ++c) {
  58. res = lwjson_stream_parse(&stream_parser, *c);
  59. if (res == lwjsonSTREAMINPROG) {
  60. printf("Parsing in progress\r\n");
  61. } else if (res == lwjsonSTREAMWAITFIRSTCHAR) {
  62. printf("Waiting first character\r\n");
  63. } else if (res == lwjsonSTREAMDONE) {
  64. printf("Done\r\n");
  65. } else {
  66. printf("Error\r\n");
  67. break;
  68. }
  69. }
  70. printf("Parsing completed\r\n");
  71. printf("data: k1 = '%s'\r\n", data.k1);
  72. for (int i = 0; i < data.k2_pos; i++) {
  73. printf("data: k2[%d] = %d\r\n", i, data.k2[i]);
  74. }
  75. }
  76. int
  77. main(void) {
  78. example_stream_run();
  79. return 0;
  80. }