vcard - vCard (RFC 6350) contacts build and parse
Import with import "vcard.j" as vcard;. Build contact Cards and encode them to vCard 4.0 text (VCARD records), and parse that text back. The contacts counterpart to ical: the two share the same content-line codec (TEXT escaping, 75-character line folding). Pure Jennifer over strings / lists, so it runs on both binaries.
import "vcard.j" as vcard;
def c as vcard.Card init vcard.card("Ada Lovelace");
$c = vcard.withName($c, "Lovelace", "Ada");
$c = vcard.addEmail($c, "ada@example.com");
def text as string init vcard.encode($c); # BEGIN:VCARD ... END:VCARDRunnable: examples/modules/vcard_demo.j.
Types
Both structs have public fields (read them directly - $c.emails, $c.family); the builder functions are the conventional way to construct them.
Card:
| Field | Type | Property |
|---|---|---|
formattedName | string | FN (required by vCard 4.0) |
family, given, additional, prefixes, suffixes | string | the five N components (family, given, middle names, honorific prefixes / suffixes) |
nickname | string | NICKNAME |
organization | string | ORG |
title | string | TITLE |
emails | list of Typed | EMAIL (0..n) |
phones | list of Typed | TEL (0..n) |
addresses | list of Address | ADR (0..n) |
url | string | URL |
bday | string | BDAY |
photo | string | PHOTO URI |
categories | list of string | CATEGORIES tags |
note | string | NOTE |
A string field is "" when unset.
| Struct | Fields |
|---|---|
Typed | value, type (the TYPE parameter: "work" / "home" / ...; "" if none) |
Address | street, locality, region, postalCode, country, type |
VcardError | kind, message, line |
Building
The builders are value-semantic - each returns a fresh copy and never mutates its argument, so you thread them:
| Call | Returns | |
|---|---|---|
vcard.card(formattedName) | Card | a card with just its FN display name |
vcard.withName(c, family, given) | Card | set the N family / given |
vcard.withFullName(c, family, given, additional, prefixes, suffixes) | Card | set all five N components |
vcard.withNickname(c, nickname) | Card | set the NICKNAME |
vcard.withOrg(c, organization, title) | Card | set ORG and TITLE |
vcard.addEmail(c, email) / addEmailTyped(c, email, type) | Card | append an EMAIL (with optional TYPE) |
vcard.addPhone(c, phone) / addPhoneTyped(c, phone, type) | Card | append a TEL (with optional TYPE) |
vcard.address(street, locality, region, postalCode, country) / addressTyped(..., type) | Address | build an address (with optional TYPE) |
vcard.addAddress(c, address) | Card | append an ADR |
vcard.withBday(c, bday) / withPhoto(c, uri) | Card | set BDAY / PHOTO |
vcard.addCategory(c, tag) | Card | append a CATEGORIES tag |
vcard.withUrl(c, url) | Card | set the URL |
vcard.withNote(c, note) | Card | set the NOTE |
import "vcard.j" as vcard;
def c as vcard.Card init vcard.card("Grace Hopper");
$c = vcard.withName($c, "Hopper", "Grace");
$c = vcard.withOrg($c, "US Navy", "Rear Admiral");
$c = vcard.addEmail($c, "grace@navy.mil");
$c = vcard.addAddress($c, vcard.address("1 Navy Way", "Arlington", "VA", "22202", "USA"));Encoding and parsing
| Call | Returns | |
|---|---|---|
vcard.encode(c) | string | one card as a VCARD (CRLF-terminated) |
vcard.encodeAll(cards) | string | many cards concatenated |
vcard.parse(text) | list of Card | parse one or many VCARDs |
parse(encode(card)) round-trips the data. parse always returns a list (a vCard file may hold many cards; blank text holds none). encode writes VERSION:4.0, escapes text values, folds long lines, and omits empty optional fields. parse reads vCard 2.1, 3.0 and 4.0: it unfolds folded lines, strips a property group prefix (item1.EMAIL reads as EMAIL), decodes 2.1 quoted-printable values (ENCODING=QUOTED-PRINTABLE or a bare QUOTED-PRINTABLE, soft line breaks included) in their CHARSET (UTF-8 when unset; otherwise an encoding codec such as ISO-8859-1 or windows-1252), reads the TYPE parameter on EMAIL / TEL / ADR (e.g. EMAIL;TYPE=work -> Typed.type "work") and ignores every other property parameter, reads the structured N / ADR values, and unescapes text.
Errors
parse is strict: malformed input throws a vcard.VcardError (kind "vcard") whose line is the 1-based physical line of the fault (a folded line counts from its first line) and whose message starts line N: . It throws for text outside BEGIN:VCARD ... END:VCARD, a card without END:VCARD, a BEGIN inside a card, an END other than END:VCARD, a line without a :, a malformed group prefix, a VERSION other than 2.1 / 3.0 / 4.0, a 3.0 / 4.0 card (or one without VERSION) that has no FN, and a quoted-printable value with a bad =XX escape, an unknown CHARSET, or bytes invalid in it.
import "vcard.j" as vcard;
use io;
try {
vcard.parse("BEGIN:VCARD\r\nVERSION:4.0\r\nN:Lovelace;Ada;;;\r\nEND:VCARD\r\n");
} catch (e) {
if ($e.kind == "vcard") {
io.printf("%s\n", $e.message); # line 1: card has no FN (required in vCard 3.0 and 4.0)
}
}Structured values
N (name), ADR (address), and ORG are structured: components are separated by ;. N is Family;Given;Additional;Prefix;Suffix (this module sets and reads all five; withName fills only family and given, withFullName all five); ADR is POBox;Extended;Street;Locality;Region;PostalCode;Country (this module sets the last five, leaving PO box and extended empty); ORG is Organization;Unit;... (this module sets and reads the first component). A ; or , inside a component value is escaped, so it never splits the structure.
Scope
- A contact subset.
FN/N/NICKNAME/ORG/TITLE/EMAIL/TEL/ADR/BDAY/PHOTO/CATEGORIES/URL/NOTE. NoGEO/TZ/KIND, a property group is read but not kept (item1.EMAIL->EMAIL), no parameter round-tripping beyondTYPEonEMAIL/TEL/ADR(any other parameter on input is dropped), and theADRPO-box / extended components are not modelled. The escaping / folding / structured-value discipline is the reusable core. - vCard 4.0 output.
encodealways writesVERSION:4.0;parsereads 2.1, 3.0 and 4.0 (a missingVERSIONline is read by the 4.0 rules).
See also
- ical.md - the calendar counterpart sharing the content-line codec.
- strings.md - the text surface the codec is built on.
- modules/index.md - the module catalog and import rules.
Academy talks
| Talk | What it shows |
|---|---|
| Your own small blog with Jennifer | a vCard contact file for the author |
API reference
Every exported function, struct and constant of vcard, with its signature, generated from the module's doc comments: the vcard API reference.