top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

0 votes
269 views
How to Serialize a Collection in Oxygene.NET using Json.NET library ?
posted May 25, 2016 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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 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 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.
answer May 25, 2016 by Shivaranjini
...