Class ForyCodecFactory

java.lang.Object
org.jwcarman.codec.fory.ForyCodecFactory
All Implemented Interfaces:
CodecFactory

public class ForyCodecFactory extends Object implements CodecFactory
A CodecFactory backed by Apache Fory: fast binary serialization for JVM-to-JVM payloads such as cache entries, queue messages and journal records.

Class registration is mandatory. Fory refuses to serialize or deserialize a class that has not been registered, which is what closes the deserialization-gadget class of attacks. This factory never relaxes that: build the ThreadSafeFory with requireClassRegistration(true) (the library default, and what of(Class[]) does) and register every type a codec will carry, including the element types of collections.

of(Class[]) is a helper: it applies Fory's security guidance and otherwise leaves Fory's defaults alone, so the wire format follows them. The security guidance is registration required plus deserializeUnknownClass(false) — the one recommended safeguard Fory does not apply by default in compatible mode. Without it a payload naming a class this instance has not registered is materialised from its metadata as an anonymous struct; with it the payload is rejected. Fory's other safeguards (a read depth of 50, a 128 MiB graph-memory gate, bounds on container and metadata sizes) are already its defaults and are left as they are. Since Fory 1.2.0 that default is compatible mode — each payload carries its class schema, so a reader whose class has gained or lost a field since the payload was written still decodes it correctly, where schema-consistent mode returns a wrong object without an error. It costs a few bytes of metadata per class per message: negligible past a few fields, but a four-field record roughly doubles. A caller who wants a particular mode — schema-consistent for the smallest output, or any setting pinned against future Fory defaults — builds their own ThreadSafeFory and passes it to the constructor.

The wire format is Fory's own and JVM-specific. It is the right choice when Java is on both ends and speed and size matter; it is the wrong choice for anything another language will read, or for data that must outlive the classes that wrote it — use a schema-based or JSON backend for those.

Codecs must be thread-safe, and a plain Fory is not, so this factory accepts only a ThreadSafeFory — which is what of(Class[]) builds, and what Fory.builder()...buildThreadSafeFory() gives a caller who configures their own.

create(TypeRef) fails fast: if the instance requires registration and the requested type — or any class named in its type arguments — is not registered, it throws IllegalArgumentException at creation rather than letting the first encode fail in production. The check walks declared generics (List<Person> checks Person); it cannot see types that only appear at runtime inside registered classes' fields, which Fory itself still rejects.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ForyCodecFactory(org.apache.fory.ThreadSafeFory fory)
    Creates a factory over a caller-configured thread-safe Fory instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> Codec<T>
    create(TypeRef<T> typeRef)
    Creates a codec for the given type reference, supporting full generic types.
    of(Class<?>... classes)
    Creates a factory over a new thread-safe Fory instance, in Java mode with class registration required, unknown-class deserialization disabled, and Fory's other defaults unchanged, with the given classes registered.
    boolean
    supports(Class<?> type)
    Reports whether this factory's Fory instance will serialize values of the given class.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface CodecFactory

    create
  • Constructor Details

    • ForyCodecFactory

      public ForyCodecFactory(org.apache.fory.ThreadSafeFory fory)
      Creates a factory over a caller-configured thread-safe Fory instance.
      Parameters:
      fory - the Fory instance every codec will serialize through; build it with class registration required unless you have a specific reason not to
  • Method Details

    • of

      public static ForyCodecFactory of(Class<?>... classes)
      Creates a factory over a new thread-safe Fory instance, in Java mode with class registration required, unknown-class deserialization disabled, and Fory's other defaults unchanged, with the given classes registered. Register every type a codec will carry — records, beans, and the element types of the collections they hold; the JDK's collections and boxed types are registered by Fory itself.
      Parameters:
      classes - the classes codecs from this factory may serialize
      Returns:
      a factory ready to create codecs for the registered classes
    • create

      public <T> Codec<T> create(TypeRef<T> typeRef)
      Description copied from interface: CodecFactory
      Creates a codec for the given type reference, supporting full generic types.
      Specified by:
      create in interface CodecFactory
      Type Parameters:
      T - the codec's value type
      Parameters:
      typeRef - the type to create a codec for
      Returns:
      a codec for the type
    • supports

      public boolean supports(Class<?> type)
      Reports whether this factory's Fory instance will serialize values of the given class. True when registration is not required; when the class is registered; or when the class is not something Fory serializes by its own identity — a JDK type, an interface, or an abstract class — because Fory registers the JDK's concrete types itself and decides interfaces and abstract types by the runtime class of each value.
      Parameters:
      type - the class to check
      Returns:
      whether codecs from this factory can carry values declared as that class