40 using std::ostringstream;
41 using std::istringstream;
45 #include <Structure.h>
46 #include <Constructor.h>
59 #define FoDapJsonTransform_debug_key "fojson"
66 template<
typename T>
unsigned int FoDapJsonTransform::json_simple_type_array_worker(ostream *strm, T *values,
unsigned int indx, vector<unsigned int> *shape,
unsigned int currentDim){
70 unsigned int currentDimSize = (*shape)[currentDim];
72 for(
unsigned int i=0; i<currentDimSize ;i++){
73 if(currentDim < shape->size()-1){
75 <<
" currentDim: " << currentDim
76 <<
" currentDimSize: " << currentDimSize
78 indx = json_simple_type_array_worker<T>(strm,values,indx,shape,currentDim+1);
79 if(i+1 != currentDimSize)
85 if(
typeid(T) ==
typeid(std::string)){
87 std::string val = ((std::string *) values)[indx++];
91 *strm << values[indx++];
106 template<
typename T>
void FoDapJsonTransform::json_simple_type_array(ostream *strm, libdap::Array *a,
string indent,
bool sendData){
109 *strm << indent <<
"{" << endl;\
111 string childindent = indent + _indent_increment;
113 writeLeafMetadata(strm,a,childindent);
115 int numDim = a->dimensions(
true);
116 vector<unsigned int> shape(numDim);
119 *strm << childindent <<
"\"shape\": [";
121 for(std::vector<unsigned int>::size_type i=0; i<shape.size() ;i++){
132 *strm << childindent <<
"\"data\": ";
134 T *src =
new T[length];
136 indx = json_simple_type_array_worker(strm, src, 0, &shape, 0);
143 *strm << endl << indent <<
"}";
155 void FoDapJsonTransform::json_string_array(std::ostream *strm, libdap::Array *a,
string indent,
bool sendData)
158 *strm << indent <<
"{" << endl;\
160 string childindent = indent + _indent_increment;
162 writeLeafMetadata(strm,a,childindent);
164 int numDim = a->dimensions(
true);
165 vector<unsigned int> shape(numDim);
168 *strm << childindent <<
"\"shape\": [";
170 for(std::vector<unsigned int>::size_type i=0; i<shape.size() ;i++){
181 *strm << childindent <<
"\"data\": ";
185 vector<std::string> sourceValues;
186 a->value(sourceValues);
187 indx = json_simple_type_array_worker(strm, (std::string *)(&sourceValues[0]), 0, &shape, 0);
195 *strm << endl << indent <<
"}";
204 void FoDapJsonTransform::writeDatasetMetadata(ostream *strm, libdap::DDS *dds,
string indent){
207 *strm << indent <<
"\"name\": \""<< dds->get_dataset_name() <<
"\"," << endl;
210 transform(strm, dds->get_attr_table(), indent);
211 *strm <<
"," << endl;
219 void FoDapJsonTransform::writeNodeMetadata(ostream *strm, libdap::BaseType *bt,
string indent){
222 *strm << indent <<
"\"name\": \""<< bt->name() <<
"\"," << endl;
225 transform(strm, bt->get_attr_table(), indent);
226 *strm <<
"," << endl;
236 void FoDapJsonTransform::writeLeafMetadata(ostream *strm, libdap::BaseType *bt,
string indent){
239 *strm << indent <<
"\"name\": \""<< bt->name() <<
"\"," << endl;
244 if(bt->type() == libdap::dods_array_c){
245 libdap::Array *a = (libdap::Array *)bt;
246 *strm << indent <<
"\"type\": \""<< a->var()->type_name() <<
"\"," << endl;
249 *strm << indent <<
"\"type\": \""<< bt->type_name() <<
"\"," << endl;
254 transform(strm, bt->get_attr_table(), indent);
255 *strm <<
"," << endl;
276 _dds(dds), _localfile(localfile), _indent_increment(
" "), _ostrm(0)
279 throw BESInternalError(
"File out JSON, null DDS passed to constructor", __FILE__, __LINE__);
281 if (_localfile.empty())
282 throw BESInternalError(
"File out JSON, empty local file name passed to constructor", __FILE__, __LINE__);
286 _dds(dds), _localfile(
""), _indent_increment(
" "), _ostrm(ostrm)
289 throw BESInternalError(
"File out JSON, null DDS passed to constructor", __FILE__, __LINE__);
292 throw BESInternalError(
"File out JSON, null stream pointer passed to constructor", __FILE__, __LINE__);
314 strm <<
BESIndent::LMarg <<
"FoDapJsonTransform::dump - (" << (
void *)
this <<
")" << endl;
334 void FoDapJsonTransform::transform(
bool sendData)
337 bool used_temp_file =
false;
341 temp_file.open(_localfile.c_str(), std::fstream::out);
343 throw BESInternalError(
"Could not open temp file: " + _localfile, __FILE__, __LINE__);
345 used_temp_file =
true;
349 transform(_ostrm, _dds,
"", sendData);
365 void FoDapJsonTransform::transform(ostream *strm, libdap::Constructor *cnstrctr,
string indent,
bool sendData){
366 vector<libdap::BaseType *> leaves;
367 vector<libdap::BaseType *> nodes;
371 libdap::DDS::Vars_iter vi = cnstrctr->var_begin();
372 libdap::DDS::Vars_iter ve = cnstrctr->var_end();
373 for (; vi != ve; vi++) {
374 if ((*vi)->send_p()) {
375 libdap::BaseType *v = *vi;
376 v->is_constructor_type();
377 libdap::Type type = v->type();
378 if(type == libdap::dods_array_c){
379 type = v->var()->type();
381 if(v->is_constructor_type() ||
382 (v->is_vector_type() && v->var()->is_constructor_type())){
392 *strm << indent <<
"{" << endl ;
393 string child_indent = indent + _indent_increment;
396 writeNodeMetadata(strm, cnstrctr, child_indent);
398 transform_node_worker(strm, leaves, nodes, child_indent, sendData);
400 *strm << indent <<
"}" << endl;
408 void FoDapJsonTransform::transform_node_worker(ostream *strm, vector<libdap::BaseType *> leaves, vector<libdap::BaseType *> nodes,
string indent,
bool sendData){
411 *strm << indent <<
"\"leaves\": [";
412 if(leaves.size() > 0)
414 for(std::vector<libdap::BaseType *>::size_type l=0; l< leaves.size(); l++){
415 libdap::BaseType *v = leaves[l];
421 transform(strm, v, indent + _indent_increment, sendData);
424 *strm << endl << indent;
425 *strm <<
"]," << endl;
429 *strm << indent <<
"\"nodes\": [";
432 for(std::vector<libdap::BaseType *>::size_type n=0; n< nodes.size(); n++){
433 libdap::BaseType *v = nodes[n];
434 transform(strm, v, indent + _indent_increment, sendData);
437 *strm << endl << indent;
439 *strm <<
"]" << endl;
449 void FoDapJsonTransform::transform(ostream *strm, libdap::DDS *dds,
string indent,
bool sendData){
456 vector<libdap::BaseType *> leaves;
457 vector<libdap::BaseType *> nodes;
459 libdap::DDS::Vars_iter vi = dds->var_begin();
460 libdap::DDS::Vars_iter ve = dds->var_end();
461 for (; vi != ve; vi++) {
462 if ((*vi)->send_p()) {
463 libdap::BaseType *v = *vi;
464 libdap::Type type = v->type();
465 if(type == libdap::dods_array_c){
466 type = v->var()->type();
468 if(v->is_constructor_type() ||
469 (v->is_vector_type() && v->var()->is_constructor_type())){
479 *strm << indent <<
"{" << endl ;
480 string child_indent = indent + _indent_increment;
483 writeDatasetMetadata(strm, dds, child_indent);
485 transform_node_worker(strm, leaves, nodes, child_indent, sendData);
487 *strm << indent <<
"}" << endl;
496 void FoDapJsonTransform::transform(ostream *strm, libdap::BaseType *bt,
string indent,
bool sendData)
500 case libdap::dods_byte_c:
501 case libdap::dods_int16_c:
502 case libdap::dods_uint16_c:
503 case libdap::dods_int32_c:
504 case libdap::dods_uint32_c:
505 case libdap::dods_float32_c:
506 case libdap::dods_float64_c:
507 case libdap::dods_str_c:
508 case libdap::dods_url_c:
509 transformAtomic(strm, bt, indent, sendData);
512 case libdap::dods_structure_c:
513 transform(strm, (libdap::Structure *) bt, indent, sendData);
516 case libdap::dods_grid_c:
517 transform(strm, (libdap::Grid *) bt, indent, sendData);
520 case libdap::dods_sequence_c:
521 transform(strm, (libdap::Sequence *) bt, indent, sendData);
524 case libdap::dods_array_c:
525 transform(strm, (libdap::Array *) bt, indent, sendData);
528 case libdap::dods_int8_c:
529 case libdap::dods_uint8_c:
530 case libdap::dods_int64_c:
531 case libdap::dods_uint64_c:
533 case libdap::dods_enum_c:
534 case libdap::dods_group_c:
536 string s = (string)
"File out JSON, " +
"DAP4 types not yet supported.";
543 string s = (string)
"File out JSON, " +
"Unrecognized type.";
556 void FoDapJsonTransform::transformAtomic(ostream *strm, libdap::BaseType *b,
string indent,
bool sendData){
558 *strm << indent <<
"{" << endl;
560 string childindent = indent + _indent_increment;
562 writeLeafMetadata(strm, b, childindent);
564 *strm << childindent <<
"\"shape\": [1]," << endl;
568 *strm << childindent <<
"\"data\": [";
570 if(b->type() == libdap::dods_str_c || b->type() == libdap::dods_url_c ){
571 libdap::Str *strVar = (libdap::Str *)b;
572 std::string tmpString = strVar->value();
576 b->print_val(*strm,
"",
false);
590 void FoDapJsonTransform::transform(ostream *strm, libdap::Array *a,
string indent,
bool sendData){
593 <<
" a->type(): " << a->type()
594 <<
" a->var()->type(): " << a->var()->type()
597 switch(a->var()->type()){
599 case libdap::dods_byte_c:
600 json_simple_type_array<libdap::dods_byte>(strm,a,indent,sendData);
603 case libdap::dods_int16_c:
604 json_simple_type_array<libdap::dods_int16>(strm,a,indent,sendData);
607 case libdap::dods_uint16_c:
608 json_simple_type_array<libdap::dods_uint16>(strm,a,indent,sendData);
611 case libdap::dods_int32_c:
612 json_simple_type_array<libdap::dods_int32>(strm,a,indent,sendData);
615 case libdap::dods_uint32_c:
616 json_simple_type_array<libdap::dods_uint32>(strm,a,indent,sendData);
619 case libdap::dods_float32_c:
620 json_simple_type_array<libdap::dods_float32>(strm,a,indent,sendData);
623 case libdap::dods_float64_c:
624 json_simple_type_array<libdap::dods_float64>(strm,a,indent,sendData);
627 case libdap::dods_str_c:
629 json_string_array(strm,a,indent,sendData);
632 string s = (string)
"File out JSON, " +
"Arrays of String objects not a supported return type.";
638 case libdap::dods_url_c:
640 json_string_array(strm,a,indent,sendData);
643 string s = (string)
"File out JSON, " +
"Arrays of URL objects not a supported return type.";
650 case libdap::dods_structure_c:
652 string s = (string)
"File out JSON, " +
"Arrays of Structure objects not a supported return type.";
656 case libdap::dods_grid_c:
658 string s = (string)
"File out JSON, " +
"Arrays of Grid objects not a supported return type.";
663 case libdap::dods_sequence_c:
665 string s = (string)
"File out JSON, " +
"Arrays of Sequence objects not a supported return type.";
670 case libdap::dods_array_c:
672 string s = (string)
"File out JSON, " +
"Arrays of Array objects not a supported return type.";
676 case libdap::dods_int8_c:
677 case libdap::dods_uint8_c:
678 case libdap::dods_int64_c:
679 case libdap::dods_uint64_c:
681 case libdap::dods_enum_c:
682 case libdap::dods_group_c:
684 string s = (string)
"File out JSON, " +
"DAP4 types not yet supported.";
691 string s = (string)
"File out JSON, " +
"Unrecognized type.";
705 void FoDapJsonTransform::transform(ostream *strm, libdap::AttrTable &attr_table,
string indent){
707 string child_indent = indent + _indent_increment;
710 *strm << indent <<
"\"attributes\": [";
718 if(attr_table.get_size() != 0) {
720 libdap::AttrTable::Attr_iter begin = attr_table.attr_begin();
721 libdap::AttrTable::Attr_iter end = attr_table.attr_end();
724 for(libdap::AttrTable::Attr_iter at_iter=begin; at_iter !=end; at_iter++){
726 switch (attr_table.get_attr_type(at_iter)){
727 case libdap::Attr_container:
729 libdap::AttrTable *atbl = attr_table.get_attr_table(at_iter);
732 if(at_iter != begin )
733 *strm <<
"," << endl;
736 *strm << child_indent <<
"{" << endl;
739 if(atbl->get_name().length()>0)
740 *strm << child_indent + _indent_increment <<
"\"name\": \"" << atbl->get_name() <<
"\"," << endl;
744 transform(strm, *atbl, child_indent + _indent_increment);
745 *strm << endl << child_indent <<
"}";
754 *strm <<
"," << endl;
757 *strm << child_indent <<
"{\"name\": \""<< attr_table.get_name(at_iter) <<
"\", ";
760 *strm <<
"\"value\": [";
761 vector<std::string> *values = attr_table.get_attr_vector(at_iter);
763 for(std::vector<std::string>::size_type i=0; i<values->size() ;i++){
770 if(attr_table.get_attr_type(at_iter) == libdap::Attr_string || attr_table.get_attr_type(at_iter) == libdap::Attr_url){
778 *strm << (*values)[i] ;
789 *strm << endl << indent;
exception thrown if inernal error encountered
static ostream & LMarg(ostream &strm)
long computeConstrainedShape(libdap::Array *a, std::vector< unsigned int > *shape)
Compute the constrained shape of the Array and return it in a vector.
std::string escape_for_json(const std::string &input)
Structure storing information used by the BES to handle the request.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream