<commit_before>#include <ossim/base/KwlNodeJsonFormatter.h>


namespace ossim
{
   void KwlNodeJsonFormatter::write(std::ostream &out,
                                 const FormatHints &hints) const
   {
      ossim_uint32 nChildren = m_kwlNode->getChildren().size();
      ossim_uint32 childIdx = 0;
      if(!m_rootTag.empty())
      {
         out << "{\"" << m_rootTag << "\" : ";
      }
      out << "{";
      for (auto n : m_kwlNode->getChildren())
      {
         toJSON(out, n.second.get(), 0, hints);
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << ",";
         }
      }
      out << "}";
      if (!m_rootTag.empty())
      {
         out << "}";
      }
   }

   void KwlNodeJsonFormatter::toJSON(std::ostream &out, const KwlNode *currentNode,
                                     int indent,
                                     const FormatHints &hints) const
   {
      std::string indentStr = hints.m_prettyPrint ? std::string(indent, ' ') : "";
      std::string separator = hints.m_prettyPrint ? "\n" : "";
      bool keyEmpty = currentNode->getKey().empty();
      ossimString value = currentNode->getValue();
      bool needsCommaForAttributes = false;

      if (currentNode->getIsArray())
      {
         out << "\"" << currentNode->getKey() << "\" : ";
         toJSONArrayType(out, currentNode, indent, hints);
      }
      else
      {
         // special case for attributes
         needsCommaForAttributes = true;

         if (currentNode->hasChildren() ||
             currentNode->hasAttributes())
         {
            out << "\"" << currentNode->getKey() << "\" : ";
            out << "{";
            toJSONChildren(out, currentNode, indent, hints);
            if (currentNode->hasAttributes())
            {
               bool needsCommaForAttributes = currentNode->hasChildren();
               if (needsCommaForAttributes)
                  out << ",";
               toJSONAttributes(out, currentNode, indent, hints);
            }
            out << "}";
         }
         else
         {
            std::string indentValueStr = hints.m_prettyPrint ? std::string(indent + hints.m_indent, ' ') : "";
            out << "\"" << currentNode->getKey() << "\" : \""
                << fixValue(currentNode->getValue()) << "\"";

            // toJSONAttributes(out, currentNode, indent, hints);
         }
      }
   }
   void KwlNodeJsonFormatter::toJSONAttributes(std::ostream &out,
                                               const KwlNode *currentNode,
                                               int indent,
                                               const FormatHints &hints) const
   {
      ossim_uint32 nChildren = currentNode->getAttributes().size();
      ossim_uint32 childIdx = 0;
      for (auto attribute : currentNode->getAttributes())
      {
         out << "\"" << attribute.second->getKey()
             << "\" : \"" << fixValue(attribute.second->getValue()) << "\"";
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << ",";
         }
      }
   }
   void KwlNodeJsonFormatter::toJSONChildren(std::ostream &out,
                                             const KwlNode *currentNode,
                                             int indent,
                                             const FormatHints &hints) const
   {
      ossim_uint32 childIdx = 0;
      ossim_uint32 nChildren = currentNode->getChildren().size();
      if (!nChildren)
         return;
      for (auto child : currentNode->getChildren())
      {
         toJSON(out, child.second.get(), indent + hints.m_indent, hints);
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << ",";
         }
      }
   }
   void KwlNodeJsonFormatter::toJSONArrayType(std::ostream &out,
                                              const KwlNode *currentNode,
                                              int indent,
                                              const FormatHints &hints) const
   {
      ossim_uint32 nChildren = currentNode->getChildren().size();
      ossim_uint32 childIdx = 0;
      out << "[";
      // sort the child array
      std::map<ossim_int32, std::shared_ptr<KwlNode>> sortedMap;
      for (auto x : currentNode->getChildren())
      {
         sortedMap.insert(std::make_pair(x.first.toInt32(), x.second));
      }
      // iterate through sorted array
      for (auto child : sortedMap)
      {
         if (child.second->hasChildren())
         {
            ossim_uint32 nGrandChildren = child.second->getChildren().size();
            ossim_uint32 grandChildIdx = 0;
            out << "{";
            for (auto skippedChild : child.second->getChildren())
            {
               toJSON(out, skippedChild.second.get(), indent + hints.m_indent, hints);
               ++grandChildIdx;
               if (grandChildIdx != nGrandChildren)
               {
                  out << ",";
               }
            }
            out << "}";
         }
         else
         {
            out << "\"" << fixValue(child.second->getValue()) << "\"";
         }
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << ",";
         }
      }
      out << "]";
   }

   ossimString KwlNodeJsonFormatter::fixValue(const ossimString &value) const
   {
      ossimString result(value);

      result = result.substitute("\n", "\\n", true);
      result = result.substitute("\"", "'", true);

      return result;
   }
}<commit_msg>Added support for pretty printing<commit_after>#include <ossim/base/KwlNodeJsonFormatter.h>


namespace ossim
{
   void KwlNodeJsonFormatter::write(std::ostream &out,
                                 const FormatHints &hints) const
   {
      ossim_uint32 indent = hints.m_indent;
      std::string separator = hints.m_prettyPrint ? "\n" : "";
      std::string indentStr = hints.m_prettyPrint ? std::string(hints.m_indent, ' ') : "";
      ossim_uint32 nChildren = m_kwlNode->getChildren().size();
      ossim_uint32 childIdx = 0;
      if(!m_rootTag.empty())
      {
         indent += hints.m_indent;
         out << "{\"" << separator << indentStr << m_rootTag << "\" : ";
      }
      out << "{" << separator;
      for (auto n : m_kwlNode->getChildren())
      {
         toJSON(out, n.second.get(), indent, hints);
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << "," << separator;
         }
      }
      if(indent)
      {
         out << indentStr << separator << "}" << separator;
      }
      else
      {
         out << separator << "}" << separator;
      }
      if (!m_rootTag.empty())
      {
         out << separator << "}" << separator;
      }
   }

   void KwlNodeJsonFormatter::toJSON(std::ostream &out, 
                                     const KwlNode *currentNode,
                                     int indent,
                                     const FormatHints &hints) const
   {
      std::string indentStr = hints.m_prettyPrint ? std::string(indent, ' ') : "";
      std::string separator = hints.m_prettyPrint ? "\n" : "";
      bool keyEmpty = currentNode->getKey().empty();
      ossimString value = currentNode->getValue();
      bool needsCommaForAttributes = false;

      if (currentNode->getIsArray())
      {
         out << indentStr << "\"" << currentNode->getKey() << "\" : ";
         toJSONArrayType(out, currentNode, indent, hints);
      }
      else
      {
         // special case for attributes
         needsCommaForAttributes = true;

         if (currentNode->hasChildren() ||
             currentNode->hasAttributes())
         {
            out << indentStr << "\"" << currentNode->getKey() << "\" : ";
            out << "{" << separator;
            toJSONChildren(out, currentNode, indent, hints);
            if (currentNode->hasAttributes())
            {
               bool needsCommaForAttributes = currentNode->hasChildren();
               if (needsCommaForAttributes)
                  out << "," << separator;
               toJSONAttributes(out, currentNode, indent + hints.m_indent, hints);
            }
            out << separator << indentStr << "}";
         }
         else
         {
            std::string indentValueStr = hints.m_prettyPrint ? std::string(indent + hints.m_indent, ' ') : "";
            out << indentStr << "\"" << currentNode->getKey() << "\" : \""
                << fixValue(currentNode->getValue()) << "\"";

            // toJSONAttributes(out, currentNode, indent, hints);
         }
      }
   }
   void KwlNodeJsonFormatter::toJSONAttributes(std::ostream &out,
                                               const KwlNode *currentNode,
                                               int indent,
                                               const FormatHints &hints) const
   {
      std::string indentStr = hints.m_prettyPrint ? std::string(indent, ' ') : "";
      std::string separator = hints.m_prettyPrint ? "\n" : "";
      ossim_uint32 nChildren = currentNode->getAttributes().size();
      ossim_uint32 childIdx = 0;
      for (auto attribute : currentNode->getAttributes())
      {
         out << indentStr << "\"" << attribute.second->getKey()
             << "\" : \"" << fixValue(attribute.second->getValue()) << "\"";
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << "," << separator;
         }
      }
   }
   void KwlNodeJsonFormatter::toJSONChildren(std::ostream &out,
                                             const KwlNode *currentNode,
                                             int indent,
                                             const FormatHints &hints) const
   {
      std::string indentStr = hints.m_prettyPrint ? std::string(indent, ' ') : "";
      std::string separator = hints.m_prettyPrint ? "\n" : "";
      ossim_uint32 childIdx = 0;
      ossim_uint32 nChildren = currentNode->getChildren().size();
      if (!nChildren)
         return;
      for (auto child : currentNode->getChildren())
      {
         toJSON(out, child.second.get(), indent + hints.m_indent, hints);
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << "," << separator;
         }
      }
   }
   void KwlNodeJsonFormatter::toJSONArrayType(std::ostream &out,
                                              const KwlNode *currentNode,
                                              int indent,
                                              const FormatHints &hints) const
   {
      std::string indentStr = hints.m_prettyPrint ? std::string(indent, ' ') : "";
      std::string indentStr2 = hints.m_prettyPrint ? std::string(indent + hints.m_indent, ' ') : "";
      std::string separator = hints.m_prettyPrint ? "\n" : "";
      ossim_uint32 nChildren = currentNode->getChildren().size();
      ossim_uint32 childIdx = 0;
      out << "[" << separator;
      // sort the child array
      std::map<ossim_int32, std::shared_ptr<KwlNode>> sortedMap;
      for (auto x : currentNode->getChildren())
      {
         sortedMap.insert(std::make_pair(x.first.toInt32(), x.second));
      }
      // iterate through sorted array
      for (auto child : sortedMap)
      {
         if (child.second->hasChildren())
         {
            ossim_uint32 nGrandChildren = child.second->getChildren().size();
            ossim_uint32 grandChildIdx = 0;
            out << indentStr2 << "{" << separator;
            for (auto skippedChild : child.second->getChildren())
            {
               toJSON(out, skippedChild.second.get(), indent + hints.m_indent*2, hints);
               ++grandChildIdx;
               if (grandChildIdx != nGrandChildren)
               {
                  out << "," << separator;
               }
            }
            out << separator << indentStr2 << "}";
         }
         else
         {
            out << indentStr2 << "\"" << fixValue(child.second->getValue()) << "\"";
         }
         ++childIdx;
         if (childIdx != nChildren)
         {
            out << "," << separator;
         }
      }
      out << separator << indentStr << "]";
   }

   ossimString KwlNodeJsonFormatter::fixValue(const ossimString &value) const
   {
      ossimString result(value);

      result = result.substitute("\n", "\\n", true);
      result = result.substitute("\"", "'", true);

      return result;
   }
}
{'fraction_non_alphanumeric': 0.13642949130714746, 'fraction_numerical': 0.005312298776561494, 'mean_word_length': 1.5639702847709451, 'pattern_counts': {'":': 0, '<': 221, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2116578', 'n_tokens_mistral': 3447, 'n_tokens_neox': 3264, 'n_words': 625}