首页 > 其他分享 >delphi基于数据模型(data-model)JSON序列

delphi基于数据模型(data-model)JSON序列

时间:2024-03-31 19:33:07浏览次数:89  
标签:function end delphi value class JSON const unjson 数据模型

delphi基于数据模型(data-model)JSON序列

需要DELPHI10.2以上版本才能支持。

1)实现JSON序列/还原的泛型模板

unit serialize;
/// <author>cxg 2024-1-11</author>

interface

uses
  system.Classes,
  System.SysUtils, System.JSON.Serializers;

type
  TSerial<T: record> = class
  public
    //还原
    class function unjson(const value: string): T; overload;
    class function unjson(const value: TStream): T; overload;
    class function unjson(const value: TBytes): T; overload;
    //序列
    class function json(const aRecord: T): string; overload;
  end;

function Stream2Raw(const aStream: TStream): RawByteString;
function bytes2raw(const aBytes: TBytes): RawByteString;

implementation

function bytes2raw(const aBytes: TBytes): RawByteString;
begin
  var len: Integer := Length(aBytes);
  SetLength(Result, len);
  Move(aBytes[0], Result[1], len);
end;

function Stream2Raw(const aStream: TStream): RawByteString;
begin
  SetLength(Result, aStream.Size);
  aStream.Position := 0;
  aStream.Read(Result[1], aStream.Size);
end;

class function TSerial<T>.json(const aRecord: T): string;
begin
  var s: TJsonSerializer := TJsonSerializer.Create;
  try
    Result := s.Serialize<T>(aRecord);
  finally
    s.Free;
  end;
end;

class function TSerial<T>.unjson(const value: string): T;
begin
  var s: TJsonSerializer := TJsonSerializer.Create;
  try
    Result := s.Deserialize<T>(value);
  finally
    s.free;
  end;
end;

class function TSerial<T>.unjson(const value: TStream): T;
begin
  Result := Self.unjson(UTF8Decode(Stream2Raw(value)));
end;

class function TSerial<T>.unjson(const value: TBytes): T;
begin
  Result := Self.unjson(UTF8Decode(bytes2raw(value)));
end;

end.

 2)定义“计量单位”的数据模型(data-model) 

unit danwei.model;
/// <author>cxg 2023-9-13</author>
interface

type      //定义 数据模型(data-model)
  Tdanwei = record
    unitid: string;
    unitname: string;
  end;

implementation

end.

  3)调用示例

table := serialize.TSerial<TTable<T>>.unjson(TStream(req.Body));  //还原json string--->record

res.Send(TSerial<TTable<T>>.json(table));   //JSON序列 send json string

  

标签:function,end,delphi,value,class,JSON,const,unjson,数据模型
From: https://www.cnblogs.com/hnxxcxg/p/18107144

相关文章

  • Json文件格式及Cpp解析
    JSON(JavaScriptObjectNotation)用于存储和传输数据,通常用于服务器-->Web端的数据传输JSON示例:{"employees":[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastN......
  • delphi ORM和泛型模板
    delphiORM和泛型模板实现CRUD1)定义数据模型(data-model)数据模型是ORM数据序列/还原所必需的。TTable<T:record>=record//1个表rows:TArray<T>;//表的行end;TTable2<T,T2:record>=record//2个表table1:TTable<T>;......
  • Delphi模式编程
    文章目录Delphi模式编程涉及以下几个关键方面:**设计模式的应用****Delphi特性的利用****实际开发中的实践**Delphi模式编程的实例Delphi模式编程是指在使用Delphi这一集成开发环境(IDE)和ObjectPascal语言进行软件开发时,采用设计模式(DesignPatterns)来解决常见编程问......
  • JSON Editor 中文文档
    JSONEditorJSONEditor根据定义的JSONSchema生成了一个Html表单来对JSON进行编辑。它完整支持JSONSchema的版本3和版本4,并且它集成了一些流行的CSS框架,例如bootstrap,foundation,andjQueryUI等。JSONEditor生成的编辑器支持输入框、下拉框、等几乎所有的html5输入......
  • 深入解析实时数仓Doris:三大数据模型详解
    码到三十五:个人主页心中有诗画,指尖舞代码,目光览世界,步履越千山,人间尽值得!目录一、基本概念二、Aggregate模型导入数据聚合三、Unique模型四、Duplicate模型五、数据模型的选择建议一、基本概念在Doris中,数据以表(Table)的形式进行逻辑上的......
  • 多层JSON字符串对象的差异化比较
    importcn.hutool.core.util.ObjUtil;importcn.hutool.core.util.StrUtil;importcom.fasterxml.jackson.databind.JsonNode;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.fasterxml.jackson.databind.node.ObjectNode;importlombok.Data;importl......
  • 探索多种数据格式:JSON、YAML、XML、CSV等数据格式详解与比较
    1.数据格式介绍数据格式是用于组织和存储数据的规范化结构,不同的数据格式适用于不同的场景。常见的数据格式包括JSON、YAML、XML、CSV等。数据可视化|一个覆盖广泛主题工具的高效在线平台(amd794.com)https://amd794.com/jsonformat2.JSON(JavaScriptObjectNotation)......
  • Fastjson反序列化分析
    依赖先研究1.2.24版本的,版本高了就有waf了,不过也能绕,高版本以后再说<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.24</version></dependency><dependency><groupId&g......
  • python-json数据、pyecharts的入门使用(折线图)
    目录1. json数据格式 1.1 json.dumps()1.2 json.loads()2. pyecharts的入门使用(折线图)2.1  pyecharts使用的简单示例2.1.1 导包2.1.2 创建对象2.1.3 添加x轴数据2.1.4 添加y轴数据2.1.5 设置全局配置项2.1.6 render()方法,生成图像3. ......
  • JackJson对象转化
    当接受jsonKey首字母为大写的时候需要用JSONProperty配合JsonIngore处理packagecom.example.demoboot.dto;importcom.example.demoboot.entity.Person;importjava.util.List;/***封装response返回的data对象对象太多可以用宽对象,把所有需要的都写一起**......