[ 
https://issues.apache.org/jira/browse/THRIFT-2859?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14235216#comment-14235216
 ] 

Stig Bakken commented on THRIFT-2859:
-------------------------------------

What about this patch (based on Konrad's, but setting precision for doubles and 
fixing an indent bug):
{code}
diff --git a/compiler/cpp/src/generate/t_json_generator.cc 
b/compiler/cpp/src/generate/t_json_generator.cc
index cbf3883..e79c9b4 100644
--- a/compiler/cpp/src/generate/t_json_generator.cc
+++ b/compiler/cpp/src/generate/t_json_generator.cc
@@ -91,6 +91,14 @@ private:
     return out.str();
   }

+  template <typename T>
+  void write_number(T n) {
+    std::ostringstream out;
+    out.precision(9);
+    out << n;
+    f_json_ << out.str();
+  }
+
   string get_type_name(t_type* ttype);
   string get_qualified_name(t_type* ttype);

@@ -110,8 +118,6 @@ private:
   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);
@@ -435,14 +441,6 @@ void t_json_generator::write_string(const string& value) {
   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()) {
@@ -452,7 +450,7 @@ void t_json_generator::write_const_value(t_const_value* 
value, bool should_force
     if (should_force_string) {
       write_string(to_string(value->get_integer()));
     } else {
-      write_integer(value->get_integer());
+      write_number(value->get_integer());
     }
     break;

@@ -460,7 +458,7 @@ void t_json_generator::write_const_value(t_const_value* 
value, bool should_force
     if (should_force_string) {
       write_string(to_string(value->get_double()));
     } else {
-      write_double(value->get_double());
+      write_number(value->get_double());
     }
     break;

@@ -474,6 +472,7 @@ void t_json_generator::write_const_value(t_const_value* 
value, bool should_force
     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 +486,7 @@ void t_json_generator::write_const_value(t_const_value* 
value, bool should_force
     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_ << ": ";
{code}


> JSON generator: output complete descriptors
> -------------------------------------------
>
>                 Key: THRIFT-2859
>                 URL: https://issues.apache.org/jira/browse/THRIFT-2859
>             Project: Thrift
>          Issue Type: Improvement
>          Components: JSON - Compiler
>    Affects Versions: 0.9.2
>            Reporter: Stig Bakken
>            Assignee: Jens Geyer
>            Priority: Minor
>             Fix For: 0.9.3
>
>
> (Someone should probably add a Jira component for the JSON generator.)
> The attached pull request contains some improvements to the JSON generator:
> 1. programs are no longer merged by default, use "--gen json:merge"
> 2. "index" is renamed to "key" for fields to match the compiler's internal 
> naming
> 3. types are described in a way that does not require string parsing
> 4. full support for exceptions, unions, namespaces, includes and 
> required/optional fields



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to