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

Published on May 3, 2016 by abundantcode

Filed under Json.NET

Last modified May 3, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 4 (1 votes)

This article have been viewed 5804 times

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.

Leave a Comment


x

You might be Interested in these Posts

C# Program to swap two numbers without using temporary variable

Problem Write a program in Visual CSharp to swap two numbers using temporary variable and display t...

C# Program to swap two numbers

Problem Write a program in C# to swap two numbers using temporary variable and display the result i...

C# Program to print the sum of two numbers

Problem Write a program in C# to add two numbers and display the result in copnsole window. How to...