Quick.YAML.pas 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. { ***************************************************************************
  2. Copyright (c) 2015-2022 Kike Pérez
  3. Unit : Quick.YAML
  4. Description : YAML Object parser
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 17/04/2019
  8. Modified : 07/03/2022
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.YAML;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. Quick.Commons,
  28. Generics.Collections,
  29. Quick.Value;
  30. type
  31. TYamlScalar = TFlexValue;
  32. TYamlAncestor = class abstract
  33. protected
  34. fOwned : Boolean;
  35. function IsNull : Boolean; virtual;
  36. procedure AddDescendant(const aDescendent: TYamlAncestor); virtual;
  37. public
  38. constructor Create;
  39. property Owned : Boolean read fOwned write fOwned;
  40. property Null : Boolean read IsNull;
  41. function IsScalar : Boolean; virtual;
  42. end;
  43. TYamlValue = class abstract(TYamlAncestor)
  44. public
  45. function Value : TFlexValue; virtual;
  46. function AsString : string; virtual; abstract;
  47. end;
  48. TYamlString = class(TYamlValue)
  49. private
  50. fValue : string;
  51. fIsNull : Boolean;
  52. protected
  53. function IsNull : Boolean; override;
  54. public
  55. constructor Create; overload;
  56. constructor Create(const aValue : string); overload;
  57. function Value : TFlexValue; override;
  58. function IsScalar : Boolean; override;
  59. function AsString : string; override;
  60. end;
  61. TYamlInteger = class(TYamlValue)
  62. private
  63. fValue : Integer;
  64. fIsNull : Boolean;
  65. protected
  66. function IsNull : Boolean; override;
  67. public
  68. constructor Create; overload;
  69. constructor Create(const aValue : Integer); overload;
  70. function Value : TFlexValue; override;
  71. function IsScalar : Boolean; override;
  72. function AsString : string; override;
  73. end;
  74. TYamlFloat = class(TYamlValue)
  75. private
  76. fValue : Double;
  77. fIsNull : Boolean;
  78. protected
  79. function IsNull : Boolean; override;
  80. public
  81. constructor Create; overload;
  82. constructor Create(const aValue : Double); overload;
  83. function Value : TFlexValue; override;
  84. function IsScalar : Boolean; override;
  85. function AsString : string; override;
  86. end;
  87. TYamlBoolean = class(TYamlValue)
  88. private
  89. fValue : Boolean;
  90. fIsNull : Boolean;
  91. protected
  92. function IsNull : Boolean; override;
  93. public
  94. constructor Create; overload;
  95. constructor Create(const aValue : Boolean); overload;
  96. function Value : TFlexValue; override;
  97. function IsScalar : Boolean; override;
  98. function AsString : string; override;
  99. end;
  100. TYamlNull = class(TYamlValue)
  101. protected
  102. function IsNull: Boolean; override;
  103. public
  104. function Value : TFlexValue; override;
  105. function AsString : string; override;
  106. end;
  107. TYamlComment = class(TYamlValue)
  108. private
  109. fValue : string;
  110. fIsNull : Boolean;
  111. protected
  112. function IsNull : Boolean; override;
  113. public
  114. constructor Create; overload;
  115. constructor Create(const aComment : string); overload;
  116. function Value : TFlexValue; override;
  117. function IsScalar : Boolean; override;
  118. function AsString : string; override;
  119. end;
  120. TYamlPair = class(TYamlAncestor)
  121. private
  122. fName : string;
  123. fValue : TYamlValue;
  124. protected
  125. procedure AddDescendant(const aDescendent: TYamlAncestor); override;
  126. public
  127. constructor Create(const aName : string; const aValue : TYamlValue); overload;
  128. constructor Create(const aName : string; const aValue : string); overload;
  129. constructor Create(const aName : string; const aValue : Integer); overload;
  130. constructor Create(const aName : string; const aValue : Double); overload;
  131. destructor Destroy; override;
  132. property Name : string read fName write fName;
  133. property Value : TYamlValue read fValue write fValue;
  134. function ToYaml : string;
  135. end;
  136. TYamlWriter = class
  137. private
  138. fData : string;
  139. public
  140. constructor Create;
  141. property Text : string read fData;
  142. procedure Write(const aValue : string);
  143. procedure Writeln(const aValue : string);
  144. end;
  145. TYamlObject = class(TYamlValue)
  146. public type
  147. TEnumerator = class
  148. private
  149. fIndex: Integer;
  150. fObject: TYamlObject;
  151. public
  152. constructor Create(const aObject: TYamlObject);
  153. function GetCurrent: TYamlPair; inline;
  154. function MoveNext: Boolean; inline;
  155. property Current: TYamlPair read GetCurrent;
  156. end;
  157. private
  158. fMembers : TList<TYamlPair>;
  159. function GetCount : Integer;
  160. class function ParseValue(yaml : TList<string>; var vIndex : Integer): TYamlAncestor;
  161. class function ParsePairName(const aPair : string) : string;
  162. class function ParsePairValue(const aPair : string) : string;
  163. class function ParseArrayValue(const aValue : string) : TYamlValue;
  164. class function GetItemLevel(const aValue : string) : Integer;
  165. function ParseToYaml(aIndent : Integer) : string;
  166. protected
  167. procedure AddDescendant(const aDescendent: TYamlAncestor); override;
  168. public
  169. constructor Create; overload;
  170. constructor Create(const aData : string); overload;
  171. destructor Destroy; override;
  172. function GetValue(const aName: string): TYamlValue;
  173. property Values[const aName: string] : TYamlValue read GetValue;
  174. procedure ParseYaml(const aData : string);
  175. property Count : Integer read GetCount;
  176. class function ParseYamlValue(const aData : string) : TYamlAncestor;
  177. function GetPair(const aIndex : Integer) : TYamlPair;
  178. function GetPairByName(const aPairName : string) : TYamlPair;
  179. function AddPair(const aPair : TYamlPair): TYamlObject; overload;
  180. function AddPair(const aName : string; const aValue : TYamlValue): TYamlObject; overload;
  181. function AddPair(const aName : string; const aValue : string): TYamlObject; overload;
  182. function RemovePair(const aPairName: string): TYamlPair;
  183. function GetEnumerator: TEnumerator; inline;
  184. property Pairs[const aIndex: Integer]: TYamlPair read GetPair;
  185. function ToYaml : string;
  186. function AsString : string; override;
  187. end;
  188. { TYamlArray }
  189. TYamlArray = class(TYamlValue)
  190. public type
  191. TEnumerator = class
  192. private
  193. fIndex : Integer;
  194. fArray : TYamlArray;
  195. public
  196. constructor Create(const aArray: TYamlArray);
  197. function GetCurrent: TYamlValue; inline;
  198. function MoveNext: Boolean; inline;
  199. property Current: TYamlValue read GetCurrent;
  200. end;
  201. private
  202. fElements: TList<TYamlValue>;
  203. function ParseToYaml(aIndent : Integer; var vIsScalar : Boolean) : string;
  204. protected
  205. procedure AddDescendant(const aDescendant: TYamlAncestor); override;
  206. function GetCount: Integer; inline;
  207. function GetValue(const aIndex: Integer): TYamlValue; overload; inline;
  208. public
  209. constructor Create; overload;
  210. constructor Create(const aFirstElem: TYamlValue); overload;
  211. destructor Destroy; override;
  212. property Count: Integer read GetCount;
  213. property Items[const aIndex: Integer]: TYamlValue read GetValue;
  214. procedure AddElement(const aElement: TYamlValue);
  215. function GetEnumerator: TEnumerator; inline;
  216. function AsString : string; override;
  217. end;
  218. EYAMLException = class(Exception);
  219. implementation
  220. const
  221. NUM_INDENT = 2;
  222. { TYamlAncestor }
  223. procedure TYamlAncestor.AddDescendant(const aDescendent: TYamlAncestor);
  224. begin
  225. raise EYAMLException.CreateFmt('Cannot add value %s to %s',[aDescendent.ClassName,ClassName]);
  226. end;
  227. constructor TYamlAncestor.Create;
  228. begin
  229. inherited Create;
  230. fOwned := True;
  231. end;
  232. function TYamlAncestor.IsNull: Boolean;
  233. begin
  234. Result := False;
  235. end;
  236. function TYamlAncestor.IsScalar: Boolean;
  237. begin
  238. Result := False;
  239. end;
  240. { TYamlObject }
  241. function TYamlObject.AddPair(const aPair: TYamlPair): TYamlObject;
  242. begin
  243. if aPair <> nil then AddDescendant(aPair);
  244. Result := Self;
  245. end;
  246. function TYamlObject.AddPair(const aName: string; const aValue: TYamlValue): TYamlObject;
  247. begin
  248. if (not aName.IsEmpty) and (aValue <> nil) then AddPair(TYamlPair.Create(aName,aValue));
  249. Result := Self;
  250. end;
  251. procedure TYamlObject.AddDescendant(const aDescendent: TYamlAncestor);
  252. begin
  253. if aDescendent <> nil then fMembers.Add(TYamlPair(aDescendent));
  254. end;
  255. function TYamlObject.AddPair(const aName, aValue: string): TYamlObject;
  256. begin
  257. if not aName.IsEmpty and (not aValue.IsEmpty) then AddPair(TYamlPair.Create(aName,aValue));
  258. Result := Self;
  259. end;
  260. function TYamlObject.AsString: string;
  261. begin
  262. Result := ToYaml;
  263. end;
  264. constructor TYamlObject.Create(const aData: string);
  265. begin
  266. inherited Create;
  267. ParseYaml(aData);
  268. end;
  269. constructor TYamlObject.Create;
  270. begin
  271. inherited Create;
  272. fMembers := TList<TYamlPair>.Create;
  273. end;
  274. destructor TYamlObject.Destroy;
  275. var
  276. member: TYamlAncestor;
  277. i: Integer;
  278. begin
  279. if Assigned(fMembers) then
  280. for i := 0 to fMembers.Count - 1 do
  281. begin
  282. {$IFNDEF FPC}
  283. member := fMembers.List[i];
  284. {$ELSE}
  285. member := fMembers.Items[i];
  286. {$ENDIF}
  287. if Assigned(member) and member.Owned then member.Free;
  288. end;
  289. FreeAndNil(fMembers);
  290. inherited;
  291. end;
  292. function TYamlObject.GetCount: Integer;
  293. begin
  294. Result := fMembers.Count;
  295. end;
  296. function TYamlObject.GetEnumerator: TEnumerator;
  297. begin
  298. Result := TEnumerator.Create(Self);
  299. end;
  300. class function TYamlObject.GetItemLevel(const aValue: string): Integer;
  301. var
  302. i : Integer;
  303. trimed : string;
  304. begin
  305. trimed := aValue.Trim;
  306. if trimed.IsEmpty or trimed.StartsWith('#') then Exit(99999);
  307. for i := Low(aValue) to aValue.Length do
  308. begin
  309. if aValue[i] <> ' ' then Exit(i);
  310. end;
  311. Result := Low(aValue);
  312. end;
  313. function TYamlObject.GetPair(const aIndex: Integer): TYamlPair;
  314. begin
  315. Result := fMembers[aIndex];
  316. end;
  317. function TYamlObject.GetPairByName(const aPairName: string): TYamlPair;
  318. var
  319. yamlpair : TYamlPair;
  320. I: Integer;
  321. begin
  322. for i := 0 to Count - 1 do
  323. begin
  324. {$IFNDEF FPC}
  325. yamlpair := fMembers.List[i];
  326. {$ELSE}
  327. yamlpair := fMembers.Items[i];
  328. {$ENDIF}
  329. if CompareText(yamlpair.Name,aPairName) = 0 then Exit(yamlpair);
  330. end;
  331. Result := nil;
  332. end;
  333. function TYamlObject.GetValue(const aName: string): TYamlValue;
  334. var
  335. ymlpair: TYamlPair;
  336. i: Integer;
  337. begin
  338. for i := 0 to Count - 1 do
  339. begin
  340. {$IFNDEF FPC}
  341. ymlpair := fMembers.List[i];
  342. {$ELSE}
  343. ymlpair := fMembers.Items[i];
  344. {$ENDIF}
  345. if CompareText(ymlpair.Name,aName) = 0 then Exit(ymlpair.Value);
  346. end;
  347. Result := nil;
  348. end;
  349. class function TYamlObject.ParseArrayValue(const aValue: string): TYamlValue;
  350. var
  351. nint : Int64;
  352. nfloat : Double;
  353. begin
  354. if TryStrToInt64(aValue,nint) then Result := TYamlInteger.Create(nint)
  355. else if TryStrToFloat(aValue,nfloat) then Result := TYamlFloat.Create(nfloat)
  356. else Result := TYamlString.Create(aValue);
  357. end;
  358. class function TYamlObject.ParsePairName(const aPair: string): string;
  359. begin
  360. Result := Copy(aPair,0,aPair.IndexOf(':'));
  361. end;
  362. class function TYamlObject.ParsePairValue(const aPair: string): string;
  363. begin
  364. Result := Copy(aPair,aPair.IndexOf(':')+2,aPair.Length).Trim;
  365. end;
  366. class function TYamlObject.ParseValue(yaml : TList<string>; var vIndex : Integer): TYamlAncestor;
  367. type
  368. TYamlType = (ytObject, ytArray, ytScalarArray, ytScalar);
  369. var
  370. name : string;
  371. value : string;
  372. yvalue : TYamlAncestor;
  373. level : Integer;
  374. nextlevel : Integer;
  375. aitem : string;
  376. yamlType : TYamlType;
  377. begin
  378. Result := nil;
  379. level := 0;
  380. while yaml.Count > vIndex do
  381. begin
  382. value := yaml[vIndex].Trim;
  383. name := ParsePairName(value);
  384. if (name.IsEmpty) or (value.IsEmpty) or (value.StartsWith('#')) or (value.StartsWith(#9)) then Exit(nil)
  385. //else if value.StartsWith('#') then Exit(TYamlComment.Create(value))
  386. else if value.StartsWith('-') then
  387. begin
  388. yaml[vIndex] := StringReplace(yaml[vIndex],'-','',[]).TrimLeft;
  389. yamlType := ytObject;
  390. Dec(vIndex);
  391. end
  392. else if value.EndsWith(':') then
  393. begin
  394. if yaml[vIndex + 1].TrimLeft.StartsWith('-') then yamlType := ytArray
  395. else yamlType := ytObject;
  396. end
  397. else if value.IndexOf(':') < value.Length then
  398. begin
  399. value := ParsePairValue(value);
  400. if (value.StartsWith('[')) and (value.EndsWith(']')) then yamlType := ytScalarArray
  401. else yamlType := ytScalar;
  402. end
  403. else yamlType := TYamlType.ytScalar;
  404. case yamlType of
  405. ytArray : //is array
  406. begin
  407. yvalue := TYamlArray.Create;
  408. level := GetItemLevel(yaml[vIndex + 1]);
  409. repeat
  410. Inc(vIndex);
  411. yvalue.AddDescendant(ParseValue(yaml,vIndex));
  412. until (yvalue = nil) or (vIndex >= yaml.Count - 1) or (GetItemLevel(yaml[vIndex + 1]) < level);
  413. Exit(TYamlPair.Create(name,TYamlValue(yvalue)));
  414. end;
  415. ytObject : //is object
  416. begin
  417. yvalue := TYamlObject.Create;
  418. repeat
  419. Inc(vIndex);
  420. nextlevel := GetItemLevel(yaml[vIndex]);
  421. if nextlevel <> 99999 then level := nextlevel;
  422. yvalue.AddDescendant(ParseValue(yaml,vIndex));
  423. //level := GetItemLevel(yaml[vIndex]);
  424. //var level2 := GetItemLevel(yaml[offset + 1]);
  425. until (yvalue = nil) or (vIndex >= yaml.Count - 1) or (GetItemLevel(yaml[vIndex + 1]) < level);
  426. Exit(TYamlPair.Create(name,TYamlValue(yvalue)));
  427. end;
  428. ytScalarArray : //is scalar array
  429. begin
  430. yvalue := TYamlArray.Create;
  431. value := StringReplace(Copy(value,2,Value.Length-2),', ',#9,[rfReplaceAll]);
  432. for aitem in value.Split([#9]) do
  433. begin
  434. yvalue.AddDescendant(ParseArrayValue(aitem));
  435. end;
  436. Exit(TYamlPair.Create(name,TYamlValue(yvalue)));
  437. end;
  438. else Exit(TYamlPair.Create(name,value)); //is scalar
  439. end;
  440. Inc(vIndex);
  441. end;
  442. end;
  443. procedure TYamlObject.ParseYaml(const aData: string);
  444. var
  445. yaml : TList<string>;
  446. line : string;
  447. data : string;
  448. yamlvalue : TYamlAncestor;
  449. vIndex : Integer;
  450. begin
  451. yaml := TList<string>.Create;
  452. try
  453. vIndex := 0;
  454. //normalize tabs
  455. data := StringReplace(aData,#9,Spaces(NUM_INDENT),[rfReplaceAll]);
  456. {$IFNDEF LINUX}
  457. for line in data.Split([#13]) do yaml.Add(StringReplace(line,#10,'',[rfReplaceAll]));
  458. {$ELSE}
  459. for line in data.Split([#10]) do yaml.Add(StringReplace(line,#13,'',[rfReplaceAll]));
  460. {$ENDIF}
  461. while yaml.Count > vIndex do
  462. begin
  463. yamlvalue := ParseValue(yaml,vIndex);
  464. if yamlvalue <> nil then AddDescendant(yamlvalue);
  465. Inc(vIndex);
  466. end;
  467. finally
  468. yaml.Free;
  469. end;
  470. end;
  471. class function TYamlObject.ParseYamlValue(const aData : string) : TYamlAncestor;
  472. var
  473. yaml : TList<string>;
  474. line : string;
  475. data : string;
  476. yamlvalue : TYamlAncestor;
  477. vIndex : Integer;
  478. begin
  479. yaml := TList<string>.Create;
  480. try
  481. vIndex := 0;
  482. //normalize tabs
  483. data := StringReplace(aData,#9,Spaces(NUM_INDENT),[rfReplaceAll]);
  484. {$IFNDEF LINUX}
  485. for line in data.Split([#13]) do yaml.Add(StringReplace(line,#10,'',[rfReplaceAll]));
  486. {$ELSE}
  487. for line in data.Split([#10]) do yaml.Add(StringReplace(line,#13,'',[rfReplaceAll]));
  488. {$ENDIF}
  489. if yaml[0].TrimLeft.StartsWith('- ') then Result := TYamlArray.Create
  490. else Result := TYamlObject.Create;
  491. while yaml.Count > vIndex do
  492. begin
  493. yamlvalue := ParseValue(yaml,vIndex);
  494. if yamlvalue <> nil then Result.AddDescendant(yamlvalue);
  495. Inc(vIndex);
  496. end;
  497. finally
  498. yaml.Free;
  499. end;
  500. end;
  501. function TYamlObject.RemovePair(const aPairName: string): TYamlPair;
  502. var
  503. yamlpair: TYamlPair;
  504. i: Integer;
  505. begin
  506. for i := 0 to Count - 1 do
  507. begin
  508. {$IFNDEF FPC}
  509. yamlpair := TYamlPair(FMembers.List[i]);
  510. {$ELSE}
  511. yamlpair := TYamlPair(fMembers.Items[i]);
  512. {$ENDIF}
  513. if CompareText(yamlpair.Name,aPairName) = 0 then
  514. begin
  515. fMembers.Remove(yamlpair);
  516. Exit(yamlpair);
  517. end;
  518. end;
  519. Result := nil;
  520. end;
  521. function TYamlObject.ToYaml: string;
  522. begin
  523. Result := ParseToYaml(0);
  524. end;
  525. function TYamlObject.ParseToYaml(aIndent : Integer) : string;
  526. var
  527. i : Integer;
  528. member : TYamlPair;
  529. yaml : TYamlWriter;
  530. yvalue : TYamlAncestor;
  531. indent : string;
  532. isscalar : Boolean;
  533. scalar : string;
  534. rarray : string;
  535. begin
  536. yaml := TYamlWriter.Create;
  537. try
  538. indent := StringOfChar(' ',aIndent);
  539. for i := 0 to fMembers.Count - 1 do
  540. begin
  541. member := fMembers[i];
  542. if member = nil then continue;
  543. yvalue := member.Value;
  544. if (yvalue.IsScalar) or (yvalue is TYamlNull) then
  545. begin
  546. if yvalue is TYamlComment then yaml.Writeln(Format('#%s%s',[indent,TYamlComment(member.Value).AsString]))
  547. else
  548. begin
  549. if yvalue is TYamlNull then scalar := 'null'
  550. else if (yvalue is TYamlFloat) or (yvalue is TYamlBoolean) then scalar := member.Value.AsString
  551. else scalar := member.Value.Value.AsString;
  552. if scalar.IsEmpty then scalar := '""';
  553. yaml.Writeln(Format('%s%s: %s',[indent,member.Name,scalar]));
  554. if (i < fMembers.Count - 1) and (fMembers[i+1].Value is TYamlComment) then yaml.Writeln('');
  555. end;
  556. end
  557. else if (yvalue is TYamlObject) then
  558. begin
  559. yaml.Writeln(Format('%s%s:',[indent,member.Name]));
  560. yaml.Write((yvalue as TYamlObject).ParseToYaml(aIndent + NUM_INDENT));
  561. if aIndent = 0 then yaml.Writeln('');
  562. end
  563. else if (yvalue is TYamlArray) then
  564. begin
  565. isscalar := False;
  566. rarray := (yvalue as TYamlArray).ParseToYaml(aIndent + NUM_INDENT,isscalar);
  567. if isscalar then yaml.Writeln(Format('%s%s: %s',[indent,member.Name,rarray]))
  568. else
  569. begin
  570. yaml.Writeln(Format('%s%s:',[indent,member.Name]));
  571. yaml.Write(rarray);
  572. end;
  573. end;
  574. end;
  575. Result := yaml.Text;
  576. finally
  577. yaml.Free;
  578. end;
  579. end;
  580. { TYamlString }
  581. constructor TYamlString.Create(const aValue: string);
  582. begin
  583. inherited Create;
  584. fValue := aValue;
  585. fIsNull := False;
  586. end;
  587. constructor TYamlString.Create;
  588. begin
  589. inherited Create;
  590. fIsNull := True;
  591. end;
  592. function TYamlString.IsNull: Boolean;
  593. begin
  594. Result := fIsNull;
  595. end;
  596. function TYamlString.IsScalar: Boolean;
  597. begin
  598. Result := True;
  599. end;
  600. function TYamlString.AsString: string;
  601. begin
  602. Result := fValue;
  603. end;
  604. function TYamlString.Value: TFlexValue;
  605. begin
  606. Result := fValue;
  607. end;
  608. { TYamlInteger }
  609. constructor TYamlInteger.Create(const aValue: Integer);
  610. begin
  611. inherited Create;
  612. fValue := aValue;
  613. fIsNull := False;
  614. end;
  615. constructor TYamlInteger.Create;
  616. begin
  617. inherited Create;
  618. fIsNull := True;
  619. end;
  620. function TYamlInteger.IsNull: Boolean;
  621. begin
  622. Result := fIsNull;
  623. end;
  624. function TYamlInteger.IsScalar: Boolean;
  625. begin
  626. Result := True;
  627. end;
  628. function TYamlInteger.AsString: string;
  629. begin
  630. Result := IntToStr(fValue);
  631. end;
  632. function TYamlInteger.Value: TFlexValue;
  633. begin
  634. Result := fValue;
  635. end;
  636. { TYamlFloat }
  637. constructor TYamlFloat.Create(const aValue: Double);
  638. begin
  639. inherited Create;
  640. fValue := aValue;
  641. fIsNull := False;
  642. end;
  643. constructor TYamlFloat.Create;
  644. begin
  645. inherited Create;
  646. fIsNull := True;
  647. end;
  648. function TYamlFloat.IsNull: Boolean;
  649. begin
  650. Result := fIsNull;
  651. end;
  652. function TYamlFloat.IsScalar: Boolean;
  653. begin
  654. Result := True;
  655. end;
  656. function TYamlFloat.AsString: string;
  657. begin
  658. Result := FloatToStr(fValue);
  659. end;
  660. function TYamlFloat.Value: TFlexValue;
  661. begin
  662. Result := fValue;
  663. end;
  664. { TYamlPair }
  665. constructor TYamlPair.Create(const aName: string; const aValue: TYamlValue);
  666. begin
  667. inherited Create;
  668. fName := aName;
  669. fValue := aValue;
  670. end;
  671. constructor TYamlPair.Create(const aName, aValue: string);
  672. begin
  673. inherited Create;
  674. fName := aName;
  675. fValue := TYamlString.Create(aValue);
  676. end;
  677. constructor TYamlPair.Create(const aName: string; const aValue: Double);
  678. begin
  679. inherited Create;
  680. fName := aName;
  681. fValue := TYamlFloat.Create(aValue);
  682. end;
  683. constructor TYamlPair.Create(const aName: string; const aValue: Integer);
  684. begin
  685. inherited Create;
  686. fName := aName;
  687. fValue := TYamlInteger.Create(aValue);
  688. end;
  689. destructor TYamlPair.Destroy;
  690. begin
  691. if (fValue <> nil) and fValue.Owned then FreeAndNil(fValue);
  692. inherited Destroy;
  693. end;
  694. function TYamlPair.ToYaml: string;
  695. var
  696. isscalar : Boolean;
  697. begin
  698. if fValue = nil then Exit('null');
  699. if fValue is TYamlObject then Result := TYamlObject(fValue).ToYaml
  700. else if fValue is TYamlArray then Result := TYamlArray(fValue).ParseToYaml(0,isscalar)
  701. else Result := Format('%s: %s',[fName,fValue.Value.AsString]);
  702. end;
  703. procedure TYamlPair.AddDescendant(const aDescendent: TYamlAncestor);
  704. begin
  705. if fName = '' then
  706. fName := TYamlString(aDescendent).Value
  707. else if fValue = nil then
  708. fValue:= TYamlValue(aDescendent)
  709. else inherited AddDescendant(aDescendent);
  710. end;
  711. { TYamlObject.TEnumerator }
  712. constructor TYamlObject.TEnumerator.Create(const aObject: TYamlObject);
  713. begin
  714. inherited Create;
  715. fIndex := -1;
  716. fObject := aObject;
  717. end;
  718. function TYamlObject.TEnumerator.GetCurrent: TYamlPair;
  719. begin
  720. {$IFNDEF FPC}
  721. Result := fObject.fMembers.List[fIndex];
  722. {$ELSE}
  723. Result := fObject.fMembers.Items[fIndex];
  724. {$ENDIF}
  725. end;
  726. function TYamlObject.TEnumerator.MoveNext: Boolean;
  727. begin
  728. Inc(fIndex);
  729. Result := fIndex < fObject.Count;
  730. end;
  731. { TYamlValue }
  732. function TYamlValue.Value: TFlexValue;
  733. begin
  734. Result := '';
  735. end;
  736. { TYamlArray.TEnumerator }
  737. constructor TYamlArray.TEnumerator.Create(const aArray: TYamlArray);
  738. begin
  739. inherited Create;
  740. fIndex := -1;
  741. fArray := aArray;
  742. end;
  743. function TYamlArray.TEnumerator.GetCurrent: TYamlValue;
  744. begin
  745. {$IFNDEF FPC}
  746. Result := fArray.fElements.List[fIndex];
  747. {$ELSE}
  748. Result := fArray.fElements.Items[fIndex];
  749. {$ENDIF}
  750. end;
  751. function TYamlArray.TEnumerator.MoveNext: Boolean;
  752. begin
  753. Inc(fIndex);
  754. Result := fIndex < fArray.Count;
  755. end;
  756. { TYamlArray }
  757. procedure TYamlArray.AddDescendant(const aDescendant: TYamlAncestor);
  758. begin
  759. fElements.Add(TYamlValue(aDescendant));
  760. end;
  761. constructor TYamlArray.Create;
  762. begin
  763. inherited Create;
  764. fElements := TList<TYamlValue>.Create;
  765. end;
  766. constructor TYamlArray.Create(const aFirstElem: TYamlValue);
  767. begin
  768. inherited Create;
  769. AddElement(aFirstElem);
  770. end;
  771. procedure TYamlArray.AddElement(const aElement: TYamlValue);
  772. begin
  773. if aElement <> nil then AddDescendant(aElement);
  774. end;
  775. function TYamlArray.AsString: string;
  776. var
  777. first : Boolean;
  778. element : TYamlValue;
  779. begin
  780. first := True;
  781. for element in fElements do
  782. begin
  783. if first then Result := Result + element.AsString
  784. else Result := Result + ',' + element.AsString;
  785. end;
  786. Result := Format('[%s]',[Result]);
  787. end;
  788. destructor TYamlArray.Destroy;
  789. var
  790. element: TYamlAncestor;
  791. i: Integer;
  792. begin
  793. if Assigned(fElements) then
  794. for i := 0 to fElements.Count - 1 do
  795. begin
  796. element := fElements[i];
  797. if Assigned(element) and (element.Owned) then element.Free;
  798. end;
  799. if Assigned(fElements) then FreeAndNil(fElements);
  800. inherited Destroy;
  801. end;
  802. function TYamlArray.GetCount: Integer;
  803. begin
  804. Result := fElements.Count;
  805. end;
  806. function TYamlArray.GetEnumerator: TEnumerator;
  807. begin
  808. Result := TEnumerator.Create(Self);
  809. end;
  810. function TYamlArray.GetValue(const aIndex: Integer): TYamlValue;
  811. begin
  812. Result := fElements[aIndex];
  813. end;
  814. function TYamlArray.ParseToYaml(aIndent : Integer; var vIsScalar : Boolean) : string;
  815. var
  816. element : TYamlValue;
  817. yaml : TYamlWriter;
  818. yvalue : TYamlAncestor;
  819. indent : string;
  820. isscalar : Boolean;
  821. begin
  822. Result := '';
  823. yvalue := nil;
  824. yaml := TYamlWriter.Create;
  825. try
  826. indent := StringOfChar(' ',aIndent);
  827. if fElements.Count = 0 then
  828. begin
  829. vIsScalar := True;
  830. Exit('[]');
  831. end;
  832. for element in fElements do
  833. begin
  834. yvalue := element;
  835. if yvalue is TYamlPair then yvalue := TYamlPair(yvalue).value;
  836. if yvalue.IsScalar then
  837. begin
  838. {$IFNDEF FPC}
  839. if Result = '' then Result := element.AsString
  840. else Result := Result + ', ' + element.AsString;
  841. {$ELSE}
  842. if Result = '' then Result := TYamlPair(element).Value.AsString
  843. else Result := Result + ', ' + TYamlPair(element).Value.AsString;
  844. {$ENDIF}
  845. end
  846. else if (yvalue is TYamlObject) then
  847. begin
  848. yaml.Write(indent + '- ' + (yvalue as TYamlObject).ParseToYaml(aIndent + NUM_INDENT).TrimLeft);
  849. end
  850. else if (yvalue is TYamlArray) then
  851. begin
  852. yaml.Write(Format('%s%s',[indent,(yvalue as TYamlArray).ParseToYaml(aIndent + NUM_INDENT,isscalar)]))
  853. end;
  854. yaml.Writeln('');
  855. end;
  856. if (yvalue <> nil) and (yvalue.IsScalar) then
  857. begin
  858. Result := '[' + Result + ']';
  859. vIsScalar := True;
  860. end
  861. else Result := yaml.Text;
  862. finally
  863. yaml.Free;
  864. end;
  865. end;
  866. { TYamlWriter }
  867. procedure TYamlWriter.Write(const aValue: string);
  868. begin
  869. fData := fData + aValue;
  870. end;
  871. procedure TYamlWriter.Writeln(const aValue: string);
  872. begin
  873. fData := fData + aValue + CRLF;
  874. end;
  875. constructor TYamlWriter.Create;
  876. begin
  877. fData := '';
  878. end;
  879. { TYamlNull }
  880. function TYamlNull.IsNull: Boolean;
  881. begin
  882. Result := True;
  883. end;
  884. function TYamlNull.AsString: string;
  885. begin
  886. Result := 'null';
  887. end;
  888. function TYamlNull.Value: TFlexValue;
  889. begin
  890. Result := nil;
  891. end;
  892. { TYamlBoolean }
  893. constructor TYamlBoolean.Create;
  894. begin
  895. inherited Create;
  896. fIsNull := True;
  897. end;
  898. constructor TYamlBoolean.Create(const aValue: Boolean);
  899. begin
  900. inherited Create;
  901. fIsNull := False;
  902. fValue := aValue;
  903. end;
  904. function TYamlBoolean.IsNull: Boolean;
  905. begin
  906. Result := fIsNull;
  907. end;
  908. function TYamlBoolean.IsScalar: Boolean;
  909. begin
  910. Result := True;
  911. end;
  912. function TYamlBoolean.AsString: string;
  913. begin
  914. Result := BoolToStr(fValue,True).ToLower;
  915. end;
  916. function TYamlBoolean.Value: TFlexValue;
  917. begin
  918. Result := fValue;
  919. end;
  920. { TYamlComment }
  921. function TYamlComment.AsString: string;
  922. begin
  923. Result := fValue;
  924. end;
  925. constructor TYamlComment.Create;
  926. begin
  927. inherited Create;
  928. fIsNull := True;
  929. end;
  930. constructor TYamlComment.Create(const aComment: string);
  931. begin
  932. inherited Create;
  933. fIsNull := False;
  934. fValue := aComment;
  935. end;
  936. function TYamlComment.IsNull: Boolean;
  937. begin
  938. Result := fIsNull;
  939. end;
  940. function TYamlComment.IsScalar: Boolean;
  941. begin
  942. Result := True;
  943. end;
  944. function TYamlComment.Value: TFlexValue;
  945. begin
  946. end;
  947. end.