Add GVariant pack and unpack methods #264

Merged
jwharm merged 1 commit from variant-pack-unpack into main 2025-08-24 12:21:04 +02:00
jwharm commented 2025-08-24 12:20:57 +02:00 (Migrated from github.com)

This adds three new methods to the org.gnome.glib.Variant class:

  • public static Variant pack(Object object) to create a Variant from a Java Object
  • public Object unpack() to unpack a Variant to a Java Object
  • public Object unpackRecursive() to unpack a Variant to a Java Object, recursively unpacking nested Variants too.

In most cases, packing and unpacking an object will result in an object that equals the original, but this is not always the case, because during the pack/unpack operations, some information gets lost. For example:

  • A Variant with an unsigned datatype is unpacked to a signed Java value
  • A Variant with an object path or type signature is unpacked to a Java String
  • Conversely, both Java List and Set are packed into an array-type Variant
  • A Java Optional<?> is packed into a maybe-type Variant, but a maybe-type Variant is unpacked into the Java value or null.
This adds three new methods to the `org.gnome.glib.Variant` class: * `public static Variant pack(Object object)` to create a Variant from a Java Object * `public Object unpack()` to unpack a Variant to a Java Object * `public Object unpackRecursive()` to unpack a Variant to a Java Object, recursively unpacking nested Variants too. In most cases, packing and unpacking an object will result in an object that equals the original, but this is not always the case, because during the pack/unpack operations, some information gets lost. For example: * A Variant with an unsigned datatype is unpacked to a signed Java value * A Variant with an object path or type signature is unpacked to a Java String * Conversely, both Java `List` and `Set` are packed into an array-type Variant * A Java `Optional<?>` is packed into a maybe-type Variant, but a maybe-type Variant is unpacked into the Java value or null.
Sign in to join this conversation.
No description provided.