THRIFT-2859: JSON generator - fixed i64 numbers generation
Client: JSON
Patch: Stig Bakken
Also forced "C locale" for numbers - problems where detected in JSON lib
in C++, so fixing it in generator also might be a good idea.
This closes #305
diff --git a/compiler/cpp/src/generate/t_json_generator.cc b/compiler/cpp/src/generate/t_json_generator.cc
index cbf3883..1cb8584 100644
--- a/compiler/cpp/src/generate/t_json_generator.cc
+++ b/compiler/cpp/src/generate/t_json_generator.cc
@@ -85,12 +85,19 @@
std::stack<bool> comma_needed_;
template <typename T>
- string to_string(T t) {
+ string number_to_string(T t) {
std::ostringstream out;
+ out.imbue(std::locale::classic());
+ out.precision(std::numeric_limits<T>::digits10);
out << t;
return out.str();
}
+ template <typename T>
+ void write_number(T n) {
+ f_json_ << number_to_string(n);
+ }
+
string get_type_name(t_type* ttype);
string get_qualified_name(t_type* ttype);
@@ -110,8 +117,6 @@
void write_type_spec_object(const char* name, t_type* ttype);
void write_type_spec(t_type* ttype);
void write_string(const string& value);
- void write_integer(long value);
- void write_double(double value);
void write_value(t_type* tvalue);
void write_const_value(t_const_value* value, bool force_string = false);
void write_key_and(string key);
@@ -199,7 +204,7 @@
void t_json_generator::write_key_and_integer(string key, int val) {
write_comma_if_needed();
- indent(f_json_) << json_str(key) << ": " << to_string(val);
+ indent(f_json_) << json_str(key) << ": " << number_to_string(val);
indicate_comma_needed();
}
@@ -435,14 +440,6 @@
f_json_ << quot << escape_json_string(value) << quot;
}
-void t_json_generator::write_integer(long value) {
- f_json_ << to_string(value);
-}
-
-void t_json_generator::write_double(double value) {
- f_json_ << to_string(value);
-}
-
void t_json_generator::write_const_value(t_const_value* value, bool should_force_string) {
switch (value->get_type()) {
@@ -450,17 +447,17 @@
case t_const_value::CV_IDENTIFIER:
case t_const_value::CV_INTEGER:
if (should_force_string) {
- write_string(to_string(value->get_integer()));
+ write_string(number_to_string(value->get_integer()));
} else {
- write_integer(value->get_integer());
+ write_number(value->get_integer());
}
break;
case t_const_value::CV_DOUBLE:
if (should_force_string) {
- write_string(to_string(value->get_double()));
+ write_string(number_to_string(value->get_double()));
} else {
- write_double(value->get_double());
+ write_number(value->get_double());
}
break;
@@ -474,6 +471,7 @@
std::vector<t_const_value*>::iterator lit;
for (lit = list.begin(); lit != list.end(); ++lit) {
write_comma_if_needed();
+ f_json_ << indent();
write_const_value(*lit);
indicate_comma_needed();
}
@@ -487,6 +485,7 @@
std::map<t_const_value*, t_const_value*>::iterator mit;
for (mit = map.begin(); mit != map.end(); ++mit) {
write_comma_if_needed();
+ f_json_ << indent();
// JSON objects only allow string keys
write_const_value(mit->first, FORCE_STRING);
f_json_ << ": ";