CYCLUS
Loading...
Searching...
No Matches
infile_converters.cc
Go to the documentation of this file.
1#include <sstream>
2
3#include <boost/shared_ptr.hpp>
4#include <libxml++/libxml++.h>
5
6#include "error.h"
7#include "infile_converters.h"
8#include "infile_tree.h"
9#include "pyhooks.h"
10#include "pyne.h"
11
12namespace cyclus {
13namespace toolkit {
14
15void AddJsonToXml(Json::Value& node, std::stringstream& ss,
16 std::string parent_name, std::string indent) {
17 using Json::Value;
18 using std::string;
19 if (node.isObject()) {
20 bool indent_child;
21 string name;
22 string newindent = indent + " ";
23 Value::Members members = node.getMemberNames();
24 for (int n = 0; n < members.size(); ++n) {
25 name = members[n];
26 if (node[name].isNull()) {
27 ss << indent << "<" << name << "/>\n";
28 continue;
29 }
30 indent_child = node[name].isObject();
31 if (!indent_child && node[name].isArray())
32 indent_child = node[name][0].isObject() || node[name][0].isArray();
33 ss << indent << "<" << name << ">";
34 if (indent_child) ss << "\n";
35 AddJsonToXml(node[name], ss, name, newindent);
36 if (indent_child) ss << indent;
37 ss << "</" << name << ">\n";
38 }
39 } else if (node.isArray()) {
40 bool indent_child;
41 std::string newindent = indent;
42 indent = indent.substr(0, indent.size() - 2);
43 int nchildren = node.size();
44 for (int n = 0; n < nchildren; ++n) {
45 if (node[n].isNull()) {
46 ss << indent << "<" << parent_name << "/>\n";
47 continue;
48 }
49 indent_child = node[n].isObject() || node[n].isArray();
50 if (n > 0) {
51 ss << indent << "<" << parent_name << ">";
52 if (indent_child) ss << "\n";
53 }
54 AddJsonToXml(node[n], ss, parent_name, newindent);
55 if (n < nchildren - 1) {
56 if (indent_child) ss << indent;
57 ss << "</" << parent_name << ">\n";
58 }
59 }
60 } else if (node.isString()) {
61 ss << node.asString();
62 } else if (node.isInt()) {
63 ss << node.asInt64();
64 } else if (node.isUInt()) {
65 ss << node.asUInt64();
66 } else if (node.isDouble()) {
67 ss << node.asDouble();
68 } else if (node.isBool()) {
69 ss << node.asBool();
70 } else {
71 // plain old data
72 ss << node.asString();
73 }
74}
75
76// Converts a JSON string into an equivalent XML string
77std::string JsonToXml(std::string s) {
78 using Json::Value;
79 using std::string;
80 using std::stringstream;
81
82 stringstream x;
83
84 // parse the JSON string
85 Value root;
86 Json::Reader reader;
87 bool parsed = reader.parse(s, root, false);
88 if (!parsed) {
89 string msg = "Failed to parse JSON file into XML:\n" +
91 throw ValidationError(msg);
92 }
93
94 // convert to XML
95 AddJsonToXml(root, x, "", "");
96 return x.str();
97}
98
99// inserts a value, or appends to an array
100void JsonInsertOrAppend(Json::Value& node, std::string key, Json::Value& val) {
101 using Json::Value;
102 using std::string;
103 if (node.isMember(key)) {
104 if (node[key].isArray()) {
105 node[key].append(val);
106 } else {
107 Value keynode = node[key];
108 keynode = Value(Json::arrayValue);
109 keynode.append(node[key]);
110 keynode.append(val);
111 node[key] = keynode;
112 }
113 } else {
114 node[key] = val;
115 }
116}
117
119 std::string parent_name) {
120 using Json::Value;
121 using std::string;
122 int n = xnode->NElements();
123 for (int i = 0; i < n; ++i) {
124 string name = xnode->GetElementName(i);
125 InfileTree* subxnode = xnode->SubTree("*", i);
126 Value val;
127 if (subxnode->NElements() == 0) {
128 try {
129 val = Value(subxnode->GetString("."));
130 } catch (cyclus::ValueError& e) {
131 val = Value(Json::nullValue);
132 }
133 } else {
134 val = Value(Json::objectValue);
135 AddXmlToJson(subxnode, val, name);
136 }
137 JsonInsertOrAppend(jnode, name, val);
138 }
139}
140
141// Converts an XML string into an equivalent JSON string
142std::string XmlToJson(std::string s) {
143 using Json::Value;
144 using std::string;
145 using std::stringstream;
146 stringstream ss(s);
147 boost::shared_ptr<XMLParser> parser =
148 boost::shared_ptr<XMLParser>(new XMLParser());
149 parser->Init(ss);
150 InfileTree xroot(*parser);
151 Value jroot(Json::objectValue);
152 string rootname = parser->Document()->get_root_node()->get_name();
153 jroot[rootname] = Value(Json::objectValue);
154 AddXmlToJson(&xroot, jroot[rootname], rootname);
155 Json::CustomWriter writer =
156 Json::CustomWriter("{", "}", "[", "]", ": ", ",", " ", 1);
157 return writer.write(jroot);
158}
159
160std::string PyToXml(std::string s) {
161 return JsonToXml(PyToJson(s));
162}
163
164std::string XmlToPy(std::string s) {
165 return JsonToPy(XmlToJson(s));
166}
167
168} // namespace toolkit
169} // namespace cyclus
Writes a Value in JSON format with custom formatting.
Definition pyne.h:4475
virtual std::string write(const Value &root)
Definition pyne.cc:14013
Unserialize a JSON document into a Value.
Definition pyne.h:4038
std::string getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
Definition pyne.cc:11425
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
Definition pyne.cc:10811
Represents a JSON value.
Definition pyne.h:3088
UInt64 asUInt64() const
Definition pyne.cc:12510
ArrayIndex size() const
Number of values in array or object.
Definition pyne.cc:12671
bool isArray() const
Definition pyne.cc:13001
bool isDouble() const
Definition pyne.cc:12989
bool isString() const
Definition pyne.cc:12997
Members getMemberNames() const
Return a list of the member names.
Definition pyne.cc:12924
Value & append(const Value &value)
Append value to array at the end.
Definition pyne.cc:12863
bool isObject() const
Definition pyne.cc:13005
Int64 asInt64() const
Definition pyne.cc:12484
std::string asString() const
Definition pyne.cc:12397
bool isBool() const
Definition pyne.cc:12973
bool asBool() const
Definition pyne.cc:12607
bool isUInt() const
Definition pyne.cc:12981
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition pyne.cc:12909
double asDouble() const
Definition pyne.cc:12553
bool isInt() const
Definition pyne.cc:12977
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
virtual int NElements()
virtual std::string GetElementName(int index=0)
investigates the current status and returns a string representing the name of a given index
InfileTree * SubTree(std::string query, int index=0)
populates a child infile based on a query and index
virtual std::string GetString(std::string query, int index=0)
investigates the current status and returns a string representing the content of a query at a given i...
For validating files received via IO.
Definition error.h:67
For values that are too big, too small, etc.
Definition error.h:37
A helper class to hold xml file data and provide automatic validation.
Definition xml_parser.h:15
@ nullValue
'null' value
Definition pyne.h:3012
@ arrayValue
array value (ordered list)
Definition pyne.h:3018
@ objectValue
object value (collection of name/value pairs).
Definition pyne.h:3019
std::string XmlToPy(std::string s)
Converts an XML string into an equivalent JSON string.
void JsonInsertOrAppend(Json::Value &node, std::string key, Json::Value &val)
void AddJsonToXml(Json::Value &node, std::stringstream &ss, std::string parent_name, std::string indent)
std::string JsonToXml(std::string s)
Converts a JSON string into an equivalent XML string.
std::string XmlToJson(std::string s)
Converts an XML string into an equivalent JSON string.
std::string PyToJson(std::string infile)
Convert Python simulation string to JSON.
Definition pyhooks.cc:110
std::string JsonToPy(std::string infile)
Convert JSON string to Python simulation string.
Definition pyhooks.cc:117
void AddXmlToJson(InfileTree *xnode, Json::Value &jnode, std::string parent_name)
std::string PyToXml(std::string s)
Converts a Python string into an equivalent XML string.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14