Skip to content

Commit df49f87

Browse files
committed
doc: n5 serialization annotations
1 parent c8cac8c commit df49f87

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/serialization/JsonArrayUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
public class JsonArrayUtils {
77

8+
/**
9+
* Reverses the order of elements in a JSON array in-place.
10+
*
11+
* @param array the JSON array to reverse; must not be null
12+
* @see N5Annotations.ReverseArray
13+
*/
814
public static void reverse(final JsonArray array) {
915

1016
JsonElement a;

src/main/java/org/janelia/saalfeldlab/n5/serialization/N5Annotations.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,34 @@
77
import java.lang.annotation.RetentionPolicy;
88
import java.lang.annotation.Target;
99

10+
/**
11+
* Provides specialized annotations for N5 serialization behaviors.
12+
* <p>
13+
* This interface defines annotations that control specific serialization
14+
* transformations needed for N5 compatibility across different storage
15+
* formats, for example, when dealing with dimension ordering conventions.
16+
*
17+
* @see ReverseArray
18+
*/
1019
public interface N5Annotations extends Serializable {
1120

21+
/**
22+
* Indicates that an array field should be reversed during serialization/deserialization.
23+
* <p>
24+
* This annotation is used to handle dimension ordering differences between storage formats.
25+
* For example, Zarr uses C-order (row-major) dimension ordering [Z, Y, X], while N5 uses
26+
* F-order (column-major) dimension ordering [X, Y, Z].
27+
* <p>
28+
* Example usage:
29+
* <pre>{@code
30+
* @ReverseArray
31+
* @Parameter("chunk_shape")
32+
* private final int[] shape; // Will be reversed when serializing
33+
* </pre>
34+
* <p>
35+
* This ensures that dimension-related arrays maintain the correct semantic meaning
36+
* across different storage format conventions.
37+
*/
1238
@Inherited
1339
@Retention(RetentionPolicy.RUNTIME)
1440
@Target(ElementType.FIELD)

src/main/java/org/janelia/saalfeldlab/n5/serialization/NameConfig.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,68 @@
99
import java.lang.annotation.RetentionPolicy;
1010
import java.lang.annotation.Target;
1111

12+
/**
13+
* Configuration interface for N5 serialization naming and parameter annotations.
14+
* <p>
15+
* This interface provides a standardized way to configure serialization names and parameters
16+
* for N5 components such as compression algorithms and codecs. It defines annotations that
17+
* control how classes and their fields are serialized and deserialized for the N5 API.
18+
* <p>
19+
* Classes implementing this interface can use the provided annotations to:
20+
* <ul>
21+
* <li>Define a serialization type name with {@link Name @Name}</li>
22+
* <li>Specify a namespace prefix with {@link Prefix @Prefix}</li>
23+
* <li>Mark fields as serialization parameters with {@link Parameter @Parameter}</li>
24+
* </ul>
25+
*
26+
* @see Name
27+
* @see Prefix
28+
* @see Parameter
29+
*/
1230
public interface NameConfig extends Serializable {
1331

32+
/**
33+
* Defines a namespace prefix for serialization.
34+
* <p>
35+
* This annotation specifies a prefix that is prepended to the serialization
36+
* type name, creating a namespaced identifier. This is useful for organizing
37+
* related components into logical groups, usually all the components implementing
38+
* a particular interface.
39+
* <p>
40+
* Example usage:
41+
* <pre>{@code
42+
* @Prefix("codec")
43+
* public interface Codec extends NameConfig {
44+
* // ...
45+
* }
46+
* }</pre>
47+
*
48+
* @see Name
49+
*/
1450
@Retention(RetentionPolicy.RUNTIME)
1551
@Inherited
1652
@Target(ElementType.TYPE)
1753
@interface Prefix {
1854
String value();
1955
}
2056

57+
/**
58+
* Specifies the serialization type name for a class.
59+
* <p>
60+
* This annotation defines the string identifier used during serialization and
61+
* deserialization to identify the type. The name should be unique within its
62+
* namespace and is typically a short, descriptive identifier.
63+
* <p>
64+
* Example usage:
65+
* <pre>{@code
66+
* @Name("gzip")
67+
* public class GzipCompression implements Compression {
68+
* // ...
69+
* }
70+
* }</pre>
71+
*
72+
* @see Prefix
73+
*/
2174
@Retention(RetentionPolicy.RUNTIME)
2275
@Inherited
2376
@Target(ElementType.TYPE)
@@ -26,14 +79,57 @@ public interface NameConfig extends Serializable {
2679
String value();
2780
}
2881

82+
/**
83+
* Marks a field as a parameter to be serialized.
84+
* <p>
85+
* This annotation identifies fields that should be included during serialization
86+
* and deserialization. It supports both required and optional parameters.
87+
* <p>
88+
* The {@code value} attribute can be used to specify an alternative name for
89+
* the parameter during serialization. If not specified, the field name is used.
90+
* <p>
91+
* Example usage:
92+
* <pre>{@code
93+
* public class GzipCompression implements Compression {
94+
* @Parameter
95+
* private final int level;
96+
*
97+
* @Parameter(optional = true)
98+
* private final boolean useZlib;
99+
* }
100+
* }</pre>
101+
*
102+
* @see Name
103+
*/
29104
@Retention(RetentionPolicy.RUNTIME)
30105
@Inherited
31106
@Target(ElementType.FIELD)
32107
@interface Parameter {
108+
/**
109+
* Alternative name for the parameter during serialization.
110+
* If empty, the field name is used.
111+
*
112+
* @return the parameter name, or empty string for field name
113+
*/
33114
String value() default "";
115+
116+
/**
117+
* Whether this parameter is optional.
118+
* Optional parameters may be omitted during deserialization.
119+
*
120+
* @return {@code true} if the parameter is optional, {@code false} otherwise
121+
*/
34122
boolean optional() default false;
35123
}
36124

125+
/**
126+
* Returns the serialization type name for this instance.
127+
* <p>
128+
* This method retrieves the value from the {@link Name @Name} annotation
129+
* if present on the class.
130+
*
131+
* @return the type name from the {@code @Name} annotation, or {@code null} if not annotated
132+
*/
37133
default String getType() {
38134

39135
final Name type = getClass().getAnnotation(Name.class);

0 commit comments

Comments
 (0)