From 730972c07c9de7c47f6223c80779dadc3f3aabe3 Mon Sep 17 00:00:00 2001 From: Enrico Fucile Date: Wed, 7 Jan 2015 12:36:43 +0000 Subject: [PATCH] fixed json format from bufr_dump and added json test --- src/grib_dumper_class_json.c | 27 +++++++++++++++++++++------ tests/Makefile.am | 2 +- tests/bufr_json.sh | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100755 tests/bufr_json.sh diff --git a/src/grib_dumper_class_json.c b/src/grib_dumper_class_json.c index 22baf3b19..134f5d4e5 100644 --- a/src/grib_dumper_class_json.c +++ b/src/grib_dumper_class_json.c @@ -22,6 +22,7 @@ IMPLEMENTS = init;destroy MEMBERS = long section_offset MEMBERS = long begin + MEMBERS = long empty MEMBERS = long end END_CLASS_DEF @@ -56,6 +57,7 @@ typedef struct grib_dumper_json { /* Members defined in json */ long section_offset; long begin; + long empty; long end; } grib_dumper_json; @@ -99,6 +101,7 @@ static int init(grib_dumper* d) { grib_dumper_json *self = (grib_dumper_json*)d; self->section_offset=0; + self->empty=1; return GRIB_SUCCESS; } @@ -139,9 +142,11 @@ static void dump_values(grib_dumper* d,grib_accessor* a) size = 3; } else more=0; - if (!self->begin) fprintf(self->dumper.out,",\n"); + if (self->begin==0 && self->empty==0 ) fprintf(self->dumper.out,",\n"); else self->begin=0; + self->empty=0; + err = grib_set_double(a->parent->h, "missingValue", missing_value); if (size>1) { int cols=4; @@ -209,9 +214,11 @@ static void dump_long(grib_dumper* d,grib_accessor* a,const char* comment) size = 3; } else more=0; - if (!self->begin) fprintf(self->dumper.out,",\n"); + if (self->begin==0 && self->empty==0) fprintf(self->dumper.out,",\n"); else self->begin=0; + self->empty=0; + if (size>1) { int cols=9; int count=0; @@ -260,9 +267,11 @@ static void dump_double(grib_dumper* d,grib_accessor* a,const char* comment) if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0) return; - if (!self->begin) fprintf(self->dumper.out,",\n"); + if (self->begin==0 && self->empty==0) fprintf(self->dumper.out,",\n"); else self->begin=0; + self->empty=0; + fprintf(self->dumper.out,"%-*s",mydepth," "); if( grib_is_missing_double(a,value) ) fprintf(self->dumper.out,"\"%s\" : null",a->name); @@ -294,9 +303,11 @@ static void dump_string_array(grib_dumper* d,grib_accessor* a,const char* commen return; } - if (!self->begin) fprintf(self->dumper.out,",\n"); + if (self->begin==0 && self->empty==0) fprintf(self->dumper.out,",\n"); else self->begin=0; + self->empty=0; + values=(char**)grib_context_malloc_clear(c,size*sizeof(char*)); if (!values) { grib_context_log(c,GRIB_LOG_FATAL,"unable to allocate %d bytes",(int)size); @@ -340,9 +351,11 @@ static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) return; } - if (!self->begin) fprintf(self->dumper.out,",\n"); + if (self->begin==0 && self->empty==0) fprintf(self->dumper.out,",\n"); else self->begin=0; + self->empty=0; + err = grib_unpack_string(a,value,&size); p=value; @@ -374,16 +387,18 @@ static void dump_section(grib_dumper* d,grib_accessor* a,grib_block_of_accessors ) { fprintf(self->dumper.out,"{\n"); self->begin=1; + self->empty=1; grib_dump_accessors_block(d,block); fprintf(self->dumper.out,"\n}\n"); } else if (!grib_inline_strcmp(a->name,"groupNumber")) { depth+=2; - fprintf(self->dumper.out,",\n"); + if (!self->empty) fprintf(self->dumper.out,",\n"); fprintf(self->dumper.out,"%-*s",depth," "); fprintf(self->dumper.out,"\"group%d\" : {",(int)a->bufr_group_number); fprintf(self->dumper.out,"\n"); /* fprintf(self->dumper.out,"%-*s",depth," "); */ self->begin=1; + self->empty=1; grib_dump_accessors_block(d,block); fprintf(self->dumper.out,"\n"); fprintf(self->dumper.out,"%-*s",depth," "); diff --git a/tests/Makefile.am b/tests/Makefile.am index f895a8032..809b4b8ee 100755 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,5 @@ TESTS = definitions.sh calendar.sh \ - bufrdc_desc_ref.sh bufrdc_ref.sh bufr_dump.sh \ + bufrdc_desc_ref.sh bufrdc_ref.sh bufr_dump.sh bufr_json.sh\ ieee.sh grib1to2.sh \ unit_tests.sh \ grib2to1.sh badgrib.sh ls.sh filter.sh \ diff --git a/tests/bufr_json.sh b/tests/bufr_json.sh new file mode 100755 index 000000000..f8530f7ca --- /dev/null +++ b/tests/bufr_json.sh @@ -0,0 +1,30 @@ +#!/bin/sh +# Copyright 2005-2015 ECMWF. +# +# This software is licensed under the terms of the Apache Licence Version 2.0 +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. +# +# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by +# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. +# + +set -e + +. ./include.sh + +REDIRECT=/dev/null + +for file in ${data_dir}/bufr/*.bufr +do + + rm -f ${file}.json | true + + ${tools_dir}bufr_dump -j $file 2> $REDIRECT > ${file}.json + + + json_xs < ${file}.json >$REDIRECT 2> $REDIRECT + + rm -f ${file}.json + +done +