json-input iterate

This commit is contained in:
John Kerl 2016-02-01 19:30:14 -05:00
parent 66ce53225a
commit e9b6ccf6a1
5 changed files with 74 additions and 4 deletions

View file

@ -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;
}
}

View file

@ -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

View file

@ -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;

View file

@ -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
}

View file

@ -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) {