altoslib: Switch preserved state format to JSON
[fw/altos] / altoslib / AltosMag.java
index c350ae46392f4ddd637b58fe8f34c0c825a9faf1..ec98882f2e0513629fa3e25395d814b120a9b757 100644 (file)
@@ -20,7 +20,7 @@ package org.altusmetrum.altoslib_11;
 import java.util.concurrent.*;
 import java.io.*;
 
-public class AltosMag implements Cloneable, AltosHashable {
+public class AltosMag implements Cloneable, AltosHashable, AltosJsonable {
        public int              along;
        public int              across;
        public int              through;
@@ -103,6 +103,15 @@ public class AltosMag implements Cloneable, AltosHashable {
                return h;
        }
 
+       public AltosJson json() {
+               AltosJson       j = new AltosJson();
+
+               j.put("along", along);
+               j.put("across", across);
+               j.put("through", through);
+               return j;
+       }
+
        public AltosMag(AltosHashSet h) {
                this();
 
@@ -117,4 +126,19 @@ public class AltosMag implements Cloneable, AltosHashable {
 
                return new AltosMag(h);
        }
+
+       public AltosMag(AltosJson j) {
+               this();
+
+               along = j.get_int("along", along);
+               across = j.get_int("across", across);
+               through = j.get_int("through", through);
+       }
+
+       public static AltosMag fromJson(AltosJson j, AltosMag def) {
+               if (j == null)
+                       return def;
+
+               return new AltosMag(j);
+       }
 }