Quick.YAML.pas 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. { ***************************************************************************
  2. Copyright (c) 2015-2020 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 : 05/02/2020
  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; virtual;
  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; virtual;
  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; virtual;
  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; virtual;
  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 InSameLevel(const aValue1, aValue2 : string) : Boolean;
  166. function ParseToYaml(aIndent : Integer) : string;
  167. protected
  168. procedure AddDescendant(const aDescendent: TYamlAncestor); override;
  169. public
  170. constructor Create; overload;
  171. constructor Create(const aData : string); overload;
  172. destructor Destroy; override;
  173. function GetValue(const aName: string): TYamlValue;
  174. property Values[const aName: string] : TYamlValue read GetValue;
  175. procedure ParseYaml(const aData : string);
  176. property Count : Integer read GetCount;
  177. class function ParseYamlValue(const aData : string) : TYamlAncestor;
  178. function GetPair(const aIndex : Integer) : TYamlPair;
  179. function GetPairByName(const aPairName : string) : TYamlPair;
  180. function AddPair(const aPair : TYamlPair): TYamlObject; overload;
  181. function AddPair(const aName : string; const aValue : TYamlValue): TYamlObject; overload;
  182. function AddPair(const aName : string; const aValue : string): TYamlObject; overload;
  183. function RemovePair(const aPairName: string): TYamlPair;
  184. function GetEnumerator: TEnumerator; inline;
  185. property Pairs[const aIndex: Integer]: TYamlPair read GetPair;
  186. function ToYaml : string;
  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. end;
  217. EYAMLException = class(Exception);
  218. implementation
  219. const
  220. CRLF = #13#10;
  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. constructor TYamlObject.Create(const aData: string);
  261. begin
  262. inherited Create;
  263. ParseYaml(aData);
  264. end;
  265. constructor TYamlObject.Create;
  266. begin
  267. inherited Create;
  268. fMembers := TList<TYamlPair>.Create;
  269. end;
  270. destructor TYamlObject.Destroy;
  271. var
  272. member: TYamlAncestor;
  273. i: Integer;
  274. begin
  275. if Assigned(fMembers) then
  276. for i := 0 to fMembers.Count - 1 do
  277. begin
  278. {$IFNDEF FPC}
  279. member := fMembers.List[i];
  280. {$ELSE}
  281. member := fMembers.Items[i];
  282. {$ENDIF}
  283. if Assigned(member) and member.Owned then member.Free;
  284. end;
  285. FreeAndNil(fMembers);
  286. inherited;
  287. end;
  288. function TYamlObject.GetCount: Integer;
  289. begin
  290. Result := fMembers.Count;
  291. end;
  292. function TYamlObject.GetEnumerator: TEnumerator;
  293. begin
  294. Result := TEnumerator.Create(Self);
  295. end;
  296. class function TYamlObject.GetItemLevel(const aValue: string): Integer;
  297. var
  298. i : Integer;
  299. trimed : string;
  300. begin
  301. trimed := aValue.Trim;
  302. if trimed.IsEmpty or trimed.StartsWith('#') then Exit(99999);
  303. for i := Low(aValue) to aValue.Length do
  304. begin
  305. if aValue[i] <> ' ' then Exit(i);
  306. end;
  307. Result := Low(aValue);
  308. end;
  309. function TYamlObject.GetPair(const aIndex: Integer): TYamlPair;
  310. begin
  311. Result := fMembers[aIndex];
  312. end;
  313. function TYamlObject.GetPairByName(const aPairName: string): TYamlPair;
  314. var
  315. yamlpair : TYamlPair;
  316. I: Integer;
  317. begin
  318. for i := 0 to Count - 1 do
  319. begin
  320. {$IFNDEF FPC}
  321. yamlpair := fMembers.List[i];
  322. {$ELSE}
  323. yamlpair := fMembers.Items[i];
  324. {$ENDIF}
  325. if CompareText(yamlpair.Name,aPairName) = 0 then Exit(yamlpair);
  326. end;
  327. Result := nil;
  328. end;
  329. function TYamlObject.GetValue(const aName: string): TYamlValue;
  330. var
  331. ymlpair: TYamlPair;
  332. i: Integer;
  333. begin
  334. for i := 0 to Count - 1 do
  335. begin
  336. {$IFNDEF FPC}
  337. ymlpair := fMembers.List[i];
  338. {$ELSE}
  339. ymlpair := fMembers.Items[i];
  340. {$ENDIF}
  341. if CompareText(ymlpair.Name,aName) = 0 then Exit(ymlpair.Value);
  342. end;
  343. Result := nil;
  344. end;
  345. function TYamlObject.InSameLevel(const aValue1, aValue2: string): Boolean;
  346. begin
  347. Result := GetItemLevel(aValue1) = GetItemLevel(aValue2);
  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. while yaml.Count > vIndex do
  379. begin
  380. value := yaml[vIndex].Trim;
  381. name := ParsePairName(value);
  382. if (name.IsEmpty) or (value.IsEmpty) or (value.StartsWith('#')) or (value.StartsWith(#9)) then Exit(nil)
  383. //else if value.StartsWith('#') then Exit(TYamlComment.Create(value))
  384. else if value.StartsWith('-') then
  385. begin
  386. yaml[vIndex] := StringReplace(yaml[vIndex],'-','',[]).TrimLeft;
  387. yamlType := ytObject;
  388. Dec(vIndex);
  389. end
  390. else if value.EndsWith(':') then
  391. begin
  392. if yaml[vIndex + 1].TrimLeft.StartsWith('-') then yamlType := ytArray
  393. else yamlType := ytObject;
  394. end
  395. else if value.IndexOf(':') < value.Length then
  396. begin
  397. value := ParsePairValue(value);
  398. if (value.StartsWith('[')) and (value.EndsWith(']')) then yamlType := ytScalarArray
  399. else yamlType := ytScalar;
  400. end;
  401. case yamlType of
  402. ytArray : //is array
  403. begin
  404. yvalue := TYamlArray.Create;
  405. level := GetItemLevel(yaml[vIndex + 1]);
  406. repeat
  407. Inc(vIndex);
  408. yvalue.AddDescendant(ParseValue(yaml,vIndex));
  409. until (yvalue = nil) or (vIndex >= yaml.Count - 1) or (GetItemLevel(yaml[vIndex + 1]) < level);
  410. Exit(TYamlPair.Create(name,TYamlValue(yvalue)));
  411. end;
  412. ytObject : //is object
  413. begin
  414. yvalue := TYamlObject.Create;
  415. repeat
  416. Inc(vIndex);
  417. nextlevel := GetItemLevel(yaml[vIndex]);
  418. if nextlevel <> 99999 then level := nextlevel;
  419. yvalue.AddDescendant(ParseValue(yaml,vIndex));
  420. //level := GetItemLevel(yaml[vIndex]);
  421. //var level2 := GetItemLevel(yaml[offset + 1]);
  422. until (yvalue = nil) or (vIndex >= yaml.Count - 1) or (GetItemLevel(yaml[vIndex + 1]) < level);
  423. Exit(TYamlPair.Create(name,TYamlValue(yvalue)));
  424. end;
  425. ytScalarArray : //is scalar array
  426. begin
  427. yvalue := TYamlArray.Create;
  428. value := StringReplace(Copy(value,2,Value.Length-2),', ',#9,[rfReplaceAll]);
  429. for aitem in value.Split([#9]) do
  430. begin
  431. yvalue.AddDescendant(ParseArrayValue(aitem));
  432. end;
  433. Exit(TYamlPair.Create(name,TYamlValue(yvalue)));
  434. end;
  435. else Exit(TYamlPair.Create(name,value)); //is scalar
  436. end;
  437. Inc(vIndex);
  438. end;
  439. end;
  440. procedure TYamlObject.ParseYaml(const aData: string);
  441. var
  442. yaml : TList<string>;
  443. line : string;
  444. data : string;
  445. yamlvalue : TYamlAncestor;
  446. vIndex : Integer;
  447. begin
  448. yaml := TList<string>.Create;
  449. try
  450. vIndex := 0;
  451. //normalize tabs
  452. data := StringReplace(aData,#9,Spaces(4),[rfReplaceAll]);
  453. {$IFNDEF LINUX}
  454. for line in data.Split([#13]) do yaml.Add(StringReplace(line,#10,'',[rfReplaceAll]));
  455. {$ELSE}
  456. for line in data.Split([#10]) do yaml.Add(StringReplace(line,#13,'',[rfReplaceAll]));
  457. {$ENDIF}
  458. while yaml.Count > vIndex do
  459. begin
  460. yamlvalue := ParseValue(yaml,vIndex);
  461. if yamlvalue <> nil then AddDescendant(yamlvalue);
  462. Inc(vIndex);
  463. end;
  464. finally
  465. yaml.Free;
  466. end;
  467. end;
  468. class function TYamlObject.ParseYamlValue(const aData : string) : TYamlAncestor;
  469. var
  470. yaml : TList<string>;
  471. line : string;
  472. data : string;
  473. yamlvalue : TYamlAncestor;
  474. vIndex : Integer;
  475. begin
  476. yaml := TList<string>.Create;
  477. try
  478. vIndex := 0;
  479. //normalize tabs
  480. data := StringReplace(aData,#9,Spaces(4),[rfReplaceAll]);
  481. {$IFNDEF LINUX}
  482. for line in data.Split([#13]) do yaml.Add(StringReplace(line,#10,'',[rfReplaceAll]));
  483. {$ELSE}
  484. for line in data.Split([#10]) do yaml.Add(StringReplace(line,#13,'',[rfReplaceAll]));
  485. {$ENDIF}
  486. if yaml[0].TrimLeft.StartsWith('- ') then Result := TYamlArray.Create
  487. else Result := TYamlObject.Create;
  488. while yaml.Count > vIndex do
  489. begin
  490. yamlvalue := ParseValue(yaml,vIndex);
  491. if yamlvalue <> nil then Result.AddDescendant(yamlvalue);
  492. Inc(vIndex);
  493. end;
  494. finally
  495. yaml.Free;
  496. end;
  497. end;
  498. function TYamlObject.RemovePair(const aPairName: string): TYamlPair;
  499. var
  500. yamlpair: TYamlPair;
  501. i: Integer;
  502. begin
  503. for i := 0 to Count - 1 do
  504. begin
  505. {$IFNDEF FPC}
  506. yamlpair := TYamlPair(FMembers.List[i]);
  507. {$ELSE}
  508. yamlpair := TYamlPair(fMembers.Items[i]);
  509. {$ENDIF}
  510. if CompareText(yamlpair.Name,aPairName) = 0 then
  511. begin
  512. fMembers.Remove(yamlpair);
  513. Exit(yamlpair);
  514. end;
  515. end;
  516. Result := nil;
  517. end;
  518. function TYamlObject.ToYaml: string;
  519. begin
  520. Result := ParseToYaml(0);
  521. end;
  522. function TYamlObject.ParseToYaml(aIndent : Integer) : string;
  523. var
  524. i : Integer;
  525. member : TYamlPair;
  526. yaml : TYamlWriter;
  527. yvalue : TYamlAncestor;
  528. indent : string;
  529. isscalar : Boolean;
  530. scalar : string;
  531. rarray : string;
  532. begin
  533. yaml := TYamlWriter.Create;
  534. try
  535. indent := StringOfChar(' ',aIndent);
  536. for i := 0 to fMembers.Count - 1 do
  537. begin
  538. member := fMembers[i];
  539. if member = nil then continue;
  540. yvalue := member.Value;
  541. if yvalue.IsScalar then
  542. begin
  543. if yvalue is TYamlComment then yaml.Writeln(Format('#%s%s',[indent,TYamlComment(member.Value).AsString]))
  544. else
  545. begin
  546. scalar := member.Value.Value.AsString;
  547. if scalar.IsEmpty then scalar := '""';
  548. yaml.Writeln(Format('%s%s: %s',[indent,member.Name,scalar]));
  549. if (i < fMembers.Count - 1) and (fMembers[i+1].Value is TYamlComment) then yaml.Writeln('');
  550. end;
  551. end
  552. else if (yvalue is TYamlObject) then
  553. begin
  554. yaml.Writeln(Format('%s%s:',[indent,member.Name]));
  555. yaml.Write((yvalue as TYamlObject).ParseToYaml(aIndent + NUM_INDENT));
  556. if aIndent = 0 then yaml.Writeln('');
  557. end
  558. else if (yvalue is TYamlArray) then
  559. begin
  560. isscalar := False;
  561. rarray := (yvalue as TYamlArray).ParseToYaml(aIndent + NUM_INDENT,isscalar);
  562. if isscalar then yaml.Writeln(Format('%s%s: %s',[indent,member.Name,rarray]))
  563. else
  564. begin
  565. yaml.Writeln(Format('%s%s:',[indent,member.Name]));
  566. yaml.Write(rarray);
  567. end;
  568. end;
  569. end;
  570. Result := yaml.Text;
  571. finally
  572. yaml.Free;
  573. end;
  574. end;
  575. { TYamlString }
  576. constructor TYamlString.Create(const aValue: string);
  577. begin
  578. inherited Create;
  579. fValue := aValue;
  580. fIsNull := False;
  581. end;
  582. constructor TYamlString.Create;
  583. begin
  584. inherited Create;
  585. fIsNull := True;
  586. end;
  587. function TYamlString.IsNull: Boolean;
  588. begin
  589. Result := fIsNull;
  590. end;
  591. function TYamlString.IsScalar: Boolean;
  592. begin
  593. Result := True;
  594. end;
  595. function TYamlString.AsString: string;
  596. begin
  597. Result := fValue;
  598. end;
  599. function TYamlString.Value: TFlexValue;
  600. begin
  601. Result := fValue;
  602. end;
  603. { TYamlInteger }
  604. constructor TYamlInteger.Create(const aValue: Integer);
  605. begin
  606. inherited Create;
  607. fValue := aValue;
  608. fIsNull := False;
  609. end;
  610. constructor TYamlInteger.Create;
  611. begin
  612. inherited Create;
  613. fIsNull := True;
  614. end;
  615. function TYamlInteger.IsNull: Boolean;
  616. begin
  617. Result := fIsNull;
  618. end;
  619. function TYamlInteger.IsScalar: Boolean;
  620. begin
  621. Result := True;
  622. end;
  623. function TYamlInteger.AsString: string;
  624. begin
  625. Result := IntToStr(fValue);
  626. end;
  627. function TYamlInteger.Value: TFlexValue;
  628. begin
  629. Result := fValue;
  630. end;
  631. { TYamlFloat }
  632. constructor TYamlFloat.Create(const aValue: Double);
  633. begin
  634. inherited Create;
  635. fValue := aValue;
  636. fIsNull := False;
  637. end;
  638. constructor TYamlFloat.Create;
  639. begin
  640. inherited Create;
  641. fIsNull := True;
  642. end;
  643. function TYamlFloat.IsNull: Boolean;
  644. begin
  645. Result := fIsNull;
  646. end;
  647. function TYamlFloat.IsScalar: Boolean;
  648. begin
  649. Result := True;
  650. end;
  651. function TYamlFloat.AsString: string;
  652. begin
  653. Result := FloatToStr(fValue);
  654. end;
  655. function TYamlFloat.Value: TFlexValue;
  656. begin
  657. Result := fValue;
  658. end;
  659. { TYamlPair }
  660. constructor TYamlPair.Create(const aName: string; const aValue: TYamlValue);
  661. begin
  662. inherited Create;
  663. fName := aName;
  664. fValue := aValue;
  665. end;
  666. constructor TYamlPair.Create(const aName, aValue: string);
  667. begin
  668. inherited Create;
  669. fName := aName;
  670. fValue := TYamlString.Create(aValue);
  671. end;
  672. constructor TYamlPair.Create(const aName: string; const aValue: Double);
  673. begin
  674. inherited Create;
  675. fName := aName;
  676. fValue := TYamlFloat.Create(aValue);
  677. end;
  678. constructor TYamlPair.Create(const aName: string; const aValue: Integer);
  679. begin
  680. inherited Create;
  681. fName := aName;
  682. fValue := TYamlInteger.Create(aValue);
  683. end;
  684. destructor TYamlPair.Destroy;
  685. begin
  686. if (fValue <> nil) and fValue.Owned then FreeAndNil(fValue);
  687. inherited Destroy;
  688. end;
  689. function TYamlPair.ToYaml: string;
  690. var
  691. isscalar : Boolean;
  692. begin
  693. if fValue = nil then Exit('null');
  694. if fValue is TYamlObject then Result := TYamlObject(fValue).ToYaml
  695. else if fValue is TYamlArray then Result := TYamlArray(fValue).ParseToYaml(0,isscalar)
  696. else Result := Format('%s: %s',[fName,fValue.Value.AsString]);
  697. end;
  698. procedure TYamlPair.AddDescendant(const aDescendent: TYamlAncestor);
  699. begin
  700. if fName = '' then
  701. fName := TYamlString(aDescendent).Value
  702. else if fValue = nil then
  703. fValue:= TYamlValue(aDescendent)
  704. else inherited AddDescendant(aDescendent);
  705. end;
  706. { TYamlObject.TEnumerator }
  707. constructor TYamlObject.TEnumerator.Create(const aObject: TYamlObject);
  708. begin
  709. inherited Create;
  710. fIndex := -1;
  711. fObject := aObject;
  712. end;
  713. function TYamlObject.TEnumerator.GetCurrent: TYamlPair;
  714. begin
  715. {$IFNDEF FPC}
  716. Result := fObject.fMembers.List[fIndex];
  717. {$ELSE}
  718. Result := fObject.fMembers.Items[fIndex];
  719. {$ENDIF}
  720. end;
  721. function TYamlObject.TEnumerator.MoveNext: Boolean;
  722. begin
  723. Inc(fIndex);
  724. Result := fIndex < fObject.Count;
  725. end;
  726. { TYamlValue }
  727. function TYamlValue.Value: TFlexValue;
  728. begin
  729. Result := '';
  730. end;
  731. { TYamlArray.TEnumerator }
  732. constructor TYamlArray.TEnumerator.Create(const aArray: TYamlArray);
  733. begin
  734. inherited Create;
  735. fIndex := -1;
  736. fArray := aArray;
  737. end;
  738. function TYamlArray.TEnumerator.GetCurrent: TYamlValue;
  739. begin
  740. {$IFNDEF FPC}
  741. Result := fArray.fElements.List[fIndex];
  742. {$ELSE}
  743. Result := fArray.fElements.Items[fIndex];
  744. {$ENDIF}
  745. end;
  746. function TYamlArray.TEnumerator.MoveNext: Boolean;
  747. begin
  748. Inc(fIndex);
  749. Result := fIndex < fArray.Count;
  750. end;
  751. { TYamlArray }
  752. procedure TYamlArray.AddDescendant(const aDescendant: TYamlAncestor);
  753. begin
  754. fElements.Add(TYamlValue(aDescendant));
  755. end;
  756. constructor TYamlArray.Create;
  757. begin
  758. inherited Create;
  759. fElements := TList<TYamlValue>.Create;
  760. end;
  761. constructor TYamlArray.Create(const aFirstElem: TYamlValue);
  762. begin
  763. inherited Create;
  764. AddElement(aFirstElem);
  765. end;
  766. procedure TYamlArray.AddElement(const aElement: TYamlValue);
  767. begin
  768. if aElement <> nil then AddDescendant(aElement);
  769. end;
  770. destructor TYamlArray.Destroy;
  771. var
  772. element: TYamlAncestor;
  773. i: Integer;
  774. begin
  775. if Assigned(fElements) then
  776. for i := 0 to fElements.Count - 1 do
  777. begin
  778. element := fElements[i];
  779. if Assigned(element) and (element.Owned) then element.Free;
  780. end;
  781. if Assigned(fElements) then FreeAndNil(fElements);
  782. inherited Destroy;
  783. end;
  784. function TYamlArray.GetCount: Integer;
  785. begin
  786. Result := fElements.Count;
  787. end;
  788. function TYamlArray.GetEnumerator: TEnumerator;
  789. begin
  790. Result := TEnumerator.Create(Self);
  791. end;
  792. function TYamlArray.GetValue(const aIndex: Integer): TYamlValue;
  793. begin
  794. Result := fElements[aIndex];
  795. end;
  796. function TYamlArray.ParseToYaml(aIndent : Integer; var vIsScalar : Boolean) : string;
  797. var
  798. element : TYamlValue;
  799. yaml : TYamlWriter;
  800. yvalue : TYamlAncestor;
  801. indent : string;
  802. isscalar : Boolean;
  803. begin
  804. Result := '';
  805. yaml := TYamlWriter.Create;
  806. try
  807. indent := StringOfChar(' ',aIndent);
  808. if fElements.Count = 0 then
  809. begin
  810. vIsScalar := True;
  811. Exit('[]');
  812. end;
  813. for element in fElements do
  814. begin
  815. yvalue := element;
  816. if yvalue is TYamlPair then yvalue := TYamlPair(yvalue).value;
  817. if yvalue.IsScalar then
  818. begin
  819. {$IFNDEF FPC}
  820. if Result = '' then Result := element.AsString
  821. else Result := Result + ', ' + element.AsString;
  822. {$ELSE}
  823. if Result = '' then Result := TYamlPair(element).Value.AsString
  824. else Result := Result + ', ' + TYamlPair(element).Value.AsString;
  825. {$ENDIF}
  826. end
  827. else if (yvalue is TYamlObject) then
  828. begin
  829. yaml.Write(indent + '- ' + (yvalue as TYamlObject).ParseToYaml(aIndent + NUM_INDENT).TrimLeft);
  830. end
  831. else if (yvalue is TYamlArray) then
  832. begin
  833. yaml.Write(Format('%s%s',[indent,(yvalue as TYamlArray).ParseToYaml(aIndent + NUM_INDENT,isscalar)]))
  834. end;
  835. yaml.Writeln('');
  836. end;
  837. if yvalue.IsScalar then
  838. begin
  839. Result := '[' + Result + ']';
  840. vIsScalar := True;
  841. end
  842. else Result := yaml.Text;
  843. finally
  844. yaml.Free;
  845. end;
  846. end;
  847. { TYamlWriter }
  848. procedure TYamlWriter.Write(const aValue: string);
  849. begin
  850. fData := fData + aValue;
  851. end;
  852. procedure TYamlWriter.Writeln(const aValue: string);
  853. begin
  854. fData := fData + aValue + CRLF;
  855. end;
  856. constructor TYamlWriter.Create;
  857. begin
  858. fData := '';
  859. end;
  860. { TYamlNull }
  861. function TYamlNull.IsNull: Boolean;
  862. begin
  863. Result := True;
  864. end;
  865. function TYamlNull.AsString: string;
  866. begin
  867. Result := 'null';
  868. end;
  869. function TYamlNull.Value: TFlexValue;
  870. begin
  871. Result := nil;
  872. end;
  873. { TYamlBoolean }
  874. constructor TYamlBoolean.Create;
  875. begin
  876. inherited Create;
  877. fIsNull := True;
  878. end;
  879. constructor TYamlBoolean.Create(const aValue: Boolean);
  880. begin
  881. inherited Create;
  882. fIsNull := False;
  883. fValue := aValue;
  884. end;
  885. function TYamlBoolean.IsNull: Boolean;
  886. begin
  887. Result := fIsNull;
  888. end;
  889. function TYamlBoolean.IsScalar: Boolean;
  890. begin
  891. Result := True;
  892. end;
  893. function TYamlBoolean.AsString: string;
  894. begin
  895. Result := fValue.ToString(True);
  896. end;
  897. function TYamlBoolean.Value: TFlexValue;
  898. begin
  899. Result := fValue;
  900. end;
  901. { TYamlComment }
  902. function TYamlComment.AsString: string;
  903. begin
  904. Result := fValue;
  905. end;
  906. constructor TYamlComment.Create;
  907. begin
  908. inherited Create;
  909. fIsNull := True;
  910. end;
  911. constructor TYamlComment.Create(const aComment: string);
  912. begin
  913. inherited Create;
  914. fIsNull := False;
  915. fValue := aComment;
  916. end;
  917. function TYamlComment.IsNull: Boolean;
  918. begin
  919. Result := fIsNull;
  920. end;
  921. function TYamlComment.IsScalar: Boolean;
  922. begin
  923. Result := True;
  924. end;
  925. function TYamlComment.Value: TFlexValue;
  926. begin
  927. end;
  928. end.