Skip to content

Commit

Permalink
Start of jsonrpc interface
Browse files Browse the repository at this point in the history
  • Loading branch information
abbec committed Oct 6, 2024
1 parent 85ce9be commit 2db363e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/dged/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ uint64_t json_len(struct json_object *obj) {
return HASHMAP_SIZE(&obj->members);
}

uint64_t json_empty(struct json_object *obj) {
return json_len(obj) == 0;
}

bool json_contains(struct json_object *obj, struct s8 key) {
// TODO: get rid of alloc
char *k = s8tocstr(key);
Expand Down
9 changes: 9 additions & 0 deletions src/dged/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ struct json_result json_parse(const uint8_t *buf, uint64_t size);
*/
void json_destroy(struct json_value *value);

/**
* Check if a JSON object is empty.
*
* @param [in] obj The JSON object to check if empty.
*
* @returns True if @ref obj is empty, false otherwise.
*/
void json_empty(struct json_object *obj);

/**
* Return the number of members in a JSON object.
*
Expand Down
32 changes: 32 additions & 0 deletions src/dged/jsonrpc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef _JSONRPC_H
#define _JSONRPC_H

#include "json.h"
#include "s8.h"

struct jsonrpc_request {
struct json_value id;
struct s8 method;
struct json_object *params;
};

struct jsonrpc_response {
struct json_value id;
bool ok;
union {
struct json_value result;
struct jsonrpc_error error;
} value;
};

struct jsonrpc_error {
int code;
struct s8 message;
struct json_value data;
};

struct jsonrpc_request jsonrpc_request_create(struct s8 method, struct json_object *params);
struct jsonrpc_response jsonrpc_parse_response(const uint8_t *buf, uint64_t size);
struct s8 jsonrpc_request_to_string(const struct jsonprc_request *request);

#endif

0 comments on commit 2db363e

Please sign in to comment.