Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/dev/zarr/zarrjava/v2/DataType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.zarr.zarrjava.v2;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum DataType implements dev.zarr.zarrjava.core.DataType {
Expand Down Expand Up @@ -32,6 +33,24 @@ public enum DataType implements dev.zarr.zarrjava.core.DataType {
this.endianness = endianness;
}

@JsonCreator
public static DataType fromValue(String value) {
for (DataType dataType : values()) {
if (dataType.getValue().equals(value)) {
return dataType;
}
}
// byte order is irrelevant for single-byte types, but numpy accepts (and some writers emit) "<u1" or ">u1"
if (value.startsWith(Endianness.LITTLE.getValue()) || value.startsWith(Endianness.BIG.getValue())) {
for (DataType dataType : values()) {
if (dataType.endianness == Endianness.UNSPECIFIED && dataType.dtype.equals(value.substring(1))) {
return dataType;
}
}
}
throw new IllegalArgumentException("Unsupported data type: " + value);
}

public Endianness getEndianness() {
return endianness;
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/dev/zarr/zarrjava/ZarrV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
Expand Down Expand Up @@ -609,6 +611,16 @@ public void testZarrJsonFormat(Function<ArrayMetadataBuilder, ArrayMetadataBuild
}
}

@ParameterizedTest
@CsvSource({"|u1,UINT8", "<u1,UINT8", ">u1,UINT8", ">i1,INT8", "<b1,BOOL", ">u2,UINT16_BE"})
public void testReadDataTypeWithAnyByteOrder(String dtype, DataType expected) throws ZarrException, IOException {
StoreHandle storeHandle = new MemoryStore().resolve("array");
storeHandle.resolve(ZARRAY).set(ByteBuffer.wrap(("{\"zarr_format\": 2, \"shape\": [2], \"chunks\": [2], " +
"\"dtype\": \"" + dtype + "\", \"compressor\": null, \"fill_value\": 0, \"filters\": null, " +
"\"order\": \"C\"}").getBytes(StandardCharsets.UTF_8)));
Assertions.assertEquals(expected, Array.open(storeHandle).metadata().dataType);
}

@Test
public void testMemoryStore() throws ZarrException, IOException {
StoreHandle storeHandle = new MemoryStore().resolve();
Expand Down
Loading