]> uap-core.de Git - uwplayer.git/commitdiff
add functions for easier json object/array access
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 8 Jan 2022 16:54:05 +0000 (17:54 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 8 Jan 2022 16:54:05 +0000 (17:54 +0100)
application/Makefile
application/json.c
application/json.h
application/player.c
application/player.h

index 8424552a14d493dca7aebfe3c688cf0dc1006f90..ea2143940db390998c3580a8b112e60176018a72 100644 (file)
@@ -37,6 +37,7 @@ SRC += window.c
 SRC += player.c
 SRC += settings.c
 SRC += utils.c
+SRC += json.c
 
 OBJ = $(SRC:%.c=$(BUILD_ROOT)/build/application/%.$(OBJ_EXT))
 
index 6b126d470e877f045f41e74f238e5cbb3701ba51..4dd672c30a1c1880e230019cee18c303901259dd 100644 (file)
@@ -842,3 +842,46 @@ int json_read_value(JSONParser *p, JSONValue **value) {
     return 1;
 }
 
+JSONValue* json_obj_get(JSONObject *obj, const char *name) {
+    for(size_t i=0;i<obj->size;i++) {
+        if(!strcmp(obj->values[i].name, name)) {
+            return obj->values[i].value;
+        }
+    }
+    return NULL;
+}
+
+JSONValue* json_array_get(JSONArray *array, size_t i) {
+    if(i >= array->size) return NULL;
+    return array->array[i];
+}
+
+void json_value_free(JSONValue *value) {
+    switch(value->type) {
+        case JSON_OBJECT: {
+            JSONObject obj = value->value.object;
+            for(size_t i=0;i<obj.size;i++) {
+                json_value_free(obj.values[i].value);
+                free(obj.values[i].name);
+            }
+            free(obj.values);
+            break;
+        }
+        case JSON_ARRAY: {
+            JSONArray array = value->value.array;
+            for(size_t i=0;i<array.size;i++) {
+                json_value_free(array.array[i]);
+            }
+            free(array.array);
+            break;
+        }
+        case JSON_STRING: {
+            free(value->value.string.string);
+            break;
+        }
+        default: {
+            break;
+        }
+    }
+    free(value);
+}
index 88494bff2858558fcfb74fc8bb6865e19bcb9879..659c7f6648c51a8493919f39379b5d4275a6f19d 100644 (file)
@@ -200,6 +200,11 @@ int json_reader_bool(JSONParser *p);
 
 int json_read_value(JSONParser *p, JSONValue **value);
 
+JSONValue* json_obj_get(JSONObject *obj, const char *name);
+
+JSONValue* json_array_get(JSONArray *array, size_t i);
+
+void json_value_free(JSONValue *value);
 
 #ifdef __cplusplus
 }
index d553b8136b6212e5c910c59bc9917208ef935b76..38b3a0c4f95731eca334839ec05e5b711427fce1 100644 (file)
@@ -37,7 +37,7 @@
 #include <sys/un.h>
 #include <pthread.h>
 
-
+#include "json.h"
 
 extern char **environ;
 
@@ -47,6 +47,8 @@ extern char **environ;
 #define PLAYER_POLL_TIMEOUT 500
 #define PLAYER_IN_BUFSIZE   8192
 
+static void json_print(JSONValue *value, char *name, int indent);
+
 static void* start_player(void *data);
 
 static void player_io(Player *p);
@@ -254,6 +256,8 @@ static void player_io(Player *p) {
     fds[1].events = POLLIN;
     fds[1].revents = 0;
     
+    JSONParser *js = json_parser_new();
+    
     char buf[PLAYER_IN_BUFSIZE];
     while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
         if(fds[0].revents == POLLIN) {
@@ -266,12 +270,22 @@ static void player_io(Player *p) {
             if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
                 break;
             }
+            json_parser_fill(js, buf, r);
             
-            write(1, buf, r);
+            JSONValue *value;
+            int ret;
+            while((ret = json_read_value(js, &value)) == 1) {
+                json_print(value, NULL, 0);
+                json_value_free(value);
+            }
             
+            if(ret == -1) {
+                fprintf(stderr, "JSON-RPC error\n");
+                break;
+            }
         }
         
-        char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"] }\n";
+        char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n";
         write(p->ipc, cmd, strlen(cmd));
     }
 }
@@ -295,3 +309,68 @@ void PlayerDestroy(Player *p) {
     free(p);
 }
 
+
+static void json_print(JSONValue *value, char *name, int indent) {
+    if(name) {
+        printf("%*s%s: ", indent*4, "", name);
+    } else {
+        printf("%*s", indent*4, "");
+    }
+    
+    
+    switch(value->type) {
+        case JSON_OBJECT: {
+            printf("{\n");
+            
+            for(int i=0;i<value->value.object.size;i++) {
+                JSONObjValue val = value->value.object.values[i];
+                json_print(val.value, val.name, indent+1);
+                if(i+1 < value->value.object.size) {
+                    printf(",\n");
+                } else {
+                    printf("\n");
+                }
+            }
+            
+            printf("%*s}", indent*4, "");
+            break;
+        }
+        case JSON_ARRAY: {
+            printf("[\n");
+            
+            for(int i=0;i<value->value.object.size;i++) {
+                JSONValue *v = value->value.array.array[i];
+                json_print(v, NULL, indent+1);
+                if(i+1 < value->value.array.size) {
+                    printf(",\n");
+                } else {
+                    printf("\n");
+                }
+            }
+            
+            printf("%*s]", indent*4, "");
+            break;
+        }
+        case JSON_STRING: {
+            printf("\"%s\"", value->value.string.string);
+            break;
+        }
+        case JSON_INTEGER: {
+            printf("%i", (int)value->value.integer.value);
+            break;
+        }
+        case JSON_NUMBER: {
+            printf("%f", value->value.number.value);
+            break;
+        }
+        case JSON_LITERAL: {
+            printf("%s\n", "literal\n");
+            break;
+        }
+    }
+    
+    if(indent == 0) {
+        putchar('\n');
+    }
+}
+
index 8d50a72744f2fc110b8ec85dd02bfa7e93bdfa86..dc608272508ea0ce452e961e91b3e0eb9ba9e9de 100644 (file)
@@ -30,7 +30,7 @@
 extern "C" {
 #endif
 
-#define REQ_ID_PLAYBACK_TIME 1
+#define REQ_ID_PLAYBACK_TIME "11"
     
 void PlayerOpenFile(MainWindow *win);