Creates a new GVariant with the specified type signature and value.
The GVariant type signature (e.g., 's' for string, 'i' for int32, 'a{sv}' for dictionary)
The JavaScript value to pack into the variant
Alias for deepUnpack method.
Recursively unpacks the variant's data into JavaScript values up to one level deep. This is the snake_case version of the same functionality.
The deeply unpacked JavaScript value
deepUnpack for the camelCase version with full documentation
Alias for deepUnpack method.
Recursively unpacks the variant's data into JavaScript values up to one level deep. This is the snake_case version of the same functionality.
The deeply unpacked JavaScript value
deepUnpack for the camelCase version with full documentation
Alias for deepUnpack method.
Recursively unpacks the variant's data into JavaScript values up to one level deep. This is the snake_case version of the same functionality.
The deeply unpacked JavaScript value
deepUnpack for the camelCase version with full documentation
Recursively unpacks the variant's data into JavaScript values.
This method unpacks a variant and its direct children, but only up to one level deep. It's the most commonly used unpacking method for D-Bus operations and GSettings.
With advanced variants enabled, this method provides automatic type inference based on the variant's type signature. You can also explicitly specify a type parameter for backward compatibility.
The deeply unpacked JavaScript value with one level of children unpacked
// Simple dictionary (a{ss}) - fully unpacked
const simpleDict = new GLib.Variant('a{ss}', {
'key1': 'value1',
'key2': 'value2'
});
const simple = simpleDict.deepUnpack(); // → { key1: "value1", key2: "value2" }
// Complex dictionary (a{sv}) - values remain as Variants
const complexDict = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'active': GLib.Variant.new_boolean(true)
});
const complex = complexDict.deepUnpack(); // → { name: Variant<"s">, active: Variant<"b"> }
// Automatic type inference (Advanced Variants)
const autoInferred = variant.deepUnpack(); // Types inferred from signature
// Explicit type parameter (backward compatibility)
const explicit = variant.deepUnpack<{ [key: string]: GLib.Variant }>();
// String arrays are fully unpacked
const strArray = GLib.Variant.new_strv(['one', 'two']);
const strings = strArray.deepUnpack(); // → ["one", "two"]
Recursively unpacks the variant's data into JavaScript values.
This method unpacks a variant and its direct children, but only up to one level deep. It's the most commonly used unpacking method for D-Bus operations and GSettings.
With advanced variants enabled, this method provides automatic type inference based on the variant's type signature. You can also explicitly specify a type parameter for backward compatibility.
The expected return type (defaults to automatically inferred type)
The deeply unpacked JavaScript value with one level of children unpacked
// Simple dictionary (a{ss}) - fully unpacked
const simpleDict = new GLib.Variant('a{ss}', {
'key1': 'value1',
'key2': 'value2'
});
const simple = simpleDict.deepUnpack(); // → { key1: "value1", key2: "value2" }
// Complex dictionary (a{sv}) - values remain as Variants
const complexDict = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'active': GLib.Variant.new_boolean(true)
});
const complex = complexDict.deepUnpack(); // → { name: Variant<"s">, active: Variant<"b"> }
// Automatic type inference (Advanced Variants)
const autoInferred = variant.deepUnpack(); // Types inferred from signature
// Explicit type parameter (backward compatibility)
const explicit = variant.deepUnpack<{ [key: string]: GLib.Variant }>();
// String arrays are fully unpacked
const strArray = GLib.Variant.new_strv(['one', 'two']);
const strings = strArray.deepUnpack(); // → ["one", "two"]
Recursively unpacks the variant's data into JavaScript values.
This method unpacks a variant and its direct children, but only up to one level deep. It's the most commonly used unpacking method for D-Bus operations and GSettings.
With advanced variants enabled, this method provides automatic type inference based on the variant's type signature. You can also explicitly specify a type parameter for backward compatibility.
The deeply unpacked JavaScript value with one level of children unpacked
// Simple dictionary (a{ss}) - fully unpacked
const simpleDict = new GLib.Variant('a{ss}', {
'key1': 'value1',
'key2': 'value2'
});
const simple = simpleDict.deepUnpack(); // → { key1: "value1", key2: "value2" }
// Complex dictionary (a{sv}) - values remain as Variants
const complexDict = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'active': GLib.Variant.new_boolean(true)
});
const complex = complexDict.deepUnpack(); // → { name: Variant<"s">, active: Variant<"b"> }
// Automatic type inference (Advanced Variants)
const autoInferred = variant.deepUnpack(); // Types inferred from signature
// Explicit type parameter (backward compatibility)
const explicit = variant.deepUnpack<{ [key: string]: GLib.Variant }>();
// String arrays are fully unpacked
const strArray = GLib.Variant.new_strv(['one', 'two']);
const strings = strArray.deepUnpack(); // → ["one", "two"]
Checks if two variants are equal.
true if the variants are equal, false otherwise
Extracts a bytestring from a bytestring variant.
The byte array
Extracts a string array from a string array variant.
Array of strings
Gets the type of the variant.
The VariantType representing this variant's type
Gets the type signature string of the variant.
This is very useful for debugging and type checking.
The type signature string (e.g., 's', 'i', 'a{sv}')
Checks if the variant is a container type.
Container types include arrays, tuples, dictionaries, and maybes.
true if the variant is a container
Optionalexpected_type: GLib.VariantType<any>Gets the number of children in a container variant.
The number of child elements
Creates a string representation of the variant.
This is extremely useful for debugging GVariant structures.
Whether to include type annotations in the output
A string representation of the variant
const variant = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'lives': GLib.Variant.new_uint32(3)
});
// Without type annotations
print(variant.print(false)); // → "{'name': 'Mario', 'lives': 3}"
// With type annotations
print(variant.print(true)); // → "{'name': <'Mario'>, 'lives': <uint32 3>}"
Recursively unpacks the variant and all its descendants into native JavaScript values.
Available since GJS 1.64 (GNOME 3.36)
This method performs complete recursive unpacking, converting all nested Variants to their native JavaScript equivalents. Type information may be lost during this process, so you'll need to know the original types to repack values.
The recursively unpacked JavaScript value with all Variants converted to native types
// Complex nested structure fully unpacked
const complexDict = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'lives': GLib.Variant.new_uint32(3),
'active': GLib.Variant.new_boolean(true)
});
const fullyUnpacked = complexDict.recursiveUnpack();
// → { name: "Mario", lives: 3, active: true }
// All nested Variants are converted to native values
const nestedTuple = new GLib.Variant('(sa{sv})', [
'player',
{ 'score': GLib.Variant.new_int32(100) }
]);
const result = nestedTuple.recursiveUnpack();
// → ["player", { score: 100 }]
Recursively unpacks the variant and all its descendants into native JavaScript values.
Available since GJS 1.64 (GNOME 3.36)
This method performs complete recursive unpacking, converting all nested Variants to their native JavaScript equivalents. Type information may be lost during this process, so you'll need to know the original types to repack values.
The recursively unpacked JavaScript value with all Variants converted to native types
// Complex nested structure fully unpacked
const complexDict = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'lives': GLib.Variant.new_uint32(3),
'active': GLib.Variant.new_boolean(true)
});
const fullyUnpacked = complexDict.recursiveUnpack();
// → { name: "Mario", lives: 3, active: true }
// All nested Variants are converted to native values
const nestedTuple = new GLib.Variant('(sa{sv})', [
'player',
{ 'score': GLib.Variant.new_int32(100) }
]);
const result = nestedTuple.recursiveUnpack();
// → ["player", { score: 100 }]
Recursively unpacks the variant and all its descendants into native JavaScript values.
Available since GJS 1.64 (GNOME 3.36)
This method performs complete recursive unpacking, converting all nested Variants to their native JavaScript equivalents. Type information may be lost during this process, so you'll need to know the original types to repack values.
The recursively unpacked JavaScript value with all Variants converted to native types
// Complex nested structure fully unpacked
const complexDict = new GLib.Variant('a{sv}', {
'name': GLib.Variant.new_string('Mario'),
'lives': GLib.Variant.new_uint32(3),
'active': GLib.Variant.new_boolean(true)
});
const fullyUnpacked = complexDict.recursiveUnpack();
// → { name: "Mario", lives: 3, active: true }
// All nested Variants are converted to native values
const nestedTuple = new GLib.Variant('(sa{sv})', [
'player',
{ 'score': GLib.Variant.new_int32(100) }
]);
const result = nestedTuple.recursiveUnpack();
// → ["player", { score: 100 }]
Unpacks the variant's data into a JavaScript value.
This performs a shallow unpacking operation - only unpacking the top level. For containers like arrays or dictionaries, child elements remain as Variant objects.
The unpacked JavaScript value with child Variants preserved
// Simple types are fully unpacked
const boolVariant = GLib.Variant.new_boolean(true);
const boolValue = boolVariant.unpack(); // → true
// String values are unpacked (discarding length information)
const stringVariant = GLib.Variant.new_string("hello");
const stringValue = stringVariant.unpack(); // → "hello"
// Arrays are unpacked but elements remain as Variants
const arrayVariant = GLib.Variant.new_strv(["one", "two"]);
const arrayValue = arrayVariant.unpack(); // → [Variant<"s">, Variant<"s">]
Unpacks the variant's data into a JavaScript value.
This performs a shallow unpacking operation - only unpacking the top level. For containers like arrays or dictionaries, child elements remain as Variant objects.
The unpacked JavaScript value with child Variants preserved
// Simple types are fully unpacked
const boolVariant = GLib.Variant.new_boolean(true);
const boolValue = boolVariant.unpack(); // → true
// String values are unpacked (discarding length information)
const stringVariant = GLib.Variant.new_string("hello");
const stringValue = stringVariant.unpack(); // → "hello"
// Arrays are unpacked but elements remain as Variants
const arrayVariant = GLib.Variant.new_strv(["one", "two"]);
const arrayValue = arrayVariant.unpack(); // → [Variant<"s">, Variant<"s">]
Unpacks the variant's data into a JavaScript value.
This performs a shallow unpacking operation - only unpacking the top level. For containers like arrays or dictionaries, child elements remain as Variant objects.
The unpacked JavaScript value with child Variants preserved
// Simple types are fully unpacked
const boolVariant = GLib.Variant.new_boolean(true);
const boolValue = boolVariant.unpack(); // → true
// String values are unpacked (discarding length information)
const stringVariant = GLib.Variant.new_string("hello");
const stringValue = stringVariant.unpack(); // → "hello"
// Arrays are unpacked but elements remain as Variants
const arrayVariant = GLib.Variant.new_strv(["one", "two"]);
const arrayValue = arrayVariant.unpack(); // → [Variant<"s">, Variant<"s">]
Static_Staticis_Staticis_StaticnewCreates a new GVariant with the specified type signature and value.
This is equivalent to using the constructor directly.
The GVariant type signature
The JavaScript value to pack
A new GVariant instance
Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_Staticnew_StaticparseOptionallimit: stringOptionalendptr: stringStaticparse_Staticparse_Staticparser_
GLib.Variant is a value container whose types are determined at construction.
It serves as a reliable and efficient format for storing structured data that can be serialized while preserving type information. Comparable to JSON, but with strong typing and support for special values like file handles.
GVariant is used throughout the GNOME Platform including GDBus, GSettings, GAction, GMenu and many other APIs. All D-Bus method, property and signal values are GVariant objects.
Example
See