Json.NET & Oxygene – How to Serialize an Object ?

Published on March 13, 2016 by abundantcode

Filed under Json.NET

Last modified February 13, 2016

Print this page

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

This article have been viewed 6332 times

One of the ways to serialize an object to JSON string in Oxygene # in the Json.NET is using the SerializeObject method defined in the JsonConvert method.

The JsonConvert class provides an easy to use wrapper class and method over the JsonSerializer.

How to Serialize an Object in Oxygene using Json.NET ?

Below is the sample code snippet demonstrating the usage of JsonConvert.SerializeObject method in C# for serialization. The SerializeObject method has 7 overloads option.

class method SerializeObject(value: Object): String;
begin
end;

class method SerializeObject(value: Object; converters: array of JsonConverter): String;
begin
end;

class method SerializeObject(value: Object; settings: JsonSerializerSettings): String;
begin
end;

class method SerializeObject(value: Object; formatting: Formatting): String;
begin
end;

class method SerializeObject(value: Object; formatting: Formatting; converters: array of JsonConverter): String;
begin
end;

class method SerializeObject(value: Object; &type: &Type; settings: JsonSerializerSettings): String;
begin
end;

class method SerializeObject(value: Object; formatting: Formatting; settings: JsonSerializerSettings): String;
begin
end;

class method SerializeObject(value: Object; &type: &Type; formatting: Formatting; settings: JsonSerializerSettings): String;
begin
end;

The below code uses the parameterized method which uses the Formatting as the second parameter.

namespace ACConsoleOxygene;
interface

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

type
  Program = class
  private
    class method Main(args: array of 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 emp: Employee := new Employee();
  emp.Name := 'Abundantcode';
  emp.IsPermanent := true;
  emp.Departments := new List<String>();
  emp.Departments.Add('Technology');
  emp.Departments.Add('Product Engineering');
  //  Serializing the employee object to Json string
  var jsonTxt: String := JsonConvert.SerializeObject(emp, Formatting.Indented);
  Console.WriteLine(jsonTxt);
  Console.ReadLine();
end;



end.

The output of the above code is

{
  "Name": "Abundantcode",
  "IsPermanent": true,
  "Departments": [
    "Technology",
    "Product Engineering"
  ]
}

2 Comments

  1. Json.NET & C# – How to Deserialize an Object ? | Abundant Code
    February 14, 2016 - 12:12 am

  2. Json.NET & VB.NET – How to Serialize an Object ? | Abundant Code
    April 13, 2016 - 5:03 pm

Leave a Comment


x

You might be Interested in these Posts

Video - Angular Router - Brian Ford

Video - Angular Router - Brian Ford http://www.youtube.com/watch?v=sfV0ApyN5fM ...

Video - An Angular2 Todo App: First look at App Development in Angular2

In this screencast , David East , one of the core contributor of the Angular provides a glimpse of A...

Video - Getting started with Angular 2

In this screencast , Alex Eagle , developer on the Angular team talks about Angular 2 developer prev...