|
@@ -10,14 +10,51 @@ namespace InvercargillJson {
|
|
|
node = Json.from_string (json);
|
|
|
}
|
|
|
|
|
|
+ public JsonElement.from_file(string path) throws GLib.Error {
|
|
|
+ var parser = new Json.Parser();
|
|
|
+ parser.load_from_file(path);
|
|
|
+ node = parser.get_root();
|
|
|
+ }
|
|
|
+
|
|
|
+ public JsonElement.from_stream(InputStream stream) throws GLib.Error {
|
|
|
+ var parser = new Json.Parser();
|
|
|
+ parser.load_from_stream(stream);
|
|
|
+ node = parser.get_root();
|
|
|
+ }
|
|
|
+
|
|
|
+ public async JsonElement.from_stream_async(InputStream stream, GLib.Cancellable cancellable) throws GLib.Error {
|
|
|
+ var parser = new Json.Parser();
|
|
|
+ yield parser.load_from_stream_async (stream, cancellable);
|
|
|
+ node = parser.get_root();
|
|
|
+ }
|
|
|
+
|
|
|
internal JsonElement.from_node(Json.Node node) {
|
|
|
this.node = node;
|
|
|
}
|
|
|
|
|
|
+ public JsonElement.from_properties(Invercargill.Properties properties) throws Invercargill.ElementError {
|
|
|
+ node = new Json.Node (Json.NodeType.OBJECT);
|
|
|
+ var object = new JsonObject();
|
|
|
+ foreach (var item in properties) {
|
|
|
+ object.set(item.key, new JsonElement.from_element(item.value));
|
|
|
+ }
|
|
|
+ node.set_object (object.json_glib_object);
|
|
|
+ }
|
|
|
+
|
|
|
+ public JsonElement.from_elements(Invercargill.Elements elements) throws Invercargill.ElementError {
|
|
|
+ node = new Json.Node (Json.NodeType.ARRAY);
|
|
|
+ var array = new JsonArray();
|
|
|
+ foreach (var item in elements) {
|
|
|
+ array.add(new JsonElement.from_element (item));
|
|
|
+ }
|
|
|
+ node.set_array (array.json_glib_object);
|
|
|
+ }
|
|
|
+
|
|
|
public JsonElement.from_element(Invercargill.Element element) throws Invercargill.ElementError {
|
|
|
// Null node is top priority
|
|
|
if(element.is_null ()) {
|
|
|
node = new Json.Node(Json.NodeType.NULL);
|
|
|
+ node.init_null ();
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -49,24 +86,24 @@ namespace InvercargillJson {
|
|
|
node.set_string (element.as<DateTime>().format_iso8601());
|
|
|
return;
|
|
|
}
|
|
|
+ if(type == typeof(Invercargill.BinaryData)) {
|
|
|
+ node.set_string (element.as<Invercargill.BinaryData>().to_base64());
|
|
|
+ return;
|
|
|
+ }
|
|
|
if(type == typeof(string)) {
|
|
|
node.set_string (element.as<string>());
|
|
|
return;
|
|
|
}
|
|
|
if(type == typeof(bool)) {
|
|
|
- node.set_string (element.as<string>());
|
|
|
+ node.set_boolean (element.as<bool>());
|
|
|
return;
|
|
|
}
|
|
|
if(type == typeof(double)) {
|
|
|
- node.set_string (element.as<string>());
|
|
|
+ node.set_double (element.as<double?>());
|
|
|
return;
|
|
|
}
|
|
|
if(type == typeof(int64)) {
|
|
|
- node.set_string (element.as<string>());
|
|
|
- return;
|
|
|
- }
|
|
|
- if(type == typeof(bool)) {
|
|
|
- node.set_string (element.as<string>());
|
|
|
+ node.set_int (element.as<int64?>());
|
|
|
return;
|
|
|
}
|
|
|
if(type == typeof(uint8)) {
|
|
@@ -125,6 +162,9 @@ namespace InvercargillJson {
|
|
|
}
|
|
|
|
|
|
public bool assignable_to_type (GLib.Type type) {
|
|
|
+ if(node.is_null() || type == typeof(Json.Node)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
if(node.get_node_type() == Json.NodeType.ARRAY) {
|
|
|
return
|
|
|
type == typeof(JsonArray) ||
|
|
@@ -138,9 +178,6 @@ namespace InvercargillJson {
|
|
|
type == typeof(Invercargill.Properties) ||
|
|
|
type == typeof(Json.Object);
|
|
|
}
|
|
|
- if(node.is_null() || type == typeof(Json.Node)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
if(node.get_value_type().is_a(typeof(int64)) || node.get_value_type ().is_a(typeof(double))) {
|
|
|
return
|
|
|
type == typeof(uint8) ||
|
|
@@ -158,6 +195,7 @@ namespace InvercargillJson {
|
|
|
if(node.get_value_type().is_a(typeof(string))) {
|
|
|
return
|
|
|
type == typeof(string) ||
|
|
|
+ type == typeof(Invercargill.BinaryData) ||
|
|
|
(type == typeof(DateTime) && new DateTime.from_iso8601(node.get_string(), null) != null);
|
|
|
}
|
|
|
if(node.get_value_type().is_a(typeof(bool))) {
|
|
@@ -173,15 +211,14 @@ namespace InvercargillJson {
|
|
|
result = null;
|
|
|
return false;
|
|
|
}
|
|
|
- if(node.is_null ()) {
|
|
|
- result = null;
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
if(typeof(T) == typeof(Json.Node)) {
|
|
|
result = node;
|
|
|
return true;
|
|
|
}
|
|
|
+ if(node.is_null ()) {
|
|
|
+ result = null;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
if(typeof(T) == typeof(JsonArray) || typeof(T) == typeof(Invercargill.Elements)) {
|
|
|
result = new JsonArray.from_existing (node.get_array ());
|
|
|
return true;
|
|
@@ -256,6 +293,10 @@ namespace InvercargillJson {
|
|
|
result = new DateTime.from_iso8601(node.get_string (), null);
|
|
|
return true;
|
|
|
}
|
|
|
+ if(typeof(T).is_a(typeof(Invercargill.BinaryData))) {
|
|
|
+ result = new Invercargill.BinaryData.from_base64(node.get_string ());
|
|
|
+ return true;
|
|
|
+ }
|
|
|
if(typeof(T).is_a(typeof(bool))) {
|
|
|
result = node.get_boolean();
|
|
|
return true;
|
|
@@ -274,6 +315,18 @@ namespace InvercargillJson {
|
|
|
return Json.to_string (node, pretty);
|
|
|
}
|
|
|
|
|
|
+ public void write_to_stream(OutputStream stream) throws Error {
|
|
|
+ var gen = new Json.Generator();
|
|
|
+ gen.set_root (node);
|
|
|
+ gen.to_stream (stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void write_to_file(string path) throws Error {
|
|
|
+ var gen = new Json.Generator();
|
|
|
+ gen.set_root (node);
|
|
|
+ gen.to_file (path);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|