djson 序列化、反序列化,复杂json例子,包含全部类型

demo下载

https://files.cnblogs.com/files/blogs/837466/djson-std.zip?t=1740055132&download=true

demo介绍

共三个模型 school、student、address 包含 json的 各种类型,模型是我的模型工具自动生成的;

school模型

image

unit test.School;

interface

uses

ddj.exception.FieldNullException,
System.Generics.Collections,
test.EnumType,
test.Address,
test.Student;

type

/// <summary>
/// 学校模型
/// </summary>
TSchool = class

private

  /// <summary>
  /// 学校名
  /// </summary>
  str: PString;

  /// <summary>
  /// int8
  /// </summary>
  i8: PShortInt;

  /// <summary>
  /// int16
  /// </summary>
  i16: PSmallInt;

  /// <summary>
  /// int32
  /// </summary>
  i32: PInteger;

  /// <summary>
  /// int64
  /// </summary>
  i64: PInt64;

  /// <summary>
  /// 单精度浮点
  /// </summary>
  flt32: PSingle;

  /// <summary>
  /// 双精度浮点
  /// </summary>
  flt64: PDouble;

  /// <summary>
  /// boolean
  /// </summary>
  boolean1: PBoolean;

  /// <summary>
  /// 时间
  /// </summary>
  time1: PDateTime;

  /// <summary>
  /// 枚举
  /// </summary>
  enum1: PEnumType;

  /// <summary>
  /// 嵌入对象
  /// </summary>
  obj1: TAddress;

  /// <summary>
  /// 嵌入对象列表
  /// </summary>
  objs: TObjectList<TStudent>;

  /// <summary>
  /// 方便测试跳过,以上
  /// </summary>
  str2: PString;

public

  destructor Destroy; override;

  function strIsNull: Boolean;
  function getStr: string;
  procedure setStr(value: string);

  function i8IsNull: Boolean;
  function getI8: ShortInt;
  procedure setI8(value: ShortInt);

  function i16IsNull: Boolean;
  function getI16: SmallInt;
  procedure setI16(value: SmallInt);

  function i32IsNull: Boolean;
  function getI32: Integer;
  procedure setI32(value: Integer);

  function i64IsNull: Boolean;
  function getI64: Int64;
  procedure setI64(value: Int64);

  function flt32IsNull: Boolean;
  function getFlt32: Single;
  procedure setFlt32(value: Single);

  function flt64IsNull: Boolean;
  function getFlt64: Single;
  procedure setFlt64(value: Single);

  function boolean1IsNull: Boolean;
  function getBoolean1: Boolean;
  procedure setBoolean1(value: Boolean);

  function time1IsNull: Boolean;
  function getTime1: TDateTime;
  procedure setTime1(value: TDateTime);

  function enum1IsNull: Boolean;
  function getEnum1: TEnumType;
  procedure setEnum1(value: TEnumType);

  function obj1IsNull: Boolean;
  function getObj1: TAddress;
  procedure setObj1(value: TAddress);

  function objsIsNull: Boolean;
  function getObjs: TObjectList<TStudent>;
  procedure setObjs(value: TObjectList<TStudent>);

  function str2IsNull: Boolean;
  function getStr2: string;
  procedure setStr2(value: string);

end;

implementation

destructor TSchool.Destroy;
begin
  if str <> nil then
  begin
    Dispose(str);
  end;

  if i8 <> nil then
  begin
    Dispose(i8);
  end;

  if i16 <> nil then
  begin
    Dispose(i16);
  end;

  if i32 <> nil then
  begin
    Dispose(i32);
  end;

  if i64 <> nil then
  begin
    Dispose(i64);
  end;

  if flt32 <> nil then
  begin
    Dispose(flt32);
  end;

  if flt64 <> nil then
  begin
    Dispose(flt64);
  end;

  if boolean1 <> nil then
  begin
    Dispose(boolean1);
  end;

  if time1 <> nil then
  begin
    Dispose(time1);
  end;

  if enum1 <> nil then
  begin
    Dispose(enum1);
  end;

  if obj1 <> nil then
  begin
    obj1.Free;
  end;

  if objs <> nil then
  begin
    objs.Free;
  end;

  if str2 <> nil then
  begin
    Dispose(str2);
  end;

  inherited;
end;

function TSchool.strIsNull: Boolean;
begin
  Result := (str = nil);
end;

function TSchool.getStr: string;
begin
  if str = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := str^;
end;

procedure TSchool.setStr(value: string);
begin
  if str = nil then
  begin
    New(str);
  end;
  str^ := value;
end;

function TSchool.i8IsNull: Boolean;
begin
  Result := (i8 = nil);
end;

function TSchool.getI8: ShortInt;
begin
  if i8 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := i8^;
end;

procedure TSchool.setI8(value: ShortInt);
begin
  if i8 = nil then
  begin
    New(i8);
  end;
  i8^ := value;
end;

function TSchool.i16IsNull: Boolean;
begin
  Result := (i16 = nil);
end;

function TSchool.getI16: SmallInt;
begin
  if i16 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := i16^;
end;

procedure TSchool.setI16(value: SmallInt);
begin
  if i16 = nil then
  begin
    New(i16);
  end;
  i16^ := value;
end;

function TSchool.i32IsNull: Boolean;
begin
  Result := (i32 = nil);
end;

function TSchool.getI32: Integer;
begin
  if i32 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := i32^;
end;

procedure TSchool.setI32(value: Integer);
begin
  if i32 = nil then
  begin
    New(i32);
  end;
  i32^ := value;
end;

function TSchool.i64IsNull: Boolean;
begin
  Result := (i64 = nil);
end;

function TSchool.getI64: Int64;
begin
  if i64 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := i64^;
end;

procedure TSchool.setI64(value: Int64);
begin
  if i64 = nil then
  begin
    New(i64);
  end;
  i64^ := value;
end;

function TSchool.flt32IsNull: Boolean;
begin
  Result := (flt32 = nil);
end;

function TSchool.getFlt32: Single;
begin
  if flt32 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := flt32^;
end;

procedure TSchool.setFlt32(value: Single);
begin
  if flt32 = nil then
  begin
    New(flt32);
  end;
  flt32^ := value;
end;

function TSchool.flt64IsNull: Boolean;
begin
  Result := (flt64 = nil);
end;

function TSchool.getFlt64: Single;
begin
  if flt64 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := flt64^;
end;

procedure TSchool.setFlt64(value: Single);
begin
  if flt64 = nil then
  begin
    New(flt64);
  end;
  flt64^ := value;
end;

function TSchool.boolean1IsNull: Boolean;
begin
  Result := (boolean1 = nil);
end;

function TSchool.getBoolean1: Boolean;
begin
  if boolean1 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := boolean1^;
end;

procedure TSchool.setBoolean1(value: Boolean);
begin
  if boolean1 = nil then
  begin
    New(boolean1);
  end;
  boolean1^ := value;
end;

function TSchool.time1IsNull: Boolean;
begin
  Result := (time1 = nil);
end;

function TSchool.getTime1: TDateTime;
begin
  if time1 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := time1^;
end;

procedure TSchool.setTime1(value: TDateTime);
begin
  if time1 = nil then
  begin
    New(time1);
  end;
  time1^ := value;
end;

function TSchool.enum1IsNull: Boolean;
begin
  Result := (enum1 = nil);
end;

function TSchool.getEnum1: TEnumType;
begin
  if enum1 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := enum1^;
end;

procedure TSchool.setEnum1(value: TEnumType);
begin
  if enum1 = nil then
  begin
    New(enum1);
  end;
  enum1^ := value;
end;

function TSchool.obj1IsNull: Boolean;
begin
  Result := (obj1 = nil);
end;

function TSchool.getObj1: TAddress;
begin
  Result := obj1;
end;

procedure TSchool.setObj1(value: TAddress);
begin
  obj1 := value;
end;

function TSchool.objsIsNull: Boolean;
begin
  Result := (objs = nil);
end;

function TSchool.getObjs: TObjectList<TStudent>;
begin
  Result := objs;
end;

procedure TSchool.setObjs(value: TObjectList<TStudent>);
begin
  objs := value;
end;

function TSchool.str2IsNull: Boolean;
begin
  Result := (str2 = nil);
end;

function TSchool.getStr2: string;
begin
  if str2 = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := str2^;
end;

procedure TSchool.setStr2(value: string);
begin
  if str2 = nil then
  begin
    New(str2);
  end;
  str2^ := value;
end;

end.

student模型

image

unit test.Student;

interface

uses

ddj.exception.FieldNullException,
System.Generics.Collections,
test.EnumType,
test.Address;

type

/// <summary>
/// 学生模型
/// </summary>
TStudent = class

private

  /// <summary>
  /// 姓名
  /// </summary>
  name: PString;

  /// <summary>
  /// 再嵌入地址
  /// </summary>
  address: TAddress;

  /// <summary>
  /// str列表
  /// </summary>
  strs: TList<string>;

  /// <summary>
  /// 数值列表
  /// </summary>
  int8s: TList<ShortInt>;

  int16s: TList<SmallInt>;

  int32s: TList<Integer>;

  int64s: TList<Int64>;

  flt32s: TList<Single>;

  flt64s: TList<Double>;

  /// <summary>
  /// 布尔列表
  /// </summary>
  bools: TList<Boolean>;

  /// <summary>
  /// 枚举列表
  /// </summary>
  enums: TList<TEnumType>;

public

  destructor Destroy; override;

  function nameIsNull: Boolean;
  function getName: string;
  procedure setName(value: string);

  function addressIsNull: Boolean;
  function getAddress: TAddress;
  procedure setAddress(value: TAddress);

  function strsIsNull: Boolean;
  function getStrs: TList<string>;
  procedure setStrs(value: TList<string>);

  function int8sIsNull: Boolean;
  function getInt8s: TList<ShortInt>;
  procedure setInt8s(value: TList<ShortInt>);

  function int16sIsNull: Boolean;
  function getInt16s: TList<SmallInt>;
  procedure setInt16s(value: TList<SmallInt>);

  function int32sIsNull: Boolean;
  function getInt32s: TList<Integer>;
  procedure setInt32s(value: TList<Integer>);

  function int64sIsNull: Boolean;
  function getInt64s: TList<Int64>;
  procedure setInt64s(value: TList<Int64>);

  function flt32sIsNull: Boolean;
  function getFlt32s: TList<Single>;
  procedure setFlt32s(value: TList<Single>);

  function flt64sIsNull: Boolean;
  function getFlt64s: TList<Double>;
  procedure setFlt64s(value: TList<Double>);

  function boolsIsNull: Boolean;
  function getBools: TList<Boolean>;
  procedure setBools(value: TList<Boolean>);

  function enumsIsNull: Boolean;
  function getEnums: TList<TEnumType>;
  procedure setEnums(value: TList<TEnumType>);

end;

implementation

destructor TStudent.Destroy;
begin
  if name <> nil then
  begin
    Dispose(name);
  end;

  if address <> nil then
  begin
    address.Free;
  end;

  if strs <> nil then
  begin
    strs.Free;
  end;

  if int8s <> nil then
  begin
    int8s.Free;
  end;

  if int16s <> nil then
  begin
    int16s.Free;
  end;

  if int32s <> nil then
  begin
    int32s.Free;
  end;

  if int64s <> nil then
  begin
    int64s.Free;
  end;

  if flt32s <> nil then
  begin
    flt32s.Free;
  end;

  if flt64s <> nil then
  begin
    flt64s.Free;
  end;

  if bools <> nil then
  begin
    bools.Free;
  end;

  if enums <> nil then
  begin
    enums.Free;
  end;

  inherited;
end;

function TStudent.nameIsNull: Boolean;
begin
  Result := (name = nil);
end;

function TStudent.getName: string;
begin
  if name = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := name^;
end;

procedure TStudent.setName(value: string);
begin
  if name = nil then
  begin
    New(name);
  end;
  name^ := value;
end;

function TStudent.addressIsNull: Boolean;
begin
  Result := (address = nil);
end;

function TStudent.getAddress: TAddress;
begin
  Result := address;
end;

procedure TStudent.setAddress(value: TAddress);
begin
  address := value;
end;

function TStudent.strsIsNull: Boolean;
begin
  Result := (strs = nil);
end;

function TStudent.getStrs: TList<string>;
begin
  Result := strs;
end;

procedure TStudent.setStrs(value: TList<string>);
begin
  strs := value;
end;

function TStudent.int8sIsNull: Boolean;
begin
  Result := (int8s = nil);
end;

function TStudent.getInt8s: TList<ShortInt>;
begin
  Result := int8s;
end;

procedure TStudent.setInt8s(value: TList<ShortInt>);
begin
  int8s := value;
end;

function TStudent.int16sIsNull: Boolean;
begin
  Result := (int16s = nil);
end;

function TStudent.getInt16s: TList<SmallInt>;
begin
  Result := int16s;
end;

procedure TStudent.setInt16s(value: TList<SmallInt>);
begin
  int16s := value;
end;

function TStudent.int32sIsNull: Boolean;
begin
  Result := (int32s = nil);
end;

function TStudent.getInt32s: TList<Integer>;
begin
  Result := int32s;
end;

procedure TStudent.setInt32s(value: TList<Integer>);
begin
  int32s := value;
end;

function TStudent.int64sIsNull: Boolean;
begin
  Result := (int64s = nil);
end;

function TStudent.getInt64s: TList<Int64>;
begin
  Result := int64s;
end;

procedure TStudent.setInt64s(value: TList<Int64>);
begin
  int64s := value;
end;

function TStudent.flt32sIsNull: Boolean;
begin
  Result := (flt32s = nil);
end;

function TStudent.getFlt32s: TList<Single>;
begin
  Result := flt32s;
end;

procedure TStudent.setFlt32s(value: TList<Single>);
begin
  flt32s := value;
end;

function TStudent.flt64sIsNull: Boolean;
begin
  Result := (flt64s = nil);
end;

function TStudent.getFlt64s: TList<Double>;
begin
  Result := flt64s;
end;

procedure TStudent.setFlt64s(value: TList<Double>);
begin
  flt64s := value;
end;

function TStudent.boolsIsNull: Boolean;
begin
  Result := (bools = nil);
end;

function TStudent.getBools: TList<Boolean>;
begin
  Result := bools;
end;

procedure TStudent.setBools(value: TList<Boolean>);
begin
  bools := value;
end;

function TStudent.enumsIsNull: Boolean;
begin
  Result := (enums = nil);
end;

function TStudent.getEnums: TList<TEnumType>;
begin
  Result := enums;
end;

procedure TStudent.setEnums(value: TList<TEnumType>);
begin
  enums := value;
end;

end.

address模型

image

unit test.Address;

interface

uses

ddj.exception.FieldNullException;

type

/// <summary>
/// 地址模型
/// </summary>
TAddress = class

private

  /// <summary>
  /// 省份
  /// </summary>
  province: PString;

  /// <summary>
  /// 城市
  /// </summary>
  city: PString;

public

  destructor Destroy; override;

  function provinceIsNull: Boolean;
  function getProvince: string;
  procedure setProvince(value: string);

  function cityIsNull: Boolean;
  function getCity: string;
  procedure setCity(value: string);

end;

implementation

destructor TAddress.Destroy;
begin
  if province <> nil then
  begin
    Dispose(province);
  end;

  if city <> nil then
  begin
    Dispose(city);
  end;

  inherited;
end;

function TAddress.provinceIsNull: Boolean;
begin
  Result := (province = nil);
end;

function TAddress.getProvince: string;
begin
  if province = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := province^;
end;

procedure TAddress.setProvince(value: string);
begin
  if province = nil then
  begin
    New(province);
  end;
  province^ := value;
end;

function TAddress.cityIsNull: Boolean;
begin
  Result := (city = nil);
end;

function TAddress.getCity: string;
begin
  if city = nil then
  begin
    raise EFieldNullException.Create;
  end;
  Result := city^;
end;

procedure TAddress.setCity(value: string);
begin
  if city = nil then
  begin
    New(city);
  end;
  city^ := value;
end;

end.

测试用的枚举

unit test.EnumType;

interface

type

/// <summary>
/// 测试枚举
/// </summary>
TEnumType = (

  /// <summary>
  /// ABC
  /// </summary>
  ABC,

  /// <summary>
  /// CDE
  /// </summary>
  CDE,

  /// <summary>
  /// HIG
  /// </summary>
  HIG,

  /// <summary>
  /// DDD
  /// </summary>
  DDD

  );

/// <summary>
/// 枚举指针
/// </summary>
PEnumType = ^TEnumType;

implementation

end.

测试的 main窗体 代码

image

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, System.Generics.Collections;

type
  TFormMain = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

uses test.School, test.Student, test.Address, test.EnumType, djson;

procedure TFormMain.Button1Click(Sender: TObject);
begin
  var dj := TDJson.Create;
  var school := TSchool.Create;
  var school2 := TSchool.Create;
  try
    school.setStr('希望小学');
    school.setI8(111);
    school.setI16(10000);
    school.setI32(99999);
    school.setI64(8888888888);
    school.setFlt32(3.1415927);
    school.setFlt64(3.141592653589793);
    school.setBoolean1(True);
    school.setTime1(Now);
    school.setEnum1(TEnumType.ABC);
    school.setObj1(TAddress.Create);
    with school.getObj1 do
    begin
      setProvince('中国');
      setCity('上海市');
    end;
    school.setObjs(TObjectList<TStudent>.Create);
    for var i := 0 to 3 do
    begin
      school.getObjs.Add(TStudent.Create);
      school.getObjs.Items[i].setName('欧阳疯 - ' + i.toString);
      school.getObjs.Items[i].setAddress(TAddress.Create);
      school.getObjs.Items[i].getAddress.setProvince('中国 - ' + i.toString);
      school.getObjs.Items[i].getAddress.setCity('北京 - ' + i.toString);
      school.getObjs.Items[i].setStrs(TList<string>.Create);
      school.getObjs.Items[i].getStrs.Add('字符串1' + sLineBreak + ' 含有回车 - ' + i.toString);
      school.getObjs.Items[i].getStrs.Add('带特殊符号"2 - ' + i.toString);
      //int8s
      school.getObjs.Items[i].setInt8s(TList<ShortInt>.Create);
      school.getObjs.Items[i].getInt8s.Add(1);
      school.getObjs.Items[i].getInt8s.Add(10);
      school.getObjs.Items[i].getInt8s.Add(100);
      //int16s
      school.getObjs.Items[i].setInt16s(TList<SmallInt>.Create);
      school.getObjs.Items[i].getInt16s.Add(1000);
      school.getObjs.Items[i].getInt16s.Add(10001);
      school.getObjs.Items[i].getInt16s.Add(10002);
      //int32s
      school.getObjs.Items[i].setInt32s(TList<Integer>.Create);
      school.getObjs.Items[i].getInt32s.Add(1000008);
      school.getObjs.Items[i].getInt32s.Add(1000009);
      school.getObjs.Items[i].getInt32s.Add(1000007);
      //int64s
      school.getObjs.Items[i].setInt64s(TList<Int64>.Create);
      school.getObjs.Items[i].getInt64s.Add(100000800200);
      school.getObjs.Items[i].getInt64s.Add(100000800201);
      school.getObjs.Items[i].getInt64s.Add(100000800202);
      //float32s
      school.getObjs.Items[i].setFlt32s(TList<Single>.Create);
      school.getObjs.Items[i].getFlt32s.Add(3.1415927);
      school.getObjs.Items[i].getFlt32s.Add(3.1415917);
      school.getObjs.Items[i].getFlt32s.Add(3.1415907);
      //float64s
      school.getObjs.Items[i].setFlt64s(TList<Double>.Create);
      school.getObjs.Items[i].getFlt64s.Add(3.141592653589793);
      school.getObjs.Items[i].getFlt64s.Add(3.141592653589794);
      school.getObjs.Items[i].getFlt64s.Add(3.141592653589795);
      //booleans
      school.getObjs.Items[i].setBools(TList<Boolean>.Create);
      school.getObjs.Items[i].getBools.Add(True);
      school.getObjs.Items[i].getBools.Add(False);
      //enums
      school.getObjs.Items[i].setEnums(TList<TEnumType>.Create); //枚举列表
      school.getObjs.Items[i].getEnums.Add(TEnumType.CDE);
      school.getObjs.Items[i].getEnums.Add(TEnumType.DDD);
    end;
    school.setStr2('需要转义"的/各种字符');

    //将 obj 序列化 json
    var oldJson := dj.objToJson(school);
    Memo1.Lines.Add('school序列化:' + sLineBreak);
    Memo1.Lines.Add(oldJson);
    Memo1.Lines.Add(sLineBreak + '------------------' + sLineBreak);

    //json 反序列化 school2,然后再把 school2 序列化成 json
    dj.jsonToObj(oldJson, school2);
    var newJson := dj.objToJson(school2);
    Memo1.Lines.Add('school2序列化:' + sLineBreak);
    Memo1.Lines.Add(newJson);

    //测试是否相等
    Memo1.Lines.Add(sLineBreak);
    if newJson.Equals(oldJson) then
    begin
      Memo1.Lines.Add('======= 结果一致,完全没有问题 ========');
    end else begin
      Memo1.Lines.Add('======= 结果不一致,请核查! ========');
    end;
  finally
    dj.Free;
    school.Free;
    school2.Free;
  end;
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
end;

end.
posted @   殴阳疯  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示