Advertisement
Guest User

Untitled

a guest
Aug 13th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.25 KB | None | 0 0
  1. using Gee;
  2. class Sas : GLib.Object {
  3.     public int a {set;get;}
  4.     //  public ArrayList<int> arr {set;get;}
  5.     public Sas(int a) {
  6.         this.a = a;
  7.     }
  8. }
  9.  
  10. string serialize_ArrayList(Value val){
  11.     if (val.type ().is_a (typeof (Gee.ArrayList))) {
  12.         unowned Gee.ArrayList<GLib.Object> arraylist_value = val as Gee.ArrayList<GLib.Object>;
  13.  
  14.         if (arraylist_value != null) {
  15.             var array = new Json.Array.sized (arraylist_value.size);
  16.  
  17.             foreach (var item in arraylist_value) {
  18.                 //message(@"$(Json.gobject_serialize (item))");
  19.                 array.add_element (Json.gobject_serialize (item));
  20.             }
  21.  
  22.             var node = new Json.Node (Json.NodeType.ARRAY);
  23.             node.set_array (array);
  24.             return node.get_string();
  25.         }
  26.         else return "Error";  
  27.     }
  28.     else return "Error";
  29. }
  30.  
  31. public static int main(string[] args) {
  32.     var arrlist = new ArrayList<Sas>.wrap({new Sas(1),new Sas(2),new Sas(4)});
  33.     foreach (var item in arrlist) {
  34.         message(@"$(item.a)\n");
  35.     }
  36.     Value arristVal = Value(typeof(ArrayList<Sas>));
  37.     arristVal = arrlist;
  38.     string strResult = serialize_ArrayList(arristVal);
  39.     message(strResult);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement