Quick.YAML.pas 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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; 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 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) or (yvalue is TYamlNull) then
  542. begin
  543. if yvalue is TYamlComment then yaml.Writeln(Format('#%s%s',[indent,TYamlComment(member.Value).AsString]))
  544. else
  545. begin
  546. if yvalue is TYamlNull then scalar := 'null'
  547. else scalar := member.Value.Value.AsString;
  548. if scalar.IsEmpty then scalar := '""';
  549. yaml.Writeln(Format('%s%s: %s',[indent,member.Name,scalar]));
  550. if (i < fMembers.Count - 1) and (fMembers[i+1].Value is TYamlComment) then yaml.Writeln('');
  551. end;
  552. end
  553. else if (yvalue is TYamlObject) then
  554. begin
  555. yaml.Writeln(Format('%s%s:',[indent,member.Name]));
  556. yaml.Write((yvalue as TYamlObject).ParseToYaml(aIndent + NUM_INDENT));
  557. if aIndent = 0 then yaml.Writeln('');
  558. end
  559. else if (yvalue is TYamlArray) then
  560. begin
  561. isscalar := False;
  562. rarray := (yvalue as TYamlArray).ParseToYaml(aIndent + NUM_INDENT,isscalar);
  563. if isscalar then yaml.Writeln(Format('%s%s: %s',[indent,member.Name,rarray]))
  564. else
  565. begin
  566. yaml.Writeln(Format('%s%s:',[indent,member.Name]));
  567. yaml.Write(rarray);
  568. end;
  569. end;
  570. end;
  571. Result := yaml.Text;
  572. finally
  573. yaml.Free;
  574. end;
  575. end;
  576. { TYamlString }
  577. constructor TYamlString.Create(const aValue: string);
  578. begin
  579. inherited Create;
  580. fValue := aValue;
  581. fIsNull := False;
  582. end;
  583. constructor TYamlString.Create;
  584. begin
  585. inherited Create;
  586. fIsNull := True;
  587. end;
  588. function TYamlString.IsNull: Boolean;
  589. begin
  590. Result := fIsNull;
  591. end;
  592. function TYamlString.IsScalar: Boolean;
  593. begin
  594. Result := True;
  595. end;
  596. function TYamlString.AsString: string;
  597. begin
  598. Result := fValue;
  599. end;
  600. function TYamlString.Value: TFlexValue;
  601. begin
  602. Result := fValue;
  603. end;
  604. { TYamlInteger }
  605. constructor TYamlInteger.Create(const aValue: Integer);
  606. begin
  607. inherited Create;
  608. fValue := aValue;
  609. fIsNull := False;
  610. end;
  611. constructor TYamlInteger.Create;
  612. begin
  613. inherited Create;
  614. fIsNull := True;
  615. end;
  616. function TYamlInteger.IsNull: Boolean;
  617. begin
  618. Result := fIsNull;
  619. end;
  620. function TYamlInteger.IsScalar: Boolean;
  621. begin
  622. Result := True;
  623. end;
  624. function TYamlInteger.AsString: string;
  625. begin
  626. Result := IntToStr(fValue);
  627. end;
  628. function TYamlInteger.Value: TFlexValue;
  629. begin
  630. Result := fValue;
  631. end;
  632. { TYamlFloat }
  633. constructor TYamlFloat.Create(const aValue: Double);
  634. begin
  635. inherited Create;
  636. fValue := aValue;
  637. fIsNull := False;
  638. end;
  639. constructor TYamlFloat.Create;
  640. begin
  641. inherited Create;
  642. fIsNull := True;
  643. end;
  644. function TYamlFloat.IsNull: Boolean;
  645. begin
  646. Result := fIsNull;
  647. end;
  648. function TYamlFloat.IsScalar: Boolean;
  649. begin
  650. Result := True;
  651. end;
  652. function TYamlFloat.AsString: string;
  653. begin
  654. Result := FloatToStr(fValue);
  655. end;
  656. function TYamlFloat.Value: TFlexValue;
  657. begin
  658. Result := fValue;
  659. end;
  660. { TYamlPair }
  661. constructor TYamlPair.Create(const aName: string; const aValue: TYamlValue);
  662. begin
  663. inherited Create;
  664. fName := aName;
  665. fValue := aValue;
  666. end;
  667. constructor TYamlPair.Create(const aName, aValue: string);
  668. begin
  669. inherited Create;
  670. fName := aName;
  671. fValue := TYamlString.Create(aValue);
  672. end;
  673. constructor TYamlPair.Create(const aName: string; const aValue: Double);
  674. begin
  675. inherited Create;
  676. fName := aName;
  677. fValue := TYamlFloat.Create(aValue);
  678. end;
  679. constructor TYamlPair.Create(const aName: string; const aValue: Integer);
  680. begin
  681. inherited Create;
  682. fName := aName;
  683. fValue := TYamlInteger.Create(aValue);
  684. end;
  685. destructor TYamlPair.Destroy;
  686. begin
  687. if (fValue <> nil) and fValue.Owned then FreeAndNil(fValue);
  688. inherited Destroy;
  689. end;
  690. function TYamlPair.ToYaml: string;
  691. var
  692. isscalar : Boolean;
  693. begin
  694. if fValue = nil then Exit('null');
  695. if fValue is TYamlObject then Result := TYamlObject(fValue).ToYaml
  696. else if fValue is TYamlArray then Result := TYamlArray(fValue).ParseToYaml(0,isscalar)
  697. else Result := Format('%s: %s',[fName,fValue.Value.AsString]);
  698. end;
  699. procedure TYamlPair.AddDescendant(const aDescendent: TYamlAncestor);
  700. begin
  701. if fName = '' then
  702. fName := TYamlString(aDescendent).Value
  703. else if fValue = nil then
  704. fValue:= TYamlValue(aDescendent)
  705. else inherited AddDescendant(aDescendent);
  706. end;
  707. { TYamlObject.TEnumerator }
  708. constructor TYamlObject.TEnumerator.Create(const aObject: TYamlObject);
  709. begin
  710. inherited Create;
  711. fIndex := -1;
  712. fObject := aObject;
  713. end;
  714. function TYamlObject.TEnumerator.GetCurrent: TYamlPair;
  715. begin
  716. {$IFNDEF FPC}
  717. Result := fObject.fMembers.List[fIndex];
  718. {$ELSE}
  719. Result := fObject.fMembers.Items[fIndex];
  720. {$ENDIF}
  721. end;
  722. function TYamlObject.TEnumerator.MoveNext: Boolean;
  723. begin
  724. Inc(fIndex);
  725. Result := fIndex < fObject.Count;
  726. end;
  727. { TYamlValue }
  728. function TYamlValue.Value: TFlexValue;
  729. begin
  730. Result := '';
  731. end;
  732. { TYamlArray.TEnumerator }
  733. constructor TYamlArray.TEnumerator.Create(const aArray: TYamlArray);
  734. begin
  735. inherited Create;
  736. fIndex := -1;
  737. fArray := aArray;
  738. end;
  739. function TYamlArray.TEnumerator.GetCurrent: TYamlValue;
  740. begin
  741. {$IFNDEF FPC}
  742. Result := fArray.fElements.List[fIndex];
  743. {$ELSE}
  744. Result := fArray.fElements.Items[fIndex];
  745. {$ENDIF}
  746. end;
  747. function TYamlArray.TEnumerator.MoveNext: Boolean;
  748. begin
  749. Inc(fIndex);
  750. Result := fIndex < fArray.Count;
  751. end;
  752. { TYamlArray }
  753. procedure TYamlArray.AddDescendant(const aDescendant: TYamlAncestor);
  754. begin
  755. fElements.Add(TYamlValue(aDescendant));
  756. end;
  757. constructor TYamlArray.Create;
  758. begin
  759. inherited Create;
  760. fElements := TList<TYamlValue>.Create;
  761. end;
  762. constructor TYamlArray.Create(const aFirstElem: TYamlValue);
  763. begin
  764. inherited Create;
  765. AddElement(aFirstElem);
  766. end;
  767. procedure TYamlArray.AddElement(const aElement: TYamlValue);
  768. begin
  769. if aElement <> nil then AddDescendant(aElement);
  770. end;
  771. destructor TYamlArray.Destroy;
  772. var
  773. element: TYamlAncestor;
  774. i: Integer;
  775. begin
  776. if Assigned(fElements) then
  777. for i := 0 to fElements.Count - 1 do
  778. begin
  779. element := fElements[i];
  780. if Assigned(element) and (element.Owned) then element.Free;
  781. end;
  782. if Assigned(fElements) then FreeAndNil(fElements);
  783. inherited Destroy;
  784. end;
  785. function TYamlArray.GetCount: Integer;
  786. begin
  787. Result := fElements.Count;
  788. end;
  789. function TYamlArray.GetEnumerator: TEnumerator;
  790. begin
  791. Result := TEnumerator.Create(Self);
  792. end;
  793. function TYamlArray.GetValue(const aIndex: Integer): TYamlValue;
  794. begin
  795. Result := fElements[aIndex];
  796. end;
  797. function TYamlArray.ParseToYaml(aIndent : Integer; var vIsScalar : Boolean) : string;
  798. var
  799. element : TYamlValue;
  800. yaml : TYamlWriter;
  801. yvalue : TYamlAncestor;
  802. indent : string;
  803. isscalar : Boolean;
  804. begin
  805. Result := '';
  806. yaml := TYamlWriter.Create;
  807. try
  808. indent := StringOfChar(' ',aIndent);
  809. if fElements.Count = 0 then
  810. begin
  811. vIsScalar := True;
  812. Exit('[]');
  813. end;
  814. for element in fElements do
  815. begin
  816. yvalue := element;
  817. if yvalue is TYamlPair then yvalue := TYamlPair(yvalue).value;
  818. if yvalue.IsScalar then
  819. begin
  820. {$IFNDEF FPC}
  821. if Result = '' then Result := element.AsString
  822. else Result := Result + ', ' + element.AsString;
  823. {$ELSE}
  824. if Result = '' then Result := TYamlPair(element).Value.AsString
  825. else Result := Result + ', ' + TYamlPair(element).Value.AsString;
  826. {$ENDIF}
  827. end
  828. else if (yvalue is TYamlObject) then
  829. begin
  830. yaml.Write(indent + '- ' + (yvalue as TYamlObject).ParseToYaml(aIndent + NUM_INDENT).TrimLeft);
  831. end
  832. else if (yvalue is TYamlArray) then
  833. begin
  834. yaml.Write(Format('%s%s',[indent,(yvalue as TYamlArray).ParseToYaml(aIndent + NUM_INDENT,isscalar)]))
  835. end;
  836. yaml.Writeln('');
  837. end;
  838. if yvalue.IsScalar then
  839. begin
  840. Result := '[' + Result + ']';
  841. vIsScalar := True;
  842. end
  843. else Result := yaml.Text;
  844. finally
  845. yaml.Free;
  846. end;
  847. end;
  848. { TYamlWriter }
  849. procedure TYamlWriter.Write(const aValue: string);
  850. begin
  851. fData := fData + aValue;
  852. end;
  853. procedure TYamlWriter.Writeln(const aValue: string);
  854. begin
  855. fData := fData + aValue + CRLF;
  856. end;
  857. constructor TYamlWriter.Create;
  858. begin
  859. fData := '';
  860. end;
  861. { TYamlNull }
  862. function TYamlNull.IsNull: Boolean;
  863. begin
  864. Result := True;
  865. end;
  866. function TYamlNull.AsString: string;
  867. begin
  868. Result := 'null';
  869. end;
  870. function TYamlNull.Value: TFlexValue;
  871. begin
  872. Result := nil;
  873. end;
  874. { TYamlBoolean }
  875. constructor TYamlBoolean.Create;
  876. begin
  877. inherited Create;
  878. fIsNull := True;
  879. end;
  880. constructor TYamlBoolean.Create(const aValue: Boolean);
  881. begin
  882. inherited Create;
  883. fIsNull := False;
  884. fValue := aValue;
  885. end;
  886. function TYamlBoolean.IsNull: Boolean;
  887. begin
  888. Result := fIsNull;
  889. end;
  890. function TYamlBoolean.IsScalar: Boolean;
  891. begin
  892. Result := True;
  893. end;
  894. function TYamlBoolean.AsString: string;
  895. begin
  896. Result := fValue.ToString(True);
  897. end;
  898. function TYamlBoolean.Value: TFlexValue;
  899. begin
  900. Result := fValue;
  901. end;
  902. { TYamlComment }
  903. function TYamlComment.AsString: string;
  904. begin
  905. Result := fValue;
  906. end;
  907. constructor TYamlComment.Create;
  908. begin
  909. inherited Create;
  910. fIsNull := True;
  911. end;
  912. constructor TYamlComment.Create(const aComment: string);
  913. begin
  914. inherited Create;
  915. fIsNull := False;
  916. fValue := aComment;
  917. end;
  918. function TYamlComment.IsNull: Boolean;
  919. begin
  920. Result := fIsNull;
  921. end;
  922. function TYamlComment.IsScalar: Boolean;
  923. begin
  924. Result := True;
  925. end;
  926. function TYamlComment.Value: TFlexValue;
  927. begin
  928. end;
  929. end.