Changed JSON parser to write valid JSON 04/56804/4
authorDavid Steele <david.steele@samsung.com>
Tue, 12 Jan 2016 17:14:48 +0000 (17:14 +0000)
committerDavid Steele <david.steele@samsung.com>
Wed, 20 Jan 2016 19:15:00 +0000 (19:15 +0000)
After parsing a JSON file, calling Write on the JsonParser now writes
valid JSON as an output; also fixed the indent levels and rolled up
arrays of numbers onto a single line to make it more readable.

Change-Id: I9413f5c3f7f4695b39e4f305eccf84b638c632bb
Signed-off-by: David Steele <david.steele@partner.samsung.com>
dali-toolkit/internal/builder/tree-node-manipulator.cpp
dali-toolkit/internal/builder/tree-node-manipulator.h

index 3e10edd..926842a 100644 (file)
@@ -36,16 +36,37 @@ namespace Internal
 namespace
 {
 
 namespace
 {
 
-void Indent(std::ostream& o, int indent)
+void Indent(std::ostream& o, int level, int indentWidth)
 {
 {
-  for (int i = 0; i < indent; ++i)
+  for (int i = 0; i < level*indentWidth; ++i)
   {
     o << " ";
   }
 }
 
   {
     o << " ";
   }
 }
 
+std::string EscapeQuotes( const char* aString)
+{
+  std::string escapedString;
+  int length = strlen(aString);
+  escapedString.reserve(length);
+
+  const char* end = aString+length;
+  for( const char* iter = aString; iter != end ; ++iter)
+  {
+    if(*iter != '\"')
+    {
+      escapedString.push_back(*iter);
+    }
+    else
+    {
+      escapedString.append("\\\"");
+    }
+  }
+  return escapedString;
 }
 
 }
 
+} // anonymous namespace
+
 TreeNodeManipulator::TreeNodeManipulator(TreeNode* node)
   : mNode(node)
 {
 TreeNodeManipulator::TreeNodeManipulator(TreeNode* node)
   : mNode(node)
 {
@@ -327,14 +348,17 @@ void TreeNodeManipulator::SetBoolean( bool b )
 void TreeNodeManipulator::Write(std::ostream& output, int indent) const
 {
   DALI_ASSERT_DEBUG(mNode && "Operation on NULL JSON node");
 void TreeNodeManipulator::Write(std::ostream& output, int indent) const
 {
   DALI_ASSERT_DEBUG(mNode && "Operation on NULL JSON node");
-  DoWrite(mNode, output, indent);
+  DoWrite(mNode, output, 0, indent, false);
 }
 
 }
 
-void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, int indent) const
+void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, int level, int indentWidth, bool groupChildren) const
 {
   DALI_ASSERT_DEBUG(value && "Operation on NULL JSON node");
 
 {
   DALI_ASSERT_DEBUG(value && "Operation on NULL JSON node");
 
-  Indent(output, indent);
+  if(!groupChildren)
+  {
+    Indent(output, level, indentWidth);
+  }
 
   if (value->GetName())
   {
 
   if (value->GetName())
   {
@@ -348,9 +372,9 @@ void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, i
       output << "null";
       if(NULL != value->mNextSibling)
       {
       output << "null";
       if(NULL != value->mNextSibling)
       {
-        output << ",";
+        output << ", ";
       }
       }
-      if(indent)
+      if( !groupChildren )
       {
         output << std::endl;
       }
       {
         output << std::endl;
       }
@@ -359,58 +383,89 @@ void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, i
     case TreeNode::OBJECT:
     case TreeNode::ARRAY:
     {
     case TreeNode::OBJECT:
     case TreeNode::ARRAY:
     {
+      bool groupMyChildren = false;
+
+      if( TreeNode::ARRAY == value->GetType() &&
+          ( TreeNode::INTEGER == value->mFirstChild->GetType() ||
+            TreeNode::FLOAT   == value->mFirstChild->GetType() ) )
+      {
+        groupMyChildren = true;
+      }
+
       if( value->GetType() == TreeNode::OBJECT)
       {
       if( value->GetType() == TreeNode::OBJECT)
       {
+        output << std::endl;
+        Indent(output, level, indentWidth);
         output << "{";
         output << "{";
-        if(indent)
-        {
-          output << std::endl;
-        }
       }
       else
       {
       }
       else
       {
-        output << "[";
-        if(indent)
+        if( !groupMyChildren )
         {
           output << std::endl;
         {
           output << std::endl;
+          Indent(output, level, indentWidth);
         }
         }
+        output << "[";
+      }
+
+      if( groupMyChildren )
+      {
+        output << " ";
+      }
+      else
+      {
+        output << std::endl;
       }
 
       for (TreeNode::ConstIterator it = value->CBegin(); it != value->CEnd(); ++it)
       {
       }
 
       for (TreeNode::ConstIterator it = value->CBegin(); it != value->CEnd(); ++it)
       {
-        DoWrite( &((*it).second), output, indent + 1);
+        DoWrite( &((*it).second), output, level+1, indentWidth, groupMyChildren );
+      }
+
+      if( !groupMyChildren )
+      {
+        Indent(output, level, indentWidth);
       }
       }
-      Indent(output, indent);
+
       if( value->GetType() == TreeNode::OBJECT )
       {
         output << "}";
       if( value->GetType() == TreeNode::OBJECT )
       {
         output << "}";
-        if(indent)
-        {
-          output << std::endl;
-        }
       }
       else
       {
         output << "]";
       }
       else
       {
         output << "]";
-        if(indent)
-        {
-          output << std::endl;
-        }
       }
       }
+
+      if( NULL != value->mNextSibling )
+      {
+        output << ",";
+      }
+
+      if( !groupChildren )
+      {
+        output << std::endl;
+      }
+
+      groupChildren = false;
       break;
     }
     case TreeNode::STRING:
     {
       break;
     }
     case TreeNode::STRING:
     {
-      output << "\"" << value->GetString() << "\"";
+      std::string escapedString = EscapeQuotes(value->GetString());
+      output << "\"" << escapedString << "\"";
       if(NULL != value->mNextSibling)
       {
         output << ",";
       }
       if(NULL != value->mNextSibling)
       {
         output << ",";
       }
-      if(indent)
+
+      if( groupChildren )
+      {
+        output << " ";
+      }
+      else
       {
         output << std::endl;
       }
       {
         output << std::endl;
       }
-
       break;
     }
     case TreeNode::INTEGER:
       break;
     }
     case TreeNode::INTEGER:
@@ -420,11 +475,15 @@ void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, i
       {
         output << ",";
       }
       {
         output << ",";
       }
-      if(indent)
+
+      if( groupChildren )
+      {
+        output << " ";
+      }
+      else
       {
         output << std::endl;
       }
       {
         output << std::endl;
       }
-
       break;
     }
     case TreeNode::FLOAT:
       break;
     }
     case TreeNode::FLOAT:
@@ -436,7 +495,12 @@ void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, i
       {
         output << ",";
       }
       {
         output << ",";
       }
-      if(indent)
+
+      if( groupChildren )
+      {
+        output << " ";
+      }
+      else
       {
         output << std::endl;
       }
       {
         output << std::endl;
       }
@@ -452,11 +516,17 @@ void TreeNodeManipulator::DoWrite(const TreeNode *value, std::ostream& output, i
       {
         output << "false";
       }
       {
         output << "false";
       }
+
       if(NULL != value->mNextSibling)
       {
         output << ",";
       }
       if(NULL != value->mNextSibling)
       {
         output << ",";
       }
-      if(indent)
+
+      if( groupChildren )
+      {
+        output << " ";
+      }
+      else
       {
         output << std::endl;
       }
       {
         output << std::endl;
       }
@@ -520,4 +590,3 @@ char *CopyString( const char *fromString, VectorCharIter& iter, const VectorChar
 } // namespace Toolkit
 
 } // namespace Dali
 } // namespace Toolkit
 
 } // namespace Dali
-
index 838c2cb..f900350 100644 (file)
@@ -191,7 +191,7 @@ private:
   /*
    * Do write to string stream
    */
   /*
    * Do write to string stream
    */
-  void DoWrite(const TreeNode *value, std::ostream& output, int ident) const;
+  void DoWrite(const TreeNode *value, std::ostream& output, int level, int ident, bool groupChildren) const;
 
 };
 
 
 };