diff --git a/README.md b/README.md
index f01dfe7..c4e5414 100644
--- a/README.md
+++ b/README.md
@@ -39,27 +39,35 @@ Log
Log entries, latest entry first, format: YYMMDD.
+## 170816
+
+Frans: Release 1.5. The BinsonArray.getXxx() method now throws
+FormatException for unexpected element types.
+
## 170507
-Release 1.4. Minor changes from 1.3.
+Frans: Release 1.4. Minor changes from previous release.
+Note, this release was not added to the Github releases.
## 160128
-Bug in field sort order fixed thanks to a bug report by Alexander Reshniuk.
+Frans: Bug in field sort order fixed thanks to a bug report
+by Alexander Reshniuk.
## 151004
-Release 1.1.
+Frans: Release 1.1.
## 150925
-Release 1.0.
+Frans: Release 1.0.
## 150912
-Added Binson Schema validation (might be called BINSON-SCHEMA-1) in 40 lines of code.
+Frans: Added Binson Schema validation (might be called BINSON-SCHEMA-1)
+in 40 lines of code.
## 140911
-First upload to github.com.
+Frans: First upload to github.com. Before that I stored it locally.
diff --git a/build.xml b/build.xml
index 9b76579..dd6fb75 100644
--- a/build.xml
+++ b/build.xml
@@ -7,15 +7,15 @@ Instructions:
* Modify stuff marked with "CHANGE" in this file.
* Set JAVA_HOME environment variable.
-* Use (for example) "1.4-dev" while developing version 1.4, remove "1.4"
- for final release.
+* Use (for example) "1.4-dev" while developing version 1.4,
+ remove "1.4" for final release.
-->
The getX() methods gets a value of the type X. If the value does not exist, a FormatException
- * is thrown. To check whether a field of a particular type exist, use the hasX() methods.
+ * The getX() methods gets a value of the type X. If the expected value does not exist,
+ * a FormatException is thrown. To check whether a field of a particular type exists,
+ * use the hasX() methods. Binson obj = new Binson().put("name", "Frans").put("height", 178);
*
*
- * obj.getString("name"); // returns "Frans"
* obj.getInteger("height"); // returns 178
@@ -49,7 +50,7 @@
* JSON (http://json.org/) objects. See the methods
fromJson()
* and toJson()
.
The table below shows how Binson types are stored as Java object internally.
+ *The table below shows how Binson types are stored as Java objects internally.
* ** Binson type Stored as diff --git a/src/org/binson/BinsonArray.java b/src/org/binson/BinsonArray.java index 3d2c3d3..9a39446 100644 --- a/src/org/binson/BinsonArray.java +++ b/src/org/binson/BinsonArray.java @@ -3,8 +3,11 @@ import java.util.ArrayList; /** - * A Binson array, an ArrayList of heterogenous values. - * See Binson class for how Binson types are mapped to Java classes. + *A Binson array is a list of heterogeneous values.
+ * + *The getX() methods gets a value of the type X. If the expected value does not exist, + * a FormatException is thrown. To check whether a field of a particular type exists, + * use the hasX() methods.
* * @author Frans Lundberg */ @@ -47,7 +50,10 @@ public boolean isBoolean(int index) { public boolean getBoolean(int index) { Object obj = get(index); - return obj instanceof Boolean ? ((Boolean) obj) : false; + if (!(obj instanceof Boolean)) { + throw new FormatException("No boolean in Binson array at index " + index + "."); + } + return ((Boolean) obj).booleanValue(); } // long @@ -62,9 +68,12 @@ public boolean isInteger(int index) { return obj instanceof Long; } - public long getInteger(int index) { + public long getInteger(int index) { Object obj = get(index); - return obj instanceof Long ? ((Long) obj).longValue() : 0; + if (!(obj instanceof Long)) { + throw new FormatException("No integer in Binson array at index " + index + "."); + } + return ((Long) obj).longValue(); } // double @@ -81,7 +90,10 @@ public boolean isDouble(int index) { public double getDouble(int index) { Object obj = get(index); - return obj instanceof Double ? ((Double) obj).doubleValue() : 0.0; + if (!(obj instanceof Double)) { + throw new FormatException("No Double in Binson array at index " + index + "."); + } + return ((Double) obj).doubleValue(); } // string @@ -101,7 +113,10 @@ public boolean isString(int index) { public String getString(int index) { Object obj = get(index); - return obj instanceof String ? (String) obj : null; + if (!(obj instanceof String)) { + throw new FormatException("No String in Binson array at index " + index + "."); + } + return (String) obj; } // bytes @@ -121,7 +136,10 @@ public boolean isBytes(int index) { public byte[] getBytes(int index) { Object obj = get(index); - return obj instanceof byte[] ? (byte[]) obj : null; + if (!(obj instanceof byte[])) { + throw new FormatException("No bytes element in Binson array at index " + index + "."); + } + return (byte[]) obj; } // array @@ -141,7 +159,10 @@ public boolean isArray(int index) { public BinsonArray getArray(int index) { Object obj = get(index); - return obj instanceof BinsonArray ? (BinsonArray) obj : null; + if (!(obj instanceof BinsonArray)) { + throw new FormatException("No BinsonArray in Binson array at index " + index + "."); + } + return (BinsonArray) obj; } // object @@ -161,6 +182,9 @@ public boolean isObject(int index) { public Binson getObject(int index) { Object obj = get(index); - return obj instanceof Binson ? (Binson) obj : null; + if (!(obj instanceof Binson)) { + throw new FormatException("No Binson object in Binson array at index " + index + "."); + } + return (Binson) obj; } }