[libhttpseverywhere/fulljson: 2/5] ruleset: implemened json deserialization



commit ec814e0934aef992fabe9702484968e6d6ad92c3
Author: grindhold <grindhold gmx net>
Date:   Fri Sep 22 22:18:02 2017 +0200

    ruleset: implemened json deserialization

 meson.build      |    2 +-
 src/context.vala |    4 ++-
 src/ruleset.vala |   92 +++++++++++++++++++++++-------------------------------
 3 files changed, 43 insertions(+), 55 deletions(-)
---
diff --git a/meson.build b/meson.build
index 80bc60b..e1ab1e0 100644
--- a/meson.build
+++ b/meson.build
@@ -61,7 +61,7 @@ httpseverywhere_lib = shared_library('httpseverywhere-'+api, httpseverywhere_lib
                                      version: libhttpseverywhere_version)
 
 httpseverywhere_test = executable('httpseverywhere_test', httpseverywhere_test_source,
-                                  dependencies: [glib, gobject, gio, gee, libxml],
+                                  dependencies: [glib, gobject, gio, gee, libxml, json_glib],
                                   link_with: httpseverywhere_lib)
 
 g_ir_compiler = find_program('g-ir-compiler')
diff --git a/src/context.vala b/src/context.vala
index 1042032..cdf05ae 100644
--- a/src/context.vala
+++ b/src/context.vala
@@ -321,7 +321,9 @@ namespace HTTPSEverywhere {
             Xml.Node* root = doc->get_root_element();
             if (root != null) {
                 try {
-                    var rs = new Ruleset.from_xml(root);
+                    // TODO: use Ruleset.from_json
+                    //var rs = new Ruleset.from_xml(root);
+                    var rs = new Ruleset();
                     rulesets.set(id, rs);
                 } catch (RulesetError e) {
                 }
diff --git a/src/ruleset.vala b/src/ruleset.vala
index e664b50..fe7801e 100644
--- a/src/ruleset.vala
+++ b/src/ruleset.vala
@@ -45,7 +45,8 @@ namespace HTTPSEverywhere {
         private Gee.ArrayList<Rule> rules;
         private Gee.ArrayList<Regex> exclusions;
         private Gee.ArrayList<Target> _targets;
-        private string securecookie;
+        // TODO: implement
+        //private string securecookie;
 
         /**
          * The target-hosts this ruleset applies to
@@ -68,63 +69,48 @@ namespace HTTPSEverywhere {
         /**
          * Creates a Ruleset from a ruleset file
          */
-        public Ruleset.from_xml(Xml.Node* root) throws RulesetError {
+        public Ruleset.from_json(Json.Node root) throws RulesetError {
             this();
-            if (root->name != "ruleset")
-                throw new RulesetError.PARSE_ERROR("Name of rootnode must be 'ruleset'");
+            var obj = root.get_object();
 
             // Set the Rulesets attributes
-            string? n = root->get_prop("name");
-            string? m;
-            if (n != null)
-                this.name = n;
-            n = root->get_prop("default_off");
-            this.default_off = n != null;
-            n = root->get_prop("platform");
-            if (n != null)
-                this.platform = n;
+            this.name = obj.has_member("name") ? obj.get_string_member("name") : null;
+            this.default_off = obj.has_member("default_off");
+            this.platform = obj.has_member("platform") ? obj.get_string_member("platform") : null;
 
-            for (Xml.Node* cn = root->children; cn != null; cn = cn->next) {
-                if (cn->type != Xml.ElementType.ELEMENT_NODE)
-                    continue;
-                switch (cn->name) {
-                    case "rule":
-                        n = cn->get_prop("from");
-                        m = cn->get_prop("to");
-                        if (n == null || m == null)
-                            warning("Skipped malformed rule");
-                        else
-                            this.add_rule(n,m);
-                        break;
-                    case "target":
-                        n = cn->get_prop("host");
-                        if (n != null)
-                            this.add_target(n);
-                        else
-                            warning("Skipped malformed target");
-                        break;
-                    case "exclusion":
-                        n = cn->get_prop("pattern");
-                        if (n != null)
-                            this.add_exclusion(n);
-                        else
-                            warning("Skipped malformed exclusion");
-                        break;
-                    case "securecookie":
-                        n = cn->get_prop("host");
-                        if (n != null)
-                            this.securecookie =  n;
-                        else
-                            warning("Skipped malformed securecookie");
-                        break;
-                    case "test":
-                        // TODO: implement
-                        break;
-                    default:
-                        warning("Unknown node found: %s".printf(cn->name));
-                        break;
-                }
+            if (obj.has_member("rule")) {
+                var rules = obj.get_array_member("rule");
+                rules.foreach_element((_,i,e)=>{
+                    string? from = null;
+                    string? to = null;
+                    var rule = e.get_object();
+                    from = rule.get_string_member("from");
+                    to = rule.get_string_member("to");
+                    if (from == null || to == null)
+                        warning("Skipped malformed rule");
+                    else
+                        this.add_rule(from, to);
+                });
+            }
+
+            if (obj.has_member("target")) {
+                var targets = obj.get_array_member("target");
+                targets.foreach_element((_, i, e) => {
+                    if (e.get_node_type() == Json.NodeType.VALUE) {
+                        this.add_target(e.get_string());
+                    }
+                });
+            }
+
+            if (obj.has_member("exclusion")) {
+                var exclusions = obj.get_array_member("exclusion");
+                exclusions.foreach_element((_, i, e) => {
+                    if (e.get_node_type() == Json.NodeType.VALUE) {
+                        this.add_exclusion(e.get_string());
+                    }
+                });
             }
+            // TODO: implement securecookies
         }
 
         /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]