]> granicus.if.org Git - pdns/commitdiff
Use json11 zone2json
authorAki Tuomi <cmouse@cmouse.fi>
Wed, 17 Jun 2015 18:08:45 +0000 (21:08 +0300)
committermind04 <mind04@monshouwer.org>
Tue, 30 Jun 2015 06:12:49 +0000 (08:12 +0200)
pdns/Makefile.am
pdns/zone2json.cc

index c9f2ce8b610fff1d96de1dc21d667602b3ba4c59..49acfc01414977f70f5a25d5a1f73ed340d012be 100644 (file)
@@ -1,4 +1,5 @@
 AM_CPPFLAGS += \
+       -I$(top_srcdir)/ext/json11 \
        -I$(top_srcdir)/ext/rapidjson/include \
        $(YAHTTP_CFLAGS) \
        $(POLARSSL_CFLAGS)
@@ -401,7 +402,7 @@ zone2json_SOURCES = \
        zone2json.cc \
        zoneparser-tng.cc
 
-zone2json_LDADD = $(POLARSSL_LIBS)
+zone2json_LDADD = $(POLARSSL_LIBS) -L$(top_srcdir)/ext/json11 -ljson11
 
 # pkglib_LTLIBRARIES = iputils.la
 # iputils_la_SOURCES = lua-iputils.cc
index 17cad46f477f9ef8557ee5a864d8708f0f3132b1..09bed5a6eddd3b475fc5d8916c059a46c32981f7 100644 (file)
 #include <sys/stat.h>
 #include <unistd.h>
 #include <boost/foreach.hpp>
+#include "json11.hpp"
 
+using namespace json11;
 
 StatBag S;
 static int g_numRecords;
 
-static void quoteValue(string &value) 
-{
-  string tmp;
-  size_t opos,pos;
-
-  // no point doing it if there isn't anything to do
-  if (value.find_first_of("\\\\\"") == string::npos) return;
-
-  pos = opos = 0;
-  while((pos = value.find_first_of("\\\\\"", opos)) != string::npos) 
-  {
-     tmp += value.substr(opos, pos - opos);
-     tmp += "\\";
-     tmp += value[pos];
-     opos = pos+1;
-  }
-
-  value = tmp;
-}
-
-
-static string emitRecord(const string& zoneName, const DNSName &DNSqname, const string &qtype, const string &ocontent, int ttl)
+static Json::object emitRecord(const string& zoneName, const DNSName &DNSqname, const string &qtype, const string &ocontent, int ttl)
 {
   int prio=0;
   string retval;
@@ -86,35 +67,15 @@ static string emitRecord(const string& zoneName, const DNSName &DNSqname, const
     trim_left(content);
   }
 
-  quoteValue(content);
-  retval = "{";
-  retval += "\"name\":\"";
-  retval += DNSqname.toString();
-  retval += "\",";
-  retval += "\"type\":\"";
-  retval += qtype;
-  retval += "\",";
-  retval += "\"ttl\":";
-  retval += lexical_cast<string>(ttl);
-  retval += ",";
-  retval += "\"prio\":";
-  retval += lexical_cast<string>(prio);
-  retval += ",";
-  retval += "\"content\":\"";
-  retval += content;
-  retval += "\"}";
+  Json::object dict;
  
-  return retval;
-}
+  dict["name"] = DNSqname.toStringNoDot();
+  dict["type"] = qtype;
+  dict["ttl"] = ttl;
+  dict["prio"] = prio;
+  dict["content"] = content;
 
-static void emitJson(vector<string> &data)
-{
-   size_t l = data.size();
-   cout << "[";
-   for(size_t i=0;i<l-1;i++) 
-      cout << data[i] << ",";
-   cout << data[l-1] << "]";
+  return dict;
 }
 
 /* 2 modes of operation, either --named or --zone (the latter needs $ORIGIN) 
@@ -193,8 +154,8 @@ try
 
       int numdomains=domains.size();
       int tick=numdomains/100;
-      cout <<"[";
-   
+      cout << "[";
+
       for(vector<BindDomainInfo>::const_iterator i=domains.begin();
           i!=domains.end();
           ++i)
@@ -205,15 +166,20 @@ try
           }
           lines.clear(); 
           try {
+            Json::object obj;
+            Json::array recs;
             ZoneParserTNG zpt(i->filename, i->name, BP.getDirectory());
             DNSResourceRecord rr;
+            obj["name"] = i->name.toStringNoDot();
+
             while(zpt.get(rr)) 
-              lines.push_back(emitRecord(i->name.toStringNoDot(), rr.qname.toStringNoDot(), rr.qtype.getName(), rr.content, rr.ttl));
-            cout << "{\"name\":\"" << i->name.toStringNoDot() << "\",\"records\": ";
-            emitJson(lines);
-            cout << "},";
+              recs.push_back(emitRecord(i->name.toStringNoDot(), rr.qname.toStringNoDot(), rr.qtype.getName(), rr.content, rr.ttl));
+            obj["records"] = recs;
+            Json tmp = obj;
+            cout<<tmp.dump();
+            if(i+1 < domains.end()) cout<<",";
             num_domainsdone++;
-          } 
+          }
           catch(std::exception &ae) {
             if(!::arg().mustDo("on-error-resume-next"))
               throw;
@@ -229,18 +195,26 @@ try
           if(!tick || !((count++)%tick))
             cerr<<"\r"<<count*100/numdomains<<"% done ("<<i->filename<<")\033\133\113";
         }
-      cout << "]\n";
+      cout << "]" << endl;
       cerr<<"\r100% done\033\133\113"<<endl;
     }
     else {
       ZoneParserTNG zpt(zonefile, ::arg()["zone-name"]);
       DNSResourceRecord rr;
-      string zname; 
-      cout << "{\"name\":\"" << ::arg()["zone-name"] << "\",\"records\":";
+      string zname;
+      Json::object obj;
+      Json::array records;
+
+      obj["name"] = ::arg()["zone-name"];
+
       while(zpt.get(rr)) 
-        lines.push_back(emitRecord(::arg()["zone-name"], rr.qname.toStringNoDot(), rr.qtype.getName(), rr.content, rr.ttl));
-      emitJson(lines);
-      cout << "}\n";
+        records.push_back(emitRecord(::arg()["zone-name"], rr.qname.toStringNoDot(), rr.qtype.getName(), rr.content, rr.ttl));
+      obj["records"] = records;
+
+      Json tmp = obj;
+
+      cout<<tmp.dump()<<endl;
+
       num_domainsdone=1;
     }
     cerr<<num_domainsdone<<" domains were fully parsed, containing "<<g_numRecords<<" records\n";