[cig-commits] commit: Make json conversion happen in memory, rather than writing an xml file

Mercurial hg at geodynamics.org
Thu Nov 10 01:10:22 PST 2011


changeset:   401:0b7dc0420159
tag:         tip
user:        Walter Landry <wlandry at caltech.edu>
date:        Thu Nov 10 01:10:10 2011 -0800
files:       json_parser/parse_json.cxx
description:
Make json conversion happen in memory, rather than writing an xml file


diff -r 953f8cc71b9a -r 0b7dc0420159 json_parser/parse_json.cxx
--- a/json_parser/parse_json.cxx	Wed Nov 09 20:35:29 2011 -0800
+++ b/json_parser/parse_json.cxx	Thu Nov 10 01:10:10 2011 -0800
@@ -1,44 +1,27 @@
 #include <json_spirit_reader.h>
 #include <json_spirit_error_position.h>
 #include <iostream>
-#include <fstream>
 #include <string>
 #include <boost/filesystem.hpp>
 #include <boost/filesystem/fstream.hpp>
+#include <sstream>
 
 #include "print_xml.h"
 
 void add_defaults(json_spirit::Value &root);
 
-boost::filesystem::path parse_json(const boost::filesystem::path &filename)
+std::string parse_json(const boost::filesystem::path &filename)
 {
-  boost::filesystem::path xml_name(filename.stem().string()+"_json.xml");
+  std::stringstream result;
   try
     {
       json_spirit::Value root;
       boost::filesystem::ifstream infile(filename);
       json_spirit::read_or_throw(infile,root);
-
       add_defaults(root);
 
-      json_spirit::Object &o(root.get_obj());
-      for(json_spirit::Object::iterator i=o.begin(); i!=o.end(); ++i)
-        {
-          if(i->name_=="outputPath")
-            {
-              if(i->value_.type()!=json_spirit::str_type)
-                throw
-                  std::runtime_error("The field 'outputPath' must be a string");
-              xml_name=i->value_.get_str() / xml_name;
-              if(!boost::filesystem::exists(i->value_.get_str()))
-                boost::filesystem::create_directories(i->value_.get_str());
-              break;
-            }
-        }
-
-      std::ofstream outfile(xml_name.c_str());
-      outfile << "<?xml version=\"1.0\"?>\n";
-      print_xml(outfile,"StGermainData",root);
+      result << "<?xml version=\"1.0\"?>\n";
+      print_xml(result,"StGermainData",root);
     }
   catch (json_spirit::Error_position &e)
     {
@@ -47,13 +30,11 @@ boost::filesystem::path parse_json(const
                 << e.column_ << "\n\t"
                 << e.reason_
                 << "\n";
-      xml_name.clear();
     }
   catch (std::exception &e)
     {
       std::cerr << e.what()
                 << "\n";
-      xml_name.clear();
     }
-  return xml_name;
+  return result.str();
 }



More information about the CIG-COMMITS mailing list