首页 > 其他分享 >json序列

json序列

时间:2024-01-25 11:12:04浏览次数:19  
标签:function const string Value json Key 序列 end

json序列

serialize.pas

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.

  json.help.pas

unit json.help;
//modified by cxg 2023-11-17 fit for d12
interface

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

type
  TJSONObjectHelper = class Helper for System.JSON.TJSONObject
  private
    procedure SetBoolean(const Key: string; const Value: Boolean);
    procedure SetFloat(const Key: string; const Value: Double);
    procedure SetInt64(const Key: string; const Value: Int64);
    procedure SetJsonObject(const Key: string; const Value: TJSONObject);
    procedure SetString(const Key, Value: string);
    procedure SetJsonArray(const Key: string; const Value: TJSONArray);
    procedure SetDateTime(const Key: string; const Value: TDateTime);
  public
    procedure LoadFromFile(const FileName: string);   //add by cxg 2023-7-11
    procedure Parse(const Value: string); overload;

    function Exist(const Key: string): Boolean;

    procedure Add(const Key: string; const Value: string; const DefaultValue: string = ''); overload;
    procedure Add(const Key: string; const Value: Boolean; const DefaultValue: Boolean = False); overload;
    procedure Add(const Key: string; const Value: Int64; const DefaultValue: Int64 = 0); overload;
    procedure Add(const Key: string; const Value: Double; const DefaultValue: Double = 0); overload;
    procedure AddDateTime(const Key: string; const Value: TDateTime; const DefaultValue: TDateTime = 0); overload;
    function AddJsonArray(const Key: string): TJSONArray; overload;
    function AddJsonObject(const Key: string): TJSONObject; overload;

    function GetBoolean(const Key: string): Boolean;
    function GetFloat(const Key: string): Double;
    function GetInt64(const Key: string): Int64;
    function GetString(const Key: string): string;
    function GetDateTime(const Key: string): TDateTime;
    function GetJsonArray(const Key: string): TJSONArray;
    function GetJsonObject(const Key: string): TJSONObject;

    function TryGetBoolean(const Key: string; var Value: Boolean): Boolean;
    function TryGetFloat(const Key: string; var Value: Double): Boolean; overload;
    function TryGetFloat(const Key: string; var Value: Single): Boolean; overload;
    function TryGetInt(const Key: string; var Value: Integer): Boolean; overload;
    function TryGetInt(const Key: string; var Value: Int64): Boolean; overload;
 //   function TryGetInt(const Key: string; var Value: NativeInt): Boolean; overload;
    function TryGetInt(const Key: string; var Value: Cardinal): Boolean; overload;
    function TryGetString(const Key: string; var Value: string): Boolean;
    function TryGetDateTime(const Key: string; var Value: TDateTime): Boolean;

    property S[const Key: string]: string read GetString write SetString;
    property I[const Key: string]: Int64 read GetInt64 write SetInt64;
    property F[const Key: string]: Double read GetFloat write SetFloat;
    property B[const Key: string]: Boolean read GetBoolean write SetBoolean;
    property D[const Key: string]: TDateTime read GetDateTime write SetDateTime;
    property O[const Key: string]: TJSONObject read GetJsonObject write SetJsonObject;
    property A[const Key: string]: TJSONArray read GetJsonArray write SetJsonArray;
  end;

implementation

{ TJSONObjectHelper }

procedure TJSONObjectHelper.Add(const Key: string; const Value: Double; const DefaultValue: Double);
begin
  if Value = DefaultValue then
    Exit;
  Self.AddPair(Key, TJSONNumber.Create(Value));
end;

procedure TJSONObjectHelper.Add(const Key: string; const Value: Int64; const DefaultValue: Int64);
begin
  if Value = DefaultValue then
    Exit;
  Self.AddPair(Key, TJSONNumber.Create(Value));
end;

procedure TJSONObjectHelper.Add(const Key, Value, DefaultValue: string);
begin
  if Value = DefaultValue then
    Exit;
  Self.AddPair(Key, Value);
end;

procedure TJSONObjectHelper.Add(const Key: string; const Value, DefaultValue: Boolean);
begin
  if Value = DefaultValue then
    Exit;
  Self.AddPair(Key, TJSONBool.Create(Value));
end;

procedure TJSONObjectHelper.AddDateTime(const Key: string; const Value, DefaultValue: TDateTime);
begin
  if Value = DefaultValue then
    Exit;
  Self.AddPair(Key, FormatDateTime('yyyy-mm-dd hh:nn:ss', Value));
end;

function TJSONObjectHelper.AddJsonArray(const Key: string): TJSONArray;
begin
  Result := TJSONArray.Create;
  Self.AddPair(Key, Result);
end;

function TJSONObjectHelper.AddJsonObject(const Key: string): TJSONObject;
begin
  Result := TJSONObject.Create;
  Self.AddPair(Key, Result);
end;

function TJSONObjectHelper.Exist(const Key: string): Boolean;
begin
  Result := Assigned(GetValue(Key));
end;

function TJSONObjectHelper.GetBoolean(const Key: string): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
    Result := V.GetValue<Boolean>()
  else
    Result := False;
end;

function TJSONObjectHelper.GetDateTime(const Key: string): TDateTime;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
    Result := V.GetValue<TDateTime>()
  else
    Result := 0;
end;

function TJSONObjectHelper.GetFloat(const Key: string): Double;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
    Result := V.GetValue<Double>()
  else
    Result := 0;
end;

function TJSONObjectHelper.GetInt64(const Key: string): Int64;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
    Result := V.GetValue<Int64>()
  else
    Result := 0;
end;

function TJSONObjectHelper.GetJsonArray(const Key: string): TJSONArray;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    if V is TJSONArray then
      Result := V as TJSONArray
    else
      Result := nil;
  end
  else
    Result := nil;
end;

function TJSONObjectHelper.GetJsonObject(const Key: string): TJSONObject;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) and (V is TJSONObject) then
    Result := V as TJSONObject
  else
    Result := nil;
end;

function TJSONObjectHelper.GetString(const Key: string): string;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
    Result := V.GetValue<string>()
  else
    Result := '';
end;

procedure TJSONObjectHelper.LoadFromFile(const FileName: string);
var f: TStringList;
begin
  f := TStringList.Create;
  try
    f.LoadFromFile(FileName{, TEncoding.UTF8});
    Self.Parse(f.Text);
  finally
    f.Free;
  end;
end;

procedure TJSONObjectHelper.Parse(const Value: string);
var
  V: TArray<Byte>;
begin
  V := TEncoding.Default.GetBytes(Value);
  Self.Parse(V, 0);
end;

procedure TJSONObjectHelper.SetBoolean(const Key: string; const Value: Boolean);
begin
  RemovePair(Key);
  Add(Key, Value);
end;

procedure TJSONObjectHelper.SetDateTime(const Key: string; const Value: TDateTime);
begin
  RemovePair(Key);
  Add(Key, Value);
end;

procedure TJSONObjectHelper.SetFloat(const Key: string; const Value: Double);
begin
  RemovePair(Key);
  Add(Key, Value);
end;

procedure TJSONObjectHelper.SetInt64(const Key: string; const Value: Int64);
begin
  RemovePair(Key);
  Add(Key, Value);
end;

procedure TJSONObjectHelper.SetJsonArray(const Key: string; const Value: TJSONArray);
begin
  RemovePair(Key);
  AddPair(Key, Value);
end;

procedure TJSONObjectHelper.SetJsonObject(const Key: string; const Value: TJSONObject);
begin
  RemovePair(Key);
  AddPair(Key, Value);
end;

procedure TJSONObjectHelper.SetString(const Key, Value: string);
begin
  RemovePair(Key);
  Add(Key, Value);
end;

function TJSONObjectHelper.TryGetBoolean(const Key: string; var Value: Boolean): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<Boolean>()
  end
  else
    Result := False;
end;

function TJSONObjectHelper.TryGetDateTime(const Key: string; var Value: TDateTime): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<TDateTime>()
  end
  else
    Result := False;
end;

function TJSONObjectHelper.TryGetFloat(const Key: string; var Value: Double): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<Double>()
  end
  else
    Result := False;
end;

function TJSONObjectHelper.TryGetFloat(const Key: string; var Value: Single): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<Single>()
  end
  else
    Result := False;
end;

function TJSONObjectHelper.TryGetInt(const Key: string; var Value: NativeInt): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<NativeInt>()
  end
  else
    Result := False;
end;

function TJSONObjectHelper.TryGetInt(const Key: string; var Value: Int64): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<Int64>()
  end
  else
    Result := False;
end;
{
function TJSONObjectHelper.TryGetInt(const Key: string; var Value: Integer): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<Integer>()
  end
  else
    Result := False;
end;      }

function TJSONObjectHelper.TryGetInt(const Key: string; var Value: Cardinal): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<Cardinal>()
  end
  else
    Result := False;
end;

function TJSONObjectHelper.TryGetString(const Key: string; var Value: string): Boolean;
var
  V: TJSONValue;
begin
  V := GetValue(Key);
  if Assigned(V) then
  begin
    Result := True;
    Value := V.GetValue<string>()
  end
  else
    Result := False;
end;

end.

  

标签:function,const,string,Value,json,Key,序列,end
From: https://www.cnblogs.com/hnxxcxg/p/17986735

相关文章

  • PYTHON 用几何布朗运动模型和蒙特卡罗MONTE CARLO随机过程模拟股票价格可视化分析耐克
    原文链接:http://tecdat.cn/?p=27099最近我们被客户要求撰写关于蒙特卡罗的研究报告,包括一些图形和统计输出。金融资产/证券已使用多种技术进行建模。该项目的主要目标是使用几何布朗运动模型和蒙特卡罗模拟来模拟股票价格。该模型基于受乘性噪声影响的随机(与确定性相反)变量该项......
  • 为啥赋值他就报错JSONDecodeError?
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【哎呦喂 是豆子~】问了一个Python解析的问题。问题如下:大佬们谁有时间帮忙看下这个情况 好奇怪哦 为啥赋值他就报错JSONDecodeError:Expectingvalue:line1column1(char0)load是load文件,loads才是数据。......
  • 代码随想录 day29 非递减子序列 全排列 全排列 II
    非递减子序列cpp就业还是太难了还是转java吧好歹这个对双非还友好一些尝试写java的第一天本题关键是理解非递减子序列判断条件需要额外一个数组记录当前元素是否在本树层使用过记录在这个数组就说明用过了全排列本题系统的演示了怎么写全排列和最基本的组合问题的......
  • PgSql 行转列的使用 jsonb_each与row_to_json
    PgSql行转列的使用jsonb_each与row_to_json1:正常的几行数据2:转换后3:code(以commodity来分组)select"Id","JabilPn","Brand","PricelnUsd","Commodity"from"Eme_Materials"emwhere"Id"<=3s......
  • python创建json文件并换行
     在Python中,您可以使用内置的json 模块创建和操作JSON文件。以下是如何创建一个JSON文件的步骤:1、Json格式JSON数据的书写格式是键(名称)/值对。JSON值可以是:字符串(在双引号中)、数组(在中括号中)、数字(整数或浮点数)、逻辑值(true或false)、对象(在大括号中)、null。JSON......
  • 21-有参转录组实战7-基因序列提取
    #本教程仿自于“https://zhuanlan.zhihu.com/p/439168788”。#正则表达式教程https://www.runoob.com/regexp/regexp-tutorial.html。#1,提取转录本gffreadPtri_genome.gtf-gPtri_genome.fa-wPtri.transcripts.fa#2,CDSgffreadPtri_genome.gtf-gPtri_genome.fa-xPt......
  • 阿里序列建模论文DIEN
    背景DIEN通过引入GRU结构来建模用户的兴趣进化趋势 方法整体结构DIEN和常用模型的差异点在序列建模的部分,该部分结构由兴趣提取层和兴趣进化层两个部分组成:兴趣提取层:从用户的行为序列中提取用户的兴趣序列兴趣进化层:建模和targetitem相关的兴趣进化过程 兴趣提取......
  • Jackson+Feign反序列化问题排查
    概述本文记录在使用SpringCloud微服务开发时遇到的一个反序列化问题,RPC/HTTP框架使用的是Feign,JSON序列化反序列化工具是Jackson。问题测试环境的ELK告警日志如下:-[43f42bf7]500ServerErrorforHTTPPOST"/api/open/dialog/nextQuestion"feign.codec.DecodeException:......
  • # yyds干货盘点 # 解析json数据,指定列去解析报错如何破?
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【哎呦喂 是豆子~】问了一个Python解析的问题。问题如下:大佬们请问下这个是啥情况呀 解析json数据 指定列去解析报错JSONDecodeError:Expecting','delimiter:line1column73(char72)数据不多我就一个个去试指......
  • Pickle反序列化学习
    什么是Pickle?很简单,就是一个python的序列化模块,方便对象的传输与存储。但是pickle的灵活度很高,可以通过对opcode的编写来实现代码执行的效果,由此引发一系列的安全问题Pickle使用举个简单的例子importpickleclassPerson():def__init__(self):self.age=18......