Skip to main content

Function modifiers

serialize

Commentary

added in 0.19.2

Serializes a value to its final type before being written to an output target.

For the most part, ShadowTraffic is able to implicitly figure out the right serialization formats for generated values before writing to output systems. But sometimes, the underlying types can't be guessed or need to be explicitly set. This function modifier helps bridge that gap.

Note that after a value is serialized, it can't and shouldn't be operated on further. For example, if you serialize a double into an avroDecimal, the underlying value becomes a byte sequence and shouldn't be further manipulated with ShadowTraffic.


Examples

Serialize to Avro Decimal

Use type set to avroDecimal to coerce the value into an Avro-compliant bytes. Be sure to also set scale.

For example, if you had Avro schema:

{
"type": "bytes",
"logicalType": "decimal",
"precision": 18,
"scale": 3
}

You can serialize the value in ShadowTraffic like so:

{
"_gen": "normalDistribution",
"mean": 50,
"sd": 5,
"serialize": {
"type": "avroDecimal",
"scale": 3
}
}
[
"\u0000“n",
"\u0000Út",
"\u0000Û7",
"\u0000¿C",
"\u0000àø"
]

Serialize to Avro Fixed

Use type set to avroFixed to coerce the value into an Avro-compliant byte array.

For example, if you had Avro schema:

{
"type": "fixed",
"size": 16,
"name": "md5"
}

You can serialize the value in ShadowTraffic like so:

{
"_gen": "bytes",
"size": 16,
"serialize": {
"type": "avroFixed"
}
}
[
".{7ô¨ÌΒ°&“ë>á`\u001f",
"BÏ7œ&ÏòJ$K\u0006|\u0015‰Ìü",
"Ôk\u0012jºRôåô~J½ú\u0010\u0014",
"\u001bQ\u0016me|ƒ\u0007+Ú\u001b‡:é&>",
"Ïák\u0018\u001dhó€\u0017/ÖY©æ{^"
]

Serialize to Postgres timestamp

Use type set to postgresTimestamp to coerce the value into a Postgres TIMESTAMP or TIMESTAMPZ type.

{
"timestamp": {
"_gen": "now",
"serialize": {
"type": "postgresTimestamp"
}
}
}
[
{
"timestamp": "2024-04-25T20:28:47.262Z"
},
{
"timestamp": "2024-04-25T20:28:47.263Z"
},
{
"timestamp": "2024-04-25T20:28:47.264Z"
},
{
"timestamp": "2024-04-25T20:28:47.265Z"
},
{
"timestamp": "2024-04-25T20:28:47.266Z"
}
]

Specification

JSON schema

{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"avroFixed",
"avroDecimal"
]
}
},
"required": [
"type"
]
}