json-input iterate

This commit is contained in:
John Kerl 2016-02-01 09:11:47 -05:00
parent 7d13ecd727
commit 8fa83ff891
5 changed files with 32 additions and 18 deletions

View file

@ -8,6 +8,8 @@ libinput_la_SOURCES= \
file_reader_stdio.h \
json.c \
json.h \
mlr_json.c \
mlr_json.h \
line_readers.c \
line_readers.h \
lrec_reader.h \

View file

@ -25,8 +25,6 @@
#include "input/json.h"
typedef struct _lrec_reader_mmap_json_state_t {
// xxx cmt why both lists
sllv_t* parsed_top_level_jsons;
sllv_t* parsed_json_objects;
int num_records;
int record_index;
@ -41,9 +39,9 @@ lrec_reader_t* lrec_reader_mmap_json_alloc(char* irs, char* ifs, char* ips, int
lrec_reader_t* plrec_reader = mlr_malloc_or_die(sizeof(lrec_reader_t));
lrec_reader_mmap_json_state_t* pstate = mlr_malloc_or_die(sizeof(lrec_reader_mmap_json_state_t));
pstate->parsed_top_level_jsons = NULL;
pstate->num_records = 0;
pstate->record_index = 0;
pstate->parsed_json_objects = NULL;
pstate->num_records = 0;
pstate->record_index = 0;
plrec_reader->pvstate = (void*)pstate;
plrec_reader->popen_func = file_reader_mmap_vopen;
@ -57,11 +55,11 @@ lrec_reader_t* lrec_reader_mmap_json_alloc(char* irs, char* ifs, char* ips, int
static void lrec_reader_mmap_json_free(lrec_reader_t* preader) {
lrec_reader_mmap_json_state_t* pstate = preader->pvstate;
for (sllve_t* pe = pstate->parsed_top_level_jsons->phead; pe != NULL; pe = pe->pnext) {
json_value_t* parsed_top_level_json = pe->pvvalue;
json_value_free(parsed_top_level_json);
for (sllve_t* pe = pstate->parsed_json_objects->phead; pe != NULL; pe = pe->pnext) {
json_value_t* parsed_json_object = pe->pvvalue;
json_value_free(parsed_json_object);
}
sllv_free(pstate->parsed_top_level_jsons);
sllv_free(pstate->parsed_json_objects);
free(pstate);
free(preader);
}
@ -79,15 +77,15 @@ static void lrec_reader_mmap_json_sof(void* pvstate, void* pvhandle) {
.max_memory = 0
};
if (pstate->parsed_top_level_jsons != NULL) {
for (sllve_t* pe = pstate->parsed_top_level_jsons->phead; pe != NULL; pe = pe->pnext) {
json_value_t* parsed_top_level_json = pe->pvvalue;
json_value_free(parsed_top_level_json);
if (pstate->parsed_json_objects != NULL) {
for (sllve_t* pe = pstate->parsed_json_objects->phead; pe != NULL; pe = pe->pnext) {
json_value_t* parsed_json_object = pe->pvvalue;
json_value_free(parsed_json_object);
}
// xxx make an sllv_free_with_callback & use it throughout
sllv_free(pstate->parsed_top_level_jsons);
sllv_free(pstate->parsed_json_objects);
}
pstate->parsed_top_level_jsons = sllv_alloc();
pstate->parsed_json_objects = sllv_alloc();
// xxx comment support missing outer [], as jq does.
@ -102,7 +100,8 @@ static void lrec_reader_mmap_json_sof(void* pvstate, void* pvhandle) {
exit(1);
}
sllv_append(pstate->parsed_top_level_jsons, parsed_top_level_json);
// xxx stub
sllv_append(pstate->parsed_json_objects, parsed_top_level_json);
// xxx
//switch(parsed_top_level_json->type) {
//case json_array:
@ -128,6 +127,13 @@ static void lrec_reader_mmap_json_sof(void* pvstate, void* pvhandle) {
}
// ----------------------------------------------------------------
// xxx transfer func:
// input: top-level json value
// input: current sllv of object
// output: appended sllv
// json value will be freedo, or transferred to the sllv
// ----------------------------------------------------------------
static lrec_t* lrec_reader_mmap_json_process(void* pvstate, void* pvhandle, context_t* pctx) {
//file_reader_mmap_state_t* phandle = pvhandle;

1
c/input/mlr_json.c Normal file
View file

@ -0,0 +1 @@
#include "mlr_json.h"

5
c/input/mlr_json.h Normal file
View file

@ -0,0 +1,5 @@
// Transfers data from the JSON parser to Miller records
#ifndef MLR_JSON_H
#define MLR_JSON_H
#endif // MLR_JSON_H

View file

@ -47,11 +47,11 @@ TOP-OF-LIST SUMMARY
TOP-OF-LIST DETAILS
* JSON I/O:
! acknowledgement to mld
- input: https://github.com/udp/json-parser ? ; http://www.json.org/ C section
! acknowledgement to mld: https://github.com/udp/json-parser
k output: mlr-native json output is trivial & just do it.
- cover x 2
- define 'tabular JSON' by example @ mld
? flatten/unflatten option?
* doc:
o \0 example into mld:ref:regex