unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type 
  TForm1 =class (TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click (Sender: TObject);
    procedure Button2Click (Sender: TObject);
    procedure Button3Click (Sender: TObject);
  end ;

var
  Form1: TForm1;

implementation

{$ R * .dfm}

uses SuperObject;

// Merge merge
procedure TForm1.Button1Click (Sender: TObject);
var
  jo, jt: ISuperObject;
begin 
  jo: = SO ('(X: 1)');
  jt: = SO ('(Y: 2}');
  jo.Merge (jt);
  ShowMessage (jo.AsJSon);   // {"Y": 2, "X": 1}
//
  jo: = SO ('{X: 1, Y: 2}');
  jt: = SO ('(Y: 3}');
  jo.Merge (jt);
  ShowMessage (jo.AsJSon);   // {"Y": 3, "X": 1}
//
  jo: = SO ('{X: 1, Y: 2}');
  jt: = SO ('{Y: [5,6,7]}');
  jo.Merge (jt);
  ShowMessage (jo.AsJSon);   // {"Y": [5,6,7], "X": 1}
//
  jo: = SO ('(A: {x: 1, y: 2})');
  jt: = SO ('(A: {m: 3, n: 4})');
  jo.Merge (jt, True);
  ShowMessage (jo.AsJSon);   // {"A": {"y": 2, "x": 1, "n": 4, "m": 3}}
end ;

// Clone clone
procedure TForm1.Button2Click (Sender: TObject);
var
  jo, jt: ISuperObject;
begin 
  jo: = SO ('(A: {x: 1, y: 2, z: 3})');
  jt: = jo.Clone;

  jt.I ['Ax']: = 7;
  jt.I ['Ay']: = 8;
  jt.I ['Az']: = 9;

  ShowMessage (jo.AsJSon); // {"A": {"z": 3, "y": 2, "x": 1}}
  ShowMessage (jt.AsJSon); // {"A": {"z": 9, "y": 8, "x": 7}}

  // if not clone
  jo: = SO ('(A: {x: 1, y: 2, z: 3})');
  jt: = jo;

  jt.Delete ('Ax');
  jt.Delete ('Ay');

  ShowMessage (jo.AsJSon); // {"A": {"z": 3}}
  ShowMessage (jt.AsJSon); // {"A": {"z": 3}}
end ;

// ForcePath points to a child object
procedure TForm1.Button3Click (Sender: TObject);
var
  jo, jt, ja: ISuperObject;
begin 
  jo: = SO ('{A: {x: 1, y: 2}, B: []}');

  {Can point to a child object through ForcePath; when the child object is modified, the original object will of course also be modified, which is originally the same}
  jt: = jo.ForcePath ('A');
  jt.I ['x']: = 9;
  ShowMessage (jo.AsJSon); // {"B": [], "A": {"y": 2, "x": 9}}

  {The original object is modified, of course, the child objects will also be modified}
  ja: = jo.ForcePath ('B', stArray);
  jo.A ['B'] [0]: = SO ('m');
  jo.A ['B'] [1]: = SO ('n');
  ShowMessage (ja.AsJSon); // ["m", "n"]


  {ISuperObject.ForcePath is similar to ISuperObject.O [], except that the latter is readable and writable}
  jo: = SO ('{A: {x: 1, y: 2}, B: []}');
  jt: = jo.O ['A']; // Or abbreviated as: jt: = jo ['A'];
  jt.I ['x']: = 8;
  ShowMessage (jo.AsJSon); // {"B": [], "A": {"y": 2, "x": 8}}
end ;

end .