Skip to content

Commit ad92de5

Browse files
committed
fix: more tolerant Axis parsing
1 parent e76da82 commit ad92de5

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

  • src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/axes

src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/axes/AxisAdapter.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,35 @@ public Axis deserialize(final JsonElement json, final Type typeOfT, final JsonDe
2121
return null;
2222

2323
final JsonObject jobj = json.getAsJsonObject();
24-
final String type = jobj.has("type") ? jobj.get("type").getAsString() : "";
25-
final String name = jobj.has("name") ? jobj.get("name").getAsString() : "";
26-
final String unit = jobj.has("unit") ? jobj.get("unit").getAsString() : "";
24+
25+
// Require name to be non-null, but try to be tolerant of null type or unit
26+
if (!jobj.has("name"))
27+
return null;
28+
29+
final String name = jobj.get("name").getAsString();
30+
final String type = jobj.has("type") ? nullSafeString(jobj.get("type")) : "";
31+
final String unit = jobj.has("unit") ? nullSafeString(jobj.get("unit")) : "";
2732

2833
return new Axis(type, name, unit);
2934
}
35+
36+
37+
/**
38+
* Returns a String from the json element or null if it is JsonNull.
39+
* <p>
40+
* Gson throws an UnsupportedOperationException when calling getAsString
41+
* on JsonNull, this method returns null instead.
42+
*
43+
* @param json a json element
44+
* @return a string, or null
45+
*/
46+
private String nullSafeString(JsonElement json) {
47+
48+
if( json.isJsonNull())
49+
return null;
50+
else
51+
return json.getAsString();
52+
}
3053

3154
@Override
3255
public JsonElement serialize(final Axis src, final Type typeOfSrc, final JsonSerializationContext context) {

0 commit comments

Comments
 (0)