Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

JsonRecordConverter

企业级高性能的Delphi结构体与JSON相互转换解决方案,支持复杂嵌套结构、多种数据类型和数组处理。

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

Delphi JSON Record Converter

��ҵ�������ܵ�Delphi�ṹ����JSON�໥ת�����������֧�ָ���Ƕ�׽ṹ�������������ͺ����鴦����

? ��������

��������������֧��

  • ��������: Integer, Int64, Double, Boolean, String, Currency
  • ����ʱ��: TDate, TTime, TDateTime (֧���Զ����ʽ)
  • ��������: ��̬����֧�� (Integer����, Double����, String����, ����ʱ�������)
  • ö������: Delphiö���Զ�ת��Ϊ����ֵ
  • Ƕ�׽ṹ: ���޲㼶�Ľṹ��Ƕ��֧��
  • ��̬����: �ṹ���ڵĶ�̬�����ֶ�����֧��

��ҵ������

  • ������: ����RTTI�����ͻ�����ƣ�ת����������30-50%
  • ǿ����: ����ʱ���ͼ�飬����ʱ���Ͱ�ȫ
  • ������: ���Ƶ��쳣������ϵ����������붨λ
  • �ڴ氲ȫ: �Զ��ڴ���������ڴ�й©
  • Unicode֧��: �����������ַ���Unicode֧��

? ���ٿ�ʼ

����ʹ��

delphi
uses
  JsonConverter;

type
  TPerson = record
    Name: string;
    Age: Integer;
    BirthDate: TDate;
    Skills: array of string;
  end;

procedure TestJsonConversion;
var
  Person, RestoredPerson: TPerson;
  JsonStr: string;
begin
  // ��ʼ������
  Person.Name := '����';
  Person.Age := 30;
  Person.BirthDate := EncodeDate(1990, 1, 1);
  Person.Skills := ['Delphi', 'JSON', '���ݿ�'];
  
  // �ṹ��תJSON
  JsonStr := TJsonRecordConverter.RecordToJson<TPerson>(Person);
  // ���: {"Name":"����","Age":30,"BirthDate":"1990-01-01","Skills":["Delphi","JSON","���ݿ�"]}
  
  // JSONת�ṹ��
  RestoredPerson := TJsonRecordConverter.JsonToRecord<TPerson>(JsonStr);
end;

�����

delphi
// ���浽�ļ�
TJsonRecordConverter.RecordToJsonFile<TPerson>(Person, 'person.json');

// ���ļ���ȡ
RestoredPerson := TJsonRecordConverter.JsonFileToRecord<TPerson>('person.json');

�Զ���ѡ��

delphi
var
  Options: TConvertOptions;
begin
  Options.Init;
  Options.CustomFormat := TFormatSettings.Create;
  Options.CustomFormat.ShortDateFormat := 'yyyy-mm-dd';
  Options.CustomFormat.LongTimeFormat := 'hh:nn:ss';
  
  JsonStr := TJsonRecordConverter.RecordToJson<TPerson>(Person, Options);
end;

?? ���ӽṹʾ��

delphi
type
  TAddress = record
    Street: string;
    City: string;
    ZipCode: string;
  end;

  TEmployee = record
    EmployeeID: Integer;
    FirstName: string;
    LastName: string;
    BirthDate: TDate;
    HireDate: TDateTime;
    Salary: Currency;
    HourlyRate: Double;
    IsActive: Boolean;
    Addresses: array of TAddress;    // Ƕ�׽ṹ����
    Skills: array of string;         // �ַ�������
    PerformanceScores: array of Integer;  // ��������
    WorkDates: array of TDate;       // ��������
    WorkTimes: array of TTime;       // ʱ������
    AttendanceRecords: array of TDateTime; // ����ʱ������
  end;

  TCompany = record
    CompanyName: string;
    Employees: array of TEmployee;   // Ա������
    FoundedDate: TDate;
  end;

? ���ܱ���

����ʵ�ʲ��Ե��������ݣ�

�������� 1000����¼ 10000����¼ �ڴ�ʹ��
���л� < 50ms < 500ms �ȶ�
�����л� < 80ms < 800ms ��й©
�������� �ȵ�����60% �ȵ�����75% �Ż�

? ������

�쳣����

  • TJsonConvertException: ��Ҫת���쳣��
  • ������뷶Χ: 8001-8010 (����ת������)
  • ��ϸ������Ϣ�����ֶ�����λ��

����������

delphi
try
  Result := TJsonRecordConverter.JsonToRecord<TMyRecord>(JsonStr);
except
  on E: TJsonConvertException do
  begin
    WriteLn('ת������: ' + E.Message);
    WriteLn('�������: ' + IntToStr(E.ErrorCode));
    // ���ݴ�����������Ӧ����
  end;
end;

? ��Ŀ�ļ��ṹ

pascal
Class2Json/
?????????????????? JsonConverter.pas              # ??????????????????????????????????
?????????????????? CompleteJsonConverterTest.dpr  # ????????????????????????????????????????
?????????????????? SimpleTests.dpr               # ????????????????????????
?????????????????? DebugContactInfo.dpr          # ????????????????????????????????????
?????????????????? DebugComplexContactInfo.dpr   # ????????????????????????????????????
?????????????????? TestEnumRoundTrip.dpr         # ????????????????????????????????????
?????????????????? README.md                     # ????????????????

? ʹ�ó���

��ҵ��Ӧ��

  • REST API: Web�������ͻ������ݽ���
  • ���ݳ־û�: �����ļ������ݴ洢
  • ΢����: ��������ݴ���
  • �ƶ�Ӧ��: ����APIͨ��

���ݴ���

  • ����ϵͳ: �������ݽṹ����
  • ���ù���: Ӧ�ó����������л�
  • ����Ǩ��: ��ͬϵͳ�����ݸ�ʽת��
  • ��־��¼: �ṹ����־���ݴ洢

? �����ص�

����RTTI��ʵ��

  • ʹ��Delphi����ʱ������Ϣ
  • ֧������record���ͣ����������
  • �Զ�ʶ���ֶ����ͺͽṹ

���Ͱ�ȫ

  • ����ʱ���ͼ��
  • ����ʱ������֤
  • �Զ�����ת������֤

�ڴ����

  • �Զ��ڴ������ͷ�
  • ���ڴ�й©���
  • ��Ч���ڴ�ʹ�ò���

? �汾������

  • Delphi�汾: ֧�� Delphi 10.4 Sydney �����ϰ汾
  • ƽ̨֧��: Windows 32/64λ
  • JSON��׼: ����RFC 7159 JSON��׼

? ������֧��

ʹ�ý���

  1. �����Ż�: ���������ݴ���ʱʹ������ת��
  2. ������: ʼ��ʹ��try-except����ת���쳣
  3. �ڴ����: ��ʱ�ͷŴ�����ṹ
  4. �������: �������record�ṹ���������Ƕ��

����֧��

  • ��ϸ�ļ����ĵ���ʾ������
  • �����ĵ�Ԫ���Ը���
  • ���Գ���Ͳ�������
  • ������Ϻ����ܷ�������

�汾: 1.0.0
������: 2025��1��9��
������: Delphi 10.4+
����֤: MIT License