On 3/21/11 8:28 AM, "Curtis Jensen" <[email protected]> wrote:
>How does one create a schema for an array of records?
>
>For example, I have a record schema for a user (name and id; see
>below). I would like to serialize an array of users. I've tried
>various combinations of things, and I'm not getting it to work.
>
>Thanks,
>Curtis
>
The below Schema is a UNION of "User" and an array of "User". (I' am
ignoring the trailing unmatched "}")
>
> [
> {
> "name": "User",
> "type": "record",
> "fields": [
> {"name": "name", "type": "string"},
> {"name": "id", "type": "int"}]
> },
> {
> "type": "array", "items": "User"
> }
> ]}
If you want only an array of Users, do:
{
"type": "array", "items": {
"name": "User",
"type": "record",
"fields": [
{"name": "name", "type": "string"},
{"name": "id", "type": "int"}
]
}
}
If you want the array to be a field inside of a "Users" record, do:
{
"type": "record", "name":"Users", "fields": [
"name": "users", "type":
{"type": "array", "items": {
"name": "User",
"type": "record",
"fields": [
{"name": "name", "type": "string"},
{"name": "id", "type": "int"}
]
}
]
}
Note, that all fields have a name and type; and a fields type is either
declared by nesting in { } or with an already defined name. Arrays do not
have names, so they cannot be referenced with one.