diff options
| -rw-r--r-- | contrib/json-glib-1.0.vapi | 21 | ||||
| -rw-r--r-- | contrib/json-object.vala | 10 | ||||
| -rw-r--r-- | contrib/json-test.vala | 81 |
3 files changed, 85 insertions, 27 deletions
diff --git a/contrib/json-glib-1.0.vapi b/contrib/json-glib-1.0.vapi index 212230e..3b93fa3 100644 --- a/contrib/json-glib-1.0.vapi +++ b/contrib/json-glib-1.0.vapi @@ -95,13 +95,13 @@ namespace Json { [CCode (cheader_filename = "json-glib/json-parser.h")] public class Parser : GLib.Object { + public weak uint current_line { get; } + public weak uint current_pos { get; } public Parser (); public bool load_from_file (string filename) throws GLib.Error; public bool load_from_data (string buffer, ulong length = -1) throws ParserError; public weak Json.Node peek_root (); public Json.Node get_root (); - public uint get_current_line (); - public uint get_current_pos (); public bool has_assingment (out string variable_name); public signal void parse_start (); public signal void parse_end (); @@ -116,9 +116,24 @@ namespace Json { [CCode (cheader_filename = "json-glib/json-generator.h")] public class Generator : GLib.Object { + [NoAccessorMethod] + public weak bool pretty { get; set; } + [NoAccessorMethod] + public weak uint indent_char { get; set; } + public Json.Node root { set; } public Generator (); public string to_data (out ulong length = null); public bool to_file (string! filename) throws GLib.FileError; - public void set_root (Json.Node node); } + + [CCode (cheader_filename = "json-glib/json-gobject.h")] + public interface Serializable : GLib.Object { + public abstract Json.Node serialize_property (string property_name, GLib.Value val, GLib.ParamSpec pspec); + public abstract bool deserialize_property (string property_name, GLib.Value val, GLib.ParamSpec pspec, Json.Node node); + } + + [CCode (cheader_filename = "json-glib/json-gobject.h")] + public static GLib.Object construct_gobject (GLib.Type g_type, string! data, ulong length = -1); + [CCode (cheader_filename = "json-glib/json-gobject.h")] + public static string serialize_gobject (GLib.Object g_object, out ulong length = null); } diff --git a/contrib/json-object.vala b/contrib/json-object.vala index a8058f5..993381d 100644 --- a/contrib/json-object.vala +++ b/contrib/json-object.vala @@ -25,7 +25,8 @@ public class Sample : GLib.Object { root.set_object (obj); var generator = new Json.Generator (); - generator.set_root (root); + generator.pretty = true; + generator.root = root; return generator.to_data (); } @@ -33,7 +34,12 @@ public class Sample : GLib.Object { static int main (string[] args) { var sample = new Sample (); - stdout.printf ("var sample = %s;\n", sample.to_json ()); + stdout.printf ("[manual] var sample = %s;\n", + sample.to_json ()); + + var buf = Json.serialize_gobject (sample); + stdout.printf ("[automatic] var sample = %s;\n", + buf); return 0; } diff --git a/contrib/json-test.vala b/contrib/json-test.vala index 872713f..9091694 100644 --- a/contrib/json-test.vala +++ b/contrib/json-test.vala @@ -12,6 +12,62 @@ using GLib; using Json; public class Sample : GLib.Object { + public void dump_node (Json.Node node) + { + switch (node.type ()) + { + case Json.NodeType.OBJECT: + var obj = node.get_object (); + stdout.printf ("* size: %d\n", obj.get_size ()); + + foreach (weak string member in obj.get_members ()) + { + var val = obj.dup_member (member); + + stdout.printf ("* member[\"%s\"] type: %s, value:\n", + member, + val.type_name ()); + + dump_node (val); + } + break; + + case Json.NodeType.ARRAY: + var array = node.get_array (); + stdout.printf ("* length: %d\n", array.get_length ()); + + var count = 0; + foreach (weak Json.Node element in array.get_elements ()) + { + stdout.printf ("* element[%d] type: %s, value:\n", + count++, + element.type_name ()); + + dump_node (element); + } + break; + + case Json.NodeType.VALUE: + switch (node.get_value_type ()) + { + case typeof (int): + stdout.printf ("*** %d\n", node.get_int ()); + break; + case typeof (double): + stdout.printf ("*** %f\n", node.get_double ()); + break; + case typeof (string): + stdout.printf ("*** %s\n", node.get_string ()); + break; + } + break; + + case Json.NodeType.NULL: + stdout.printf ("*** null\n"); + break; + } + } + public void parse (string buffer) { var parser = new Json.Parser (); @@ -32,7 +88,7 @@ public class Sample : GLib.Object { node.set_object (o); var gen = new Json.Generator (); - gen.set_root (node); + gen.root = node; var len = 0; var dump = gen.to_data (ref len); @@ -52,28 +108,9 @@ public class Sample : GLib.Object { var root = parser.get_root (); stdout.printf ("root node type: %s\n", root.type_name ()); - - switch (root.type ()) { - case Json.NodeType.OBJECT: - break; - case Json.NodeType.ARRAY: - var array = root.get_array (); - stdout.printf ("* length: %d\n", array.get_length ()); - - var count = 0; - foreach (weak Json.Node element in array.get_elements ()) { - stdout.printf ("* element[%d] type: %s\n", - count++, - element.type_name ()); - } - break; - case Json.NodeType.VALUE: - break; - case Json.NodeType.NULL: - stdout.printf ("null node\n"); - break; - } + dump_node (root); + return; } |
