json_extract_field.c
Go to the documentation of this file.
00001 #include <options/options.h>
00002 #include "../csm/csm_all.h"
00003 
00004 void jo_write_plain(JO jo, FILE* out);
00005 
00006 int main(int argc, const char * argv[]) {
00007         sm_set_program_name(argv[0]);
00008         
00009         const char*input_filename;
00010         const char*output_filename;
00011         const char*field;
00012         int exit_on_error;
00013         
00014         struct option* ops = options_allocate(3);
00015         options_string(ops, "in", &input_filename, "stdin", "input file (JSON)");
00016         options_string(ops, "out", &output_filename, "stdout", "output file (JSON)");
00017         options_int(ops, "exit_on_error", &exit_on_error, 0, "if true, exit if object has no field");
00018         options_string(ops, "field", &field, "field_name", "field to extract from structure");
00019         
00020         if(!options_parse_args(ops, argc, argv)) {
00021                 sm_info("Extracts a field from JSON object."
00022                         "\n\nOptions:\n");
00023                 options_print_help(ops, stderr);
00024                 return -1;
00025         }
00026         
00027         FILE * input_stream = open_file_for_reading(input_filename);
00028         FILE *output_stream = open_file_for_writing(output_filename);
00029         
00030         if(!input_stream || !output_stream) return -1;
00031         
00032         int n=0;
00033         while(1) {
00034                 n++;
00035                 
00036                 JO jo = json_read_stream(input_stream);
00037                 if(!jo) {
00038                         if(feof(input_stream)) break;
00039                         sm_error("Error while reading stream.\n");
00040                         return -2;
00041                 }
00042                 
00043                 JO jo_field = jo_get(jo, field);
00044                 if(!jo_field) {
00045                         if(exit_on_error) {
00046                                 sm_error("object #%d: field '%s' not found in structure.\n", n, field);
00047                                 return -1;
00048                         } else {
00049                                 sm_error("object #%d: field '%s' not found in structure.\n", n, field);
00050                                 continue;
00051                         }
00052                 }
00053                 
00054                 jo_write_plain(jo_field, output_stream);
00055                 fputs("\n", output_stream);
00056                 jo_free(jo);
00057         };
00058         
00059         return 0;
00060 }
00061 
00062 void jo_write_plain(JO jo, FILE* out) {
00063         switch(json_object_get_type(jo)) {
00064                 case json_type_boolean: {
00065                         int v = (int) json_object_get_boolean(jo);
00066                         fprintf(out, "%d", v);
00067                         break;
00068                 }
00069                 case json_type_int: {
00070                         int v = json_object_get_int(jo);
00071                         fprintf(out, "%d", v);
00072                         break;
00073                 }
00074                 case json_type_double: {
00075                         double v = json_object_get_double(jo);
00076                         fprintf(out, "%g", v);
00077                         break;
00078                 }
00079                 case json_type_string: {
00080                         const char * s = json_object_get_string(jo);
00081                         fputs(s, out);
00082                         break;
00083                 }
00084                 case json_type_object: {
00085                         fputs("{object}", out);
00086                         break;
00087                 }
00088                 case json_type_array: {
00089                         int k, len = json_object_array_length(jo);
00090                         for(k=0; k<len; k++) {
00091                                 JO v = json_object_array_get_idx(jo, k);
00092                                 jo_write_plain(v, out);
00093                                 if(k!=len-1) fputs(" ", out);
00094                         }
00095                         break;
00096                 }
00097 
00098         default:
00099                         sm_error("Unknown JSON type %d.\n", json_object_get_type(jo) );
00100         }
00101         
00102 }
00103 


csm
Author(s): Andrea Censi
autogenerated on Fri May 17 2019 02:28:33