System.JSON.BSON.pas
System.JSON.BSON.pas
这可是DELPHI官方支持BSON的单元。是DELPHI10开始增加的。
TBsonWriter,TBsonReader 分别用于BSON序列和还原。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | uses System.JSON.BSON, System.JSON.Writers, System.JSON.Builders; procedure BSONWriterSample; var Builder: TJSONObjectBuilder; //To use the built-in JSON builder BytesStream: TBytesStream; //To create the BSON document as a byte stream Writer: TBsonWriter; //A BSON writer Bytes: TBytes; //To contain the bytes from the byte stream begin BytesStream := TBytesStream.Create; Writer := TBsonWriter.Create(BytesStream); Builder := TJSONObjectBuilder.Create(Writer); try Builder .BeginObject .BeginArray( 'colors' ) .BeginObject .Add( 'name' , 'red' ) .Add( 'hex' , '#f00' ) .EndAll; Bytes := BytesStream.Bytes; SetLength(Bytes, BytesStream.Size); //Bytes contains the BSON document. Memo1.text := Bytes2String(Bytes); //To see the representation of the BSON document in a TMemo, passing the Bytes var that contains the BSON document. finally BytesStream.free; Writer.free; Builder.free; end; end; |
1 2 3 4 5 6 7 8 | { "colors" : [ { "name" : "red" , "hex" : "#f00" } ] } |
1 2 3 4 5 6 7 8 9 10 11 | function Bytes2String( const ABytes: TBytes): string ; var I: Integer; begin Result := '' ; for I := Low(ABytes) to High(ABytes) do if I = 0 then Result := IntToHex(ABytes[I], 2) else Result := Result + '-' + IntToHex(ABytes[I], 2); end; |
序列JSON对象为BSON:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | uses System.JSON.BSON, System.JSON.Writers, System.JSON.Readers, System.JSON.Builders; procedure JSONtoBSON; var SR: TStringReader; //A String Reader JsonReader: TJsonTextReader; //A JSON text Reader BsonWriter: TBsonWriter; // A BSON writer Stream: TBytesStream; //To create the BSON document as a byte stream Bytes: TBytes; //To contain the bytes from the byte stream begin SR := TStringReader.Create(Memo1.Text); //It gets the JSON object as a string from a TMemo. JsonReader := TJsonTextReader.Create(SR); Stream := TBytesStream.Create; BsonWriter := TBsonWriter.Create(Stream); try BsonWriter.WriteToken(JsonReader); //It gets the JSON object as an argument and analize the object token by token. Stream.Position := 0; Bytes := Stream.Bytes; SetLength(Bytes, Stream.Size); Memo2.Text := Bytes2String(Bytes); //It shows the String representation of the BSON document in a TMemo. finally SR.free; JsonReader.free; Stream.free; BsonWriter.free; end; end; |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/13636089.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2016-09-08 FDMemTable的详细使用方法