Value Codecs
For a type you own, prefer @NodeValue with @ValueToRaw and @RawToValue methods. Use a ValueCodec<V, R> when the type comes from an external library or you want to keep its definition unchanged. See Modeling (OBNT) for the annotation-based approach.
A codec converts a Java value V to and from a JSON-compatible raw value R. In OBNT, it remains a logical value node, even when its raw representation is a Map or List. SJF4J includes codecs for common types such as UUID, URI, Instant, and LocalDate.
ValueCodec Interface
ValueCodec<V, R> defines how a Java value V maps to a raw representation R. Its core contract is:
public interface ValueCodec<V, R> {
Class<V> valueClazz();
Class<R> rawClazz();
R valueToRaw(V value);
V rawToValue(R raw);
default V valueCopy(V value) { return value; }
}valueClazz() identifies the domain type; rawClazz() identifies its JSON-compatible shape (String, Number, Boolean, Map, or List). Both conversion directions accept and return non-null values only. SJF4J handles null without invoking the codec. The same contract applies to valueCopy() and annotated value conversions. Violations have unspecified behavior. Override valueCopy() for mutable values. For simple conversions, ValueCodec.SimpleValueCodec accepts encoder and decoder functions, as shown below.
Declare a Codec
Declare ValueCodec<V, R> fields on an ordinary interface. Both generic arguments must be concrete classes.
final class OrderId {
public final String value;
public OrderId(String value) {
this.value = value;
}
}
interface AppCodecs {
ValueCodec<OrderId, String> ORDER_ID =
new ValueCodec.SimpleValueCodec<>(
OrderId.class, String.class,
id -> id.value, OrderId::new);
}Runtime Registration
Register each codec instance during application startup, before SJF4J analyzes the affected types:
ValueRegistry.register(AppCodecs.ORDER_ID, "", false);
Sjf4j sjf4j = new Sjf4j();
OrderId id = sjf4j.fromJson("\"A-42\"", OrderId.class);
String json = sjf4j.toJsonString(id); // "A-42"Runtime registration is global.
Named Formats
For Compiled APIs, use @NodeValue on a codec field to declare a named representation:
interface AppCodecs {
ValueCodec<OrderId, String> ORDER_ID =
new ValueCodec.SimpleValueCodec<>(
OrderId.class, String.class,
id -> id.value, OrderId::new);
@NodeValue(format = "prefixed")
ValueCodec<OrderId, String> PREFIXED =
new ValueCodec.SimpleValueCodec<>(
OrderId.class, String.class,
id -> "ord:" + id.value,
raw -> new OrderId(raw.substring(4)));
}For Runtime APIs, register the named format explicitly before use:
ValueRegistry.register(AppCodecs.PREFIXED, "prefixed", false);Select a format on a property or configure a Runtime instance:
class Purchase {
@NodeProperty(valueFormat = "prefixed")
public OrderId orderId;
}
Sjf4j sjf4j = Sjf4j.builder()
.valueFormat(OrderId.class, "prefixed")
.build();For Compiled APIs, an unnamed codec is the default; @NodeValue(format = "...", defaultCodec = true) declares a named default. Duplicate formats and multiple defaults in one interface are rejected. For Runtime APIs, the first registered codec is the default unless a later registration sets forceDefault to true.
Compiled Usage
Declare codec fields on a compiled interface or inherit them:
@CompiledBinder
interface OrderBinder extends AppCodecs {
@Read OrderId read(String json);
@Write String write(OrderId value);
}Generated code resolves local codecs without global registration. Runtime fallback uses only Runtime registrations. See Compiled APIs.
Raw Values and Streaming
A codec may declare a raw String, numeric type, Boolean, Map, or List—not Object. Map/List contents may contain null. Map values should use Map<String, Object>; SJF4J does not recursively bind typed Map contents at the codec boundary. For mutable logical values, override valueCopy() to define copy semantics.
For specialized parsing and writing without intermediate raw conversion, use ValueCodec.StreamingValueCodec. Streaming callbacks follow the same non-null contract: they consume or write exactly one non-null JSON value. The framework handles JSON null.
API Reference: ValueCodec · ValueRegistry