# `Barcode`

What a barcode encodes, and which kind of barcode it is.

Use it when one field can hold several kinds, such as tickets and products together. If you only have product codes, [`Ean13`](/docs/types/type-ean13.md) or [`UpcA`](/docs/types/type-upc-a.md) alone is simpler.

## Shape

One of these, each with its own fields:

| Variant | Field | Type | Required? |
|---|---|---|---|
| `Barcode.ean13` | `data` | [`Ean13`](/docs/types/type-ean13.md) | Yes |
| `Barcode.upc_a` | `data` | [`UpcA`](/docs/types/type-upc-a.md) | Yes |
| `Barcode.code128` | `data` | `Text` | Yes |
| `Barcode.qr_code` | `data` | `Text` | Yes |

## Example

```jadpo
Barcode.qr_code { data: "https://example.com/tickets/42" }
```

## Good to know

- The others are `Barcode.ean13`, `Barcode.upc_a` and `Barcode.code128`, each with `data`. An `ean13` checks its digits. `code128` takes printable ASCII only.
- It holds the data, not an image of the barcode.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it.

## In JSON

An object of its variant's name, as `tag`, and the variant's fields, in a request, a response or `json.write`:

```json
{"tag":"ean13","data":"4006381333931"}
{"tag":"upc_a","data":"036000291452"}
{"tag":"code128","data":"CTN-0042"}
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`Barcode` is in the group Commerce and logistics, and has been in Jadpo since 0.1.0.

### Text form

It has no single text form, so `text.from` and `text.parse` don't take it.

### How it is stored

It is stored in 2 columns, each named for the field, `_` and its suffix. This is `{"tag":"code128","data":"CTN-0042"}`:

| Column | Type | Holds |
|---|---|---|
| `<field>_kind` | `text` | `'code128'` |
| `<field>_data` | `text` | `'CTN-0042'` |

### How it is shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `{"tag":"ean13","data":"4006381333931"}` | `4006381333931` |
| `{"tag":"upc_a","data":"036000291452"}` | `036000291452` |
| `{"tag":"code128","data":"CTN-0042"}` | `CTN-0042` |
| `{"tag":"qr_code","data":"https://example.com/a1"}` | `https://example.com/a1` |

### Operations

It also takes `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
