How to create typed JSON collections

Goal

Create a statically Typed JSON array containing other Typed JSON objects.

Steps

1. Create a Typed JSON class to store in the array

For this example, we'll create a simple Typed JSON class Person. The array that we will later create will be typed to this class. For your application, this can be any Typed JSON class.

Person.json
{
"Name": ""
}
Person.json.cs
using Starcounter;
namespace TypedJSONCollectionSample
{
partial class Person : Json
{
}
}

2. Create the Typed JSON class with the array

The Typed JSON class that with the array that will hold the Person objects only needs one property: an array of objects:

PersonCollection.json
{
"People": [{}]
}
PersonCollection.json.cs
using Starcounter;
namespace TypedJSONCollectionSample
{
partial class PersonCollection : Json
{
}
}

3. Make the array typed

JSON arrays are not typed by default. To make them typed, we'll specify the instance type of the array. This is done in a static constructor in the code-behind:

PersonCollection.json.cs
using Starcounter;
namespace TypedJSONCollectionSample
{
partial class PersonCollection : Json
{
static PersonCollection()
{
DefaultTemplate.People.ElementType.InstanceType = typeof(Person);
}
}
}

Summary

With this setup, you can add instances of the Person class to the PersonCollection and know that the array only contains Person objects:

Program.cs
static void Main()
{
var alice = new Person() { Name = "Alice" };
var bob = new Person() { Name = "Bob" };
var friends = new PersonCollection();
friends.People.Add(alice);
friends.People.Add(bob);
Console.WriteLine(alice.ToJson()); // {"Name":"Alice"}
Console.WriteLine(bob.ToJson()); // {"Name":"Bob"}
Console.WriteLine(friends.ToJson()); // {"People":[{"Name":"Alice"},{"Name":"Bob"}]}
}