← Back to index

How to encode a record as an array?

Sat Dec 18 2021

rescript@9.1.4

When all keys of a record are numeric and sequential:

type t = {
  @as("0") x: int,
  @as("1") y: int,
  @as("2") z: int,
}

let x = {x: 1, y: 2, z: 3}

Then the record is encoded as an array:

var x = [
  1,
  2,
  3
];

When not sequential:

type t = {
  @as("3") x: int,
  @as("4") y: int,
  @as("5") z: int,
}

let x = {x: 1, y: 2, z: 3}

Then it's encoded as an object:

var x = {
  "0": 1,
  "2": 2,
  "4": 3
};

When not starting from 0:

type t = {
  @as("1") x: int,
  @as("2") y: int,
  @as("3") z: int,
}

let x = {x: 1, y: 2, z: 3}

Then it's encoded as an object:

var x = {
  "1": 1,
  "2": 2,
  "3": 3
};

When different types:

type t = {
  @as("0") x: int,
  @as("1") y: float,
  @as("2") z: bool,
}

let x = {x: 1, y: 2.0, z: true}

Same rules as above:

var x = [
  1,
  2.0,
  true
];