Commit 75c28e55 authored by Canek Peláez's avatar Canek Peláez
Browse files

Quote strings.

parent f1e4fd32
Loading
Loading
Loading
Loading
+16 −6
Original line number Original line Diff line number Diff line
@@ -10,6 +10,22 @@
          <object class="GtkBox">
          <object class="GtkBox">
            <property name="spacing">6</property>
            <property name="spacing">6</property>
            <property name="orientation">horizontal</property>
            <property name="orientation">horizontal</property>
            <child>
              <object class="GtkLabel">
                <property name="xalign">0.0</property>
                <property name="halign">start</property>
                <binding name="label">
                  <lookup name="node-type" type="GtkJsonApplicationNodeRow">
                    <lookup name="item">expander</lookup>
                  </lookup>
                </binding>
                <binding name="css-classes">
                  <lookup name="css-type" type="GtkJsonApplicationNodeRow">
                    <lookup name="item">expander</lookup>
                  </lookup>
                </binding>
              </object>
            </child>
            <child>
            <child>
              <object class="GtkLabel">
              <object class="GtkLabel">
                <property name="xalign">0.0</property>
                <property name="xalign">0.0</property>
@@ -24,9 +40,6 @@
                    <lookup name="item">expander</lookup>
                    <lookup name="item">expander</lookup>
                  </lookup>
                  </lookup>
                </binding>
                </binding>
                <style>
                  <class name="node-type" />
                </style>
              </object>
              </object>
            </child>
            </child>
            <child>
            <child>
@@ -43,9 +56,6 @@
                    <lookup name="item">expander</lookup>
                    <lookup name="item">expander</lookup>
                  </lookup>
                  </lookup>
                </binding>
                </binding>
                <style>
                  <class name="node-type" />
                </style>
              </object>
              </object>
            </child>
            </child>
          </object>
          </object>
+6 −9
Original line number Original line Diff line number Diff line
@@ -10,25 +10,22 @@
    margin: 10px;
    margin: 10px;
}
}


.array-key {
.array-type {
    font-weight: bold;
    font-weight: bold;
    color: #ff0;
    color: #ff0;
}
}


.array-value {
.array-key, .object-key, .value-key {
    color: #ffa;
    font-size: small;
    color: #c00;
}
}


.object-key {
.object-type {
    font-weight: bold;
    font-weight: bold;
    color: #f0f;
    color: #f0f;
}
}


.object-value {
.value-type {
    color: #faf;
}

.value-key {
    font-weight: bold;
    font-weight: bold;
    color: #0ff;
    color: #0ff;
}
}
+1 −1
Original line number Original line Diff line number Diff line
@@ -77,7 +77,7 @@ namespace GtkJson.Application {
            var model = new GLib.ListStore(typeof(NodeRow));
            var model = new GLib.ListStore(typeof(NodeRow));
            for (uint i = 0; i < array.get_length(); i++) {
            for (uint i = 0; i < array.get_length(); i++) {
                var n = array.get_element(i);
                var n = array.get_element(i);
                var row = new NodeRow(n);
                var row = new NodeRow(n, @"$(i)");
                model.append(row);
                model.append(row);
            }
            }
            return model;
            return model;
+9 −1
Original line number Original line Diff line number Diff line
@@ -77,6 +77,11 @@ namespace GtkJson.Application {
        private void open_handler() {
        private void open_handler() {
            var dialog = new Gtk.FileDialog();
            var dialog = new Gtk.FileDialog();
            dialog.accept_label = "Open JSON file";
            dialog.accept_label = "Open JSON file";
            var filters = new GLib.ListStore(typeof(Gtk.FileFilter));
            var filter = new Gtk.FileFilter();
            filter.add_mime_type("application/json");
            filters.append(filter);
            dialog.filters = filters;
            dialog.open.begin(application_window, null, open_result);
            dialog.open.begin(application_window, null, open_result);
        }
        }


@@ -89,8 +94,10 @@ namespace GtkJson.Application {
                var file = dialog.open.end(result);
                var file = dialog.open.end(result);
                if (file != null)
                if (file != null)
                    load(file);
                    load(file);
            } catch (Gtk.DialogError error) {
                return;
            } catch (GLib.Error error) {
            } catch (GLib.Error error) {
                GLib.error("Error: $(error.message)");
                GLib.error(@"Error: $(error.message)");
            }
            }
        }
        }


@@ -102,6 +109,7 @@ namespace GtkJson.Application {
                application_window.add_root(node);
                application_window.add_root(node);
                application_window.set_subtitle(file.get_basename());
                application_window.set_subtitle(file.get_basename());
            } catch (GLib.Error error) {
            } catch (GLib.Error error) {
                stderr.printf("Identification: %" + uint32.FORMAT + "\n", error.domain);
                GLib.error("Error: $(error.message)");
                GLib.error("Error: $(error.message)");
            }
            }
        }
        }
+42 −13
Original line number Original line Diff line number Diff line
@@ -24,33 +24,37 @@ namespace GtkJson.Application {


    public class NodeRow : GLib.Object {
    public class NodeRow : GLib.Object {


        public Json.Node node { get; private set; }

        public string node_type { get; private set; }
        public string node_key { get; private set; }
        public string node_key { get; private set; }
        public string node_value { get; private set; }
        public string node_value { get; private set; }
        public Json.Node node { get; private set; }

        public string[] css_type { get; private set; }
        public string[] css_key { get; private set; }
        public string[] css_key { get; private set; }
        public string[] css_value { get; private set; }
        public string[] css_value { get; private set; }


        public NodeRow(Json.Node node, string name="") {
        public NodeRow(Json.Node node, string key="") {
            node_value = name;
            node_key = (quote_key(key)) ? @"“$(key)”" : key;
            switch (node.get_node_type()) {
            switch (node.get_node_type()) {
            case Json.NodeType.ARRAY:
            case Json.NodeType.ARRAY:
                node_key = "Array";
                node_type = "Array";
                css_type = new string[] { "array-type" };
                css_key = new string[] { "array-key" };
                css_key = new string[] { "array-key" };
                css_value = new string[] { "array-value" };
                break;
                break;
            case Json.NodeType.NULL:
            case Json.NodeType.NULL:
                node_key = "null";
                node_type = "null";
                css_key = new string[] { "null-key" };
                css_type = new string[] { "null-type" };
                break;
                break;
            case Json.NodeType.OBJECT:
            case Json.NodeType.OBJECT:
                node_key = "Object";
                node_type = "Object";
                css_type = new string[] { "object-type" };
                css_key = new string[] { "object-key" };
                css_key = new string[] { "object-key" };
                css_value = new string[] { "object-value" };
                break;
                break;
            case Json.NodeType.VALUE:
            case Json.NodeType.VALUE:
                node_key = "Value";
                node_type = "Value";
                node_value = name == "" ? get_value_node_text(node) :
                node_value = get_value_node_text(node);
                    @"$(name): " + get_value_node_text(node);
                css_type = new string[] { "value-type" };
                css_key = new string[] { "value-key" };
                css_key = new string[] { "value-key" };
                css_value = new string[] { "value-value" };
                css_value = new string[] { "value-value" };
                break;
                break;
@@ -58,10 +62,35 @@ namespace GtkJson.Application {
            this.node = node;
            this.node = node;
        }
        }


        private bool quote_key(string key) {
            if (key == "-" || key == ".")
                return true;
            int d = 0;
            for (int i = 0; i < key.length; i++) {
                char c = key.get(i);
                if (c == '-') {
                    if (i > 0)
                        return true;
                    else
                        continue;
                }
                if (c == '.') {
                    if (d++ > 0)
                        return true;
                    else
                        continue;
                }
                if (c.isdigit())
                    continue;
                return true;
            }
            return false;
        }

        private string get_value_node_text(Json.Node node) {
        private string get_value_node_text(Json.Node node) {
            var val = node.get_value();
            var val = node.get_value();
            if (val.holds(typeof(string)))
            if (val.holds(typeof(string)))
                return val.get_string();
                return "“" + val.get_string() + "”";
            if (val.holds(typeof(float)))
            if (val.holds(typeof(float)))
                return "%g".printf(val.get_float());
                return "%g".printf(val.get_float());
            if (val.holds(typeof(double)))
            if (val.holds(typeof(double)))