From e9b6ccf6a1fcb5cdc98a211160139cea17fead2e Mon Sep 17 00:00:00 2001 From: John Kerl Date: Mon, 1 Feb 2016 19:30:14 -0500 Subject: [PATCH] json-input iterate --- c/input/json.c | 15 ++++++++++ c/input/json.h | 4 +++ c/input/lrec_reader_mmap_json.c | 7 ++++- c/input/mlr_json.c | 50 +++++++++++++++++++++++++++++++-- c/input/mlr_json.h | 2 +- 5 files changed, 74 insertions(+), 4 deletions(-) diff --git a/c/input/json.c b/c/input/json.c index 7e0687a95..bcc125873 100644 --- a/c/input/json.c +++ b/c/input/json.c @@ -900,3 +900,18 @@ void json_value_free(json_value_t * value) { settings.mem_free = default_free; json_value_free_ex(&settings, value); } + +// ---------------------------------------------------------------- +char* json_describe_type(json_type_t type) { + switch(type) { + case JSON_NONE: return "JSON_NONE"; break; + case JSON_OBJECT: return "JSON_OBJECT"; break; + case JSON_ARRAY: return "JSON_ARRAY"; break; + case JSON_INTEGER: return "JSON_INTEGER"; break; + case JSON_DOUBLE: return "JSON_DOUBLE"; break; + case JSON_STRING: return "JSON_STRING"; break; + case JSON_BOOLEAN: return "JSON_BOOLEAN"; break; + case JSON_NULL: return "JSON_NULL"; break; + default: return "???"; break; + } +} diff --git a/c/input/json.h b/c/input/json.h index d0b1a1239..7cfd848b0 100644 --- a/c/input/json.h +++ b/c/input/json.h @@ -26,6 +26,8 @@ // SUCH DAMAGE. // ================================================================ +// xxx rename to json_parser.c/h + #ifndef _JSON_H #define _JSON_H @@ -139,4 +141,6 @@ void json_value_free_ex( json_settings_t * settings, json_value_t *); +char* json_describe_type(json_type_t type); + #endif // _JSON_H diff --git a/c/input/lrec_reader_mmap_json.c b/c/input/lrec_reader_mmap_json.c index 59f6fec1b..40a2d340d 100644 --- a/c/input/lrec_reader_mmap_json.c +++ b/c/input/lrec_reader_mmap_json.c @@ -23,8 +23,11 @@ #include "input/file_reader_mmap.h" #include "input/lrec_readers.h" #include "input/json.h" +// xxx to mlr_json_adapter or some such +#include "input/mlr_json.h" typedef struct _lrec_reader_mmap_json_state_t { + // xxx just have a list of top-level objects and a list of lrecs? sllv_t* parsed_json_objects; int num_records; int record_index; @@ -101,7 +104,9 @@ static void lrec_reader_mmap_json_sof(void* pvstate, void* pvhandle) { } // xxx stub - sllv_append(pstate->parsed_json_objects, parsed_top_level_json); + //sllv_append(pstate->parsed_json_objects, parsed_top_level_json); + // xxx swap arg order + transfer_objects(parsed_top_level_json, pstate->parsed_json_objects); if (item_start == NULL) break; diff --git a/c/input/mlr_json.c b/c/input/mlr_json.c index f7007fb53..638b367a0 100644 --- a/c/input/mlr_json.c +++ b/c/input/mlr_json.c @@ -1,4 +1,6 @@ -#include "mlr_json.h" +#include "lib/mlr_globals.h" +#include "lib/mlrutil.h" +#include "input/mlr_json.h" // ---------------------------------------------------------------- // xxx transfer func: @@ -6,12 +8,36 @@ // input: current sllv of object // output: appended sllv // json value will be freed, or transferred to the sllv +// xxx define work done here: *not* recursing into JSON objects. just ascertaining that they *are* JSON objects. +// xxx define pointer-ownership ... do *not* call it transfer_objects but rather reference_objects. the sllv +// should not free the strings. +// xxx why not make lrecs right here -- want to be able to produce data up to the bad point (or not ...) -void transfer_objects(json_value_t* ptop_level_json, sllv_t* pobjects) { +int transfer_objects(json_value_t* ptop_level_json, sllv_t* pobjects) { if (ptop_level_json->type == JSON_ARRAY) { + int n = ptop_level_json->u.array.length; + for (int i = 0; i < n; i++) { + json_value_t* pnext_level_json = ptop_level_json->u.array.values[i]; + if (pnext_level_json->type != JSON_OBJECT) { + fprintf(stderr, + "%s: found non-object (type %s) within top-level array. This is valid but unmillerable JSON.\n", + MLR_GLOBALS.argv0, json_describe_type(ptop_level_json->type)); + return FALSE; + } + sllv_append(pobjects, validate_millerable_object(pnext_level_json)); + } + // xxx free the pointer-array?!? put this logic as a method inside json.c/h. + ptop_level_json->u.array.length = 0; } else if (ptop_level_json->type == JSON_OBJECT) { + sllv_append(pobjects, validate_millerable_object(ptop_level_json)); + return TRUE; } else { + fprintf(stderr, + "%s: found non-terminal (type %s) at top level. This is valid but unmillerable JSON.\n", + MLR_GLOBALS.argv0, json_describe_type(ptop_level_json->type)); + return FALSE; } + return TRUE; } // xxx @@ -41,5 +67,25 @@ void transfer_objects(json_value_t* ptop_level_json, sllv_t* pobjects) { // xxx validate func: return object or die json_value_t* validate_millerable_object(json_value_t* pjson) { + // xxx redundantly assert this is of type JSON_OBJECT? or just note as precondition? + int n = pjson->u.array.length; + for (int i = 0; i < n; i++) { + json_object_entry_t* pobject_entry = &pjson->u.object.values[i]; + char* key = (char*)pobject_entry->name; + json_value_t* pvalue = pobject_entry->value; + if (pvalue->type == JSON_ARRAY || pvalue->type == JSON_OBJECT) { + fprintf(stderr, + "%s: found nested non-object (type %s). This is valid but unmillerable JSON.\n", + MLR_GLOBALS.argv0, json_describe_type(pvalue->type)); + } + + printf("xxx temp key=%s\n", key); + +//typedef struct _json_object_entry_t { + //json_char * name; + //unsigned int name_length; + //struct _json_value_t * value; +//} json_object_entry_t; + } return NULL; // xxx temp } diff --git a/c/input/mlr_json.h b/c/input/mlr_json.h index 6bc1b7884..9f62908ee 100644 --- a/c/input/mlr_json.h +++ b/c/input/mlr_json.h @@ -14,7 +14,7 @@ // output: appended sllv // json value will be freed, or transferred to the sllv -void transfer_objects(json_value_t* ptop_level_json, sllv_t* pobjects); +int transfer_objects(json_value_t* ptop_level_json, sllv_t* pobjects); // xxx //switch(parsed_top_level_json->type) {