---
title: "Encoding and Data"
description: "The @Var, @URI, @MIME, @ZLIB, and @XML classes for serialization, URL handling, encoding, compression, and XML."
canonical_url: "https://www.zeyos.com/zymba/zymba-guide/standard-library/encoding-data"
markdown_url: "https://www.zeyos.com/ai/guide/zymba/zymba-guide/standard-library/encoding-data.md"
section: "zymba-guide"
authority: "draft-guide"
language: "en"
source_repository: "zeyos/guide"
source_path: "docs/zymba/07-standard-library/encoding-data.md"
source_revision: "cce886a8da98cd124ee3644711085bbdcf1a87ce"
---

# Encoding and Data

## @Var — type utilities and serialization (30 methods)

### JSON encoding/decoding

```zymba
$data = [name: "Alice", age: 30, tags: ["admin", "user"]];

$json   = @Var.toJSON($data);
// '{"name":"Alice","age":30,"tags":["admin","user"]}'

$pretty = @Var.toPrettyJSON($data);
// Formatted with indentation

$parsed = @Var.fromJSON($json);
echo $parsed.name; // "Alice"

// Validate before parsing
if (@Var.isValidJSON($input)) {
    $data = @Var.fromJSON($input);
}
```

### PHP serialization

```zymba
$serialized = @Var.toSerializedPHP([a: 1, b: 2]);
$restored   = @Var.fromSerializedPHP($serialized);
echo $restored.a; // 1
```

### Binary pack/unpack

```zymba
$packed   = @Var.pack("N", 12345);      // Pack as network byte order
$unpacked = @Var.unpack("N", $packed);  // Unpack
```

### Type inspection

```zymba
echo @Var.typeOf(42);        // "int"
echo @Var.typeOf("hello");   // "string"
echo @Var.typeOf([1, 2]);    // "object"

echo @Var.isInt(42);         // true
echo @Var.isFloat(3.14);     // true
echo @Var.isNumber(42);      // true (int or float)
echo @Var.isNumeric("42");   // true (string that looks like a number)
echo @Var.isString("hi");    // true
echo @Var.isBool(true);      // true
echo @Var.isObject([]);       // true
echo @Var.isNull(null);       // true
echo @Var.isFunction(function(){}); // true
echo @Var.isScalar("hi");    // true (string, int, float, or bool)
echo @Var.isComplex([1,2]);   // true (object/array)
echo @Var.isEmpty("");        // true
echo @Var.exists(null);       // false
echo @Var.count([1,2,3]);     // 3
echo @Var.compare(1, 2);      // -1
```

### Cloning

```zymba
$shallow = @Var.clone([a: [1, 2]]);     // Shallow copy
$deep    = @Var.cloneDeep([a: [1, 2]]); // Deep copy

$deep.a[] = 3; // Only affects the deep copy
```

### Source representation

```zymba
echo @Var.toSource([1, "two", true, null]);
// Zymba-syntax representation of the value
```

### Global variable introspection

```zymba
$count = @Var.countGlobalVariables();
$names = @Var.listGlobalVariableNames();
$vars  = @Var.listGlobalVariables();
```

---

## @URI — URL operations (20 methods)

### Encoding/decoding

```zymba
echo @URI.encode("hello world");   // "hello%20world"
echo @URI.decode("hello%20world"); // "hello world"
```

### Query strings

```zymba
// Build query string from array
$qs = @URI.createQuery([name: "Alice", age: "30", city: "NYC"]);
// "name=Alice&age=30&city=NYC"

// Parse query string to array
$params = @URI.parseQuery("name=Alice&age=30");
// [name: "Alice", age: "30"]
```

### URL parsing

```zymba
$url = "https://user:pass@example.com:8080/path?q=1#frag";

echo @URI.getScheme($url);    // "https"
echo @URI.getHost($url);      // "example.com"
echo @URI.getPort($url);      // "8080"
echo @URI.getPath($url);      // "/path"
echo @URI.getQuery($url);     // "q=1"
echo @URI.getFragment($url);  // "frag"
echo @URI.getUser($url);      // "user"
echo @URI.getPassword($url);  // "pass"
```

### Validation

```zymba
echo @URI.isValid("https://example.com");               // true
echo @URI.isAbsolute("https://example.com/path");       // true
echo @URI.normalize("https://example.com/../a/./b");    // normalized
```

### Data URIs

```zymba
$dataUri = @URI.createDataURI("Hello", "text/plain", "utf-8");
// "data:text/plain;charset=utf-8,Hello"
```

---

## @MIME — MIME encoding (12 methods)

```zymba
echo @MIME.toBase64("Hello World");
// "SGVsbG8gV29ybGQ="

echo @MIME.fromBase64("SGVsbG8gV29ybGQ=");
// "Hello World"
```

---

## @ZLIB — compression (6 methods)

```zymba
// Deflate/inflate (raw compression)
$compressed = @ZLIB.deflate("Hello World");
$original   = @ZLIB.inflate($compressed);

// Raw deflate (no headers)
$raw      = @ZLIB.deflateRaw("Hello World");
$original = @ZLIB.inflateRaw($raw);

// Gzip format
$gzipped  = @ZLIB.gzip("Hello World");
$original = @ZLIB.gunzip($gzipped);
```

---

## @XML — XML parsing (5 methods)

```zymba
// Parse XML to tree structure
$xml  = "<root><item id=\"1\">Hello</item><item id=\"2\">World</item></root>";
$tree = @XML.parseToTree($xml);
echo $tree.children[0].cdata; // "Hello"

// Create XML from tree
$xmlStr = @XML.createFromTree($tree);

// Parse to array
$data = @XML.parse($xml);

// Escape/unescape XML content
echo @XML.escape("<Hello & World>");  // "&lt;Hello &amp; World&gt;"
echo @XML.unescape("&lt;test&gt;");   // "<test>"
```
