Json.NET & Oxygene – How to Serialize a Collection?

Do you want to serialize an collection in your Remobjects Oxygene.NET application?. Json.NET supports this functionality with ease.

The Collection can be an Array , Dictionary or List. You need to simply pass collection to the JsonConvert.SerializeObject static method which would serialize the collection and return you the Json string.

How to Serialize a Collection in Oxygene.NET using Json.NET library ?

For example , assume that you want to serialize the employee Oxygene.NET class as shown below.

type
  Employee = public class
  public
    property Name: String;
    property IsPermanent: Boolean;
    property Departments: List<String>;
end;

You will create the List<Employee> and fill it with the values as shown below.

var employees: List<Employee> := new List<Employee>();
// Employee 1 Data
var emp1: Employee := new Employee();
emp1.Name := 'Employee 1';
emp1.IsPermanent := true;
var departments: List<String> := new List<String>();
departments.Add('Technology');
departments.Add('Design');
emp1.Departments := departments;
employees.Add(emp1);
//  Employee 2 Data
var emp2: Employee := new Employee();
emp2.Name := 'Employee 2';
emp2.IsPermanent := false;
var departments1: List<String> := new List<String>();
departments1.Add("Testing");
emp2.Departments := departments1;
employees.Add(emp2);

Once the List<Employee> data is available , pass it to the JsonConvert.Serialize method as shown below.

//  Convert the collection to Json string - Serialize data
var jsonData: String := JsonConvert.SerializeObject(employees);

Below is the complete code snippet that is used  in this blog post for serializing the collection in Oxygene.NET.

namespace ACConsoleOxygene;

interface

uses
  System,
  System.Collections.Generic,
  Newtonsoft.Json;

type
  Program = class
  private
    class method Main(args: array of String);
  public
    class method SerializeCollection: String;
  end;

  Employee = public class
  public
    property Name: String;
    property IsPermanent: Boolean;
    property Departments: List<String>;
  end;

implementation

class method Program.Main(args: array of String);
begin
  var jsonData := SerializeCollection();
  Console.WriteLine('Serialized Data : ' + jsonData);
  Console.ReadLine();
end;

class method Program.SerializeCollection: String;
begin
  var employees: List<Employee> := new List<Employee>();
  // Employee 1 Data
  var emp1: Employee := new Employee();
  emp1.Name := 'Employee 1';
  emp1.IsPermanent := true;
  var departments: List<String> := new List<String>();
  departments.Add('Technology');
  departments.Add('Design');
  emp1.Departments := departments;
  employees.Add(emp1);
  //  Employee 2 Data
  var emp2: Employee := new Employee();
  emp2.Name := 'Employee 2';
  emp2.IsPermanent := false;
  var departments1: List<String> := new List<String>();
  departments1.Add("Testing");
  emp2.Departments := departments1;
  employees.Add(emp2);

  //  Convert the collection to Json string - Serialize data
  var jsonData: String := JsonConvert.SerializeObject(employees);
  // Return Json data
  exit jsonData;
end;

end.
%d