json_extract_field.c
Go to the documentation of this file.
1 #include <options/options.h>
2 #include "../csm/csm_all.h"
3 
4 void jo_write_plain(JO jo, FILE* out);
5 
6 int main(int argc, const char * argv[]) {
7  sm_set_program_name(argv[0]);
8 
9  const char*input_filename;
10  const char*output_filename;
11  const char*field;
12  int exit_on_error;
13 
14  struct option* ops = options_allocate(3);
15  options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
16  options_string(ops, "out", &output_filename, "stdout", "output file (JSON)");
17  options_int(ops, "exit_on_error", &exit_on_error, 0, "if true, exit if object has no field");
18  options_string(ops, "field", &field, "field_name", "field to extract from structure");
19 
20  if(!options_parse_args(ops, argc, argv)) {
21  sm_info("Extracts a field from JSON object."
22  "\n\nOptions:\n");
23  options_print_help(ops, stderr);
24  return -1;
25  }
26 
27  FILE * input_stream = open_file_for_reading(input_filename);
28  FILE *output_stream = open_file_for_writing(output_filename);
29 
30  if(!input_stream || !output_stream) return -1;
31 
32  int n=0;
33  while(1) {
34  n++;
35 
36  JO jo = json_read_stream(input_stream);
37  if(!jo) {
38  if(feof(input_stream)) break;
39  sm_error("Error while reading stream.\n");
40  return -2;
41  }
42 
43  JO jo_field = jo_get(jo, field);
44  if(!jo_field) {
45  if(exit_on_error) {
46  sm_error("object #%d: field '%s' not found in structure.\n", n, field);
47  return -1;
48  } else {
49  sm_error("object #%d: field '%s' not found in structure.\n", n, field);
50  continue;
51  }
52  }
53 
54  jo_write_plain(jo_field, output_stream);
55  fputs("\n", output_stream);
56  jo_free(jo);
57  };
58 
59  return 0;
60 }
61 
62 void jo_write_plain(JO jo, FILE* out) {
63  switch(json_object_get_type(jo)) {
64  case json_type_boolean: {
65  int v = (int) json_object_get_boolean(jo);
66  fprintf(out, "%d", v);
67  break;
68  }
69  case json_type_int: {
70  int v = json_object_get_int(jo);
71  fprintf(out, "%d", v);
72  break;
73  }
74  case json_type_double: {
75  double v = json_object_get_double(jo);
76  fprintf(out, "%g", v);
77  break;
78  }
79  case json_type_string: {
80  const char * s = json_object_get_string(jo);
81  fputs(s, out);
82  break;
83  }
84  case json_type_object: {
85  fputs("{object}", out);
86  break;
87  }
88  case json_type_array: {
89  int k, len = json_object_array_length(jo);
90  for(k=0; k<len; k++) {
91  JO v = json_object_array_get_idx(jo, k);
92  jo_write_plain(v, out);
93  if(k!=len-1) fputs(" ", out);
94  }
95  break;
96  }
97 
98  default:
99  sm_error("Unknown JSON type %d.\n", json_object_get_type(jo) );
100  }
101 
102 }
103 
char * json_object_get_string(struct json_object *this)
Definition: json_object.c:452
int json_object_array_length(struct json_object *this)
Definition: json_object.c:516
double json_object_get_double(struct json_object *this)
Definition: json_object.c:395
void sm_set_program_name(const char *name)
Definition: logging.c:21
int main(int argc, const char *argv[])
enum json_type json_object_get_type(struct json_object *this)
Definition: json_object.c:190
struct option * ops
Definition: rb_sm.c:31
int json_object_get_int(struct json_object *this)
Definition: json_object.c:341
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
Definition: options.h:49
struct option * options_allocate(int n)
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
void jo_write_plain(JO jo, FILE *out)
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
#define jo_get
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void sm_info(const char *msg,...)
Definition: logging.c:71
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
JO json_read_stream(FILE *f)
struct json_object * json_object_array_get_idx(struct json_object *this, int idx)
Definition: json_object.c:535
void sm_error(const char *msg,...)
Definition: logging.c:49
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
boolean json_object_get_boolean(struct json_object *this)
Definition: json_object.c:306
#define jo_free


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23