encode
Encode and decode data across common formats: base64, hex, CSV, big-endian binary integers and floats, plus JSON and XML helpers. Import it with import encode.
encode.base64
| Function | Signature | Description |
|---|
encode | encode.base64.encode(s: string) → string | Standard base64-encode a string |
decode | encode.base64.decode(s: string) → string | Decode a standard base64 string |
encodeURL | encode.base64.encodeURL(s: string) → string | URL-safe base64-encode a string |
decodeURL | encode.base64.decodeURL(s: string) → string | Decode a URL-safe base64 string |
encode.hex
| Function | Signature | Description |
|---|
encode | encode.hex.encode(s: string) → string | Hex-encode a string |
decode | encode.hex.decode(s: string) → string | Decode a hex string |
encode.csv
| Function | Signature | Description |
|---|
encodeRows | encode.csv.encodeRows(rows: string) → string | Encode CSV from a JSON string holding an array of string arrays |
decodeAll | encode.csv.decodeAll(data: string) → string | Parse CSV text into a JSON string of an array of string arrays |
encodeRow | encode.csv.encodeRow(row: string[]) → string | Encode a single CSV row (no trailing newline) |
encode.binary
All integers and floats use 8-byte (or 4-byte) big-endian encoding, stored as the bytes of a string.
| Function | Signature | Description |
|---|
encodeInt32 | encode.binary.encodeInt32(n: int) → string | Encode an int as 4 big-endian bytes |
encodeInt64 | encode.binary.encodeInt64(n: int) → string | Encode an int as 8 big-endian bytes |
decodeInt32 | encode.binary.decodeInt32(s: string) → int | Decode 4 big-endian bytes (0 if too short) |
decodeInt64 | encode.binary.decodeInt64(s: string) → int | Decode 8 big-endian bytes (0 if too short) |
encodeFloat64 | encode.binary.encodeFloat64(f: float) → string | Encode a float as 8-byte IEEE 754 big-endian |
decodeFloat64 | encode.binary.decodeFloat64(s: string) → float | Decode 8-byte IEEE 754 big-endian (0 if too short) |
encode.json
| Function | Signature | Description |
|---|
parse | encode.json.parse(s: string) → any | Parse JSON into a navigable value tree |
stringify | encode.json.stringify(v: any, indent?: string) → string | Serialize a value to JSON, optionally indented |
pretty | encode.json.pretty(s: string) → string | Re-indent a JSON string with two-space indentation |
valid | encode.json.valid(s: string) → bool | Report whether the string is valid JSON |
keys | encode.json.keys(v: any) → string[] | Sorted field names of an object or JSON object string |
encode.xml
| Function | Signature | Description |
|---|
valid | encode.xml.valid(s: string) → bool | Report whether the string is well-formed XML |
pretty | encode.xml.pretty(s: string) → string | Return the XML string (pass-through) |
Example
import encode
// base64 round-trip
let enc = encode.base64.encode("hello")
print(encode.base64.decode(enc)) // hello
// build CSV from rows
let csv = encode.csv.encodeRows("[[\"name\",\"age\"],[\"ada\",\"36\"]]")
print(csv)
// JSON helpers
print(encode.json.valid("{\"a\":1}")) // true
let obj = encode.json.parse("{\"a\":1,\"b\":2}")
print(encode.json.keys(obj)) // ["a", "b"]