fpjson.schema.writer.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. {
  2. This file is part of the Free Component Library
  3. JSON Schema - Write as JSON or to stream
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit FpJson.Schema.Writer;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. {$IFDEF FPC_DOTTEDUNITS}
  16. System.Classes, System.SysUtils, FpJson.Data, FpJson.Schema.Types, FpJson.Schema.Schema;
  17. {$ELSE}
  18. Classes, SysUtils, fpjson, FpJson.Schema.Types, FpJson.Schema.Schema;
  19. {$ENDIF}
  20. Type
  21. EJSONSchemaWriter = class(EJSONSchema);
  22. { TJSONSchemaWriter }
  23. TJSONSchemaWriter = class(TComponent)
  24. private
  25. Protected
  26. Procedure WriteProperty(const aName : TJSONStringType);
  27. Procedure WriteProperty(const aName : TJSONStringType; aValue : Boolean);
  28. Procedure WriteProperty(const aName : TJSONStringType; aValue : Integer);
  29. Procedure WriteProperty(const aName : TJSONStringType; aValue : Int64);
  30. Procedure WriteProperty(const aName : TJSONStringType; aValue : Double);
  31. Procedure WriteProperty(const aName : TJSONStringType; aValue : String);
  32. Procedure WriteProperty(const aName : TJSONStringType; aValue : TJSONSchema);
  33. Procedure WriteProperty(const aName : TJSONStringType; aValue : TJSONSchemaList);
  34. Procedure WriteProperty(const aName : TJSONStringType; aValue : TStrings);
  35. Procedure WriteProperty(const aName : TJSONStringType; aValue : TJSONData);
  36. Procedure WriteProperty(const aName : TJSONStringType; aValue : TSchemaDependentRequiredList);
  37. Procedure WriteProperty(const aName : TJSONStringType; aValue : TJSONSchemaVocabularyList);
  38. Procedure WriteProperty(const aName : TJSONStringType; aValue : TSchemaSimpleTypes);
  39. procedure WriteValue(aValue: TJSONData);
  40. procedure WriteValue(aValue: TStrings);
  41. // Override in descendants
  42. Procedure WriteValue(); virtual; abstract;
  43. Procedure WriteValue(aValue : Boolean); virtual; abstract;
  44. Procedure WriteValue(aValue : Integer); virtual; abstract;
  45. Procedure WriteValue(aValue : Int64); virtual; abstract;
  46. Procedure WriteValue(aValue : Double); virtual; abstract;
  47. Procedure WriteValue(const aValue : String); virtual; abstract;
  48. Procedure StartProperty(const aName: string); virtual; abstract;
  49. Procedure EndProperty; virtual; abstract;
  50. Procedure StartArray; virtual; abstract;
  51. Procedure EndArray; virtual; abstract;
  52. Procedure NextElement; virtual; abstract;
  53. Procedure StartObject; virtual; abstract;
  54. Procedure EndObject; virtual; abstract;
  55. Procedure DoWriteSchema(aSchema : TJSONSchema);
  56. end;
  57. { TJSONSchemaWriterJSON }
  58. TJSONSchemaWriterJSON = class(TJSONSchemaWriter)
  59. Private
  60. FStack : Array of TJSONData;
  61. FCount : Integer;
  62. FPropertyName : String;
  63. protected
  64. function CurrentStruct : TJSONData;
  65. Procedure PushData(Obj : TJSONData);
  66. Procedure PopData;
  67. procedure EndArray; override;
  68. procedure EndObject; override;
  69. procedure EndProperty; override;
  70. procedure NextElement; override;
  71. procedure StartArray; override;
  72. procedure StartObject; override;
  73. procedure StartProperty(const aName: string); override;
  74. procedure WriteValue(aValue: Boolean); override;
  75. procedure WriteValue(aValue: Double); override;
  76. procedure WriteValue(aValue: Int64); override;
  77. procedure WriteValue(aValue: Integer); override;
  78. procedure WriteValue(const aValue: String); override;
  79. procedure WriteValue; override;
  80. Public
  81. function WriteSchema(aSchema : TJSONSchema) : TJSONData;
  82. end;
  83. { TJSONSchemaWriterStream }
  84. TJSONSchemaWriterStream = class(TJSONSchemaWriter)
  85. private
  86. FStream: TStream;
  87. FCounts : Array of Integer;
  88. FLen : Integer;
  89. FStrictStrings: Boolean;
  90. Protected
  91. Procedure PushElCount;
  92. Procedure PopElCount;
  93. Procedure IncElCount;
  94. Function ElCount : Integer;
  95. procedure WriteString(const aString : TJSONStringType);
  96. Procedure WriteValue(); override;
  97. Procedure WriteValue(aValue : Boolean); override;
  98. Procedure WriteValue(aValue : Integer); override;
  99. Procedure WriteValue(aValue : Int64); override;
  100. Procedure WriteValue(aValue : Double); override;
  101. Procedure WriteValue(const aValue : String); override;
  102. Procedure StartProperty(const aName: string); override;
  103. Procedure EndProperty; override;
  104. Procedure StartArray; override;
  105. Procedure EndArray; override;
  106. Procedure NextElement; override;
  107. Procedure StartObject; override;
  108. Procedure EndObject; override;
  109. Property Stream : TStream Read FStream;
  110. Public
  111. procedure WriteSchema(aSchema : TJSONSchema; aStream : TStream);
  112. property StrictStrings : Boolean Read FStrictStrings Write FStrictStrings;
  113. end;
  114. implementation
  115. uses FpJson.Schema.Consts;
  116. { TJSONSchemaWriter }
  117. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType);
  118. begin
  119. WriteValue(aName);
  120. end;
  121. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: Boolean);
  122. begin
  123. StartProperty(aName);
  124. WriteValue(aValue);
  125. EndProperty;
  126. end;
  127. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: Integer);
  128. begin
  129. StartProperty(aName);
  130. WriteValue(aValue);
  131. EndProperty;
  132. end;
  133. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: Int64);
  134. begin
  135. StartProperty(aName);
  136. WriteValue(aValue);
  137. EndProperty;
  138. end;
  139. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: Double);
  140. begin
  141. StartProperty(aName);
  142. WriteValue(aValue);
  143. EndProperty;
  144. end;
  145. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: String);
  146. begin
  147. StartProperty(aName);
  148. WriteValue(aValue);
  149. EndProperty;
  150. end;
  151. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TJSONSchema);
  152. begin
  153. case aValue.MatchType of
  154. smNone,smAny : WriteProperty(aName,aValue.MatchType=smAny);
  155. else
  156. begin
  157. StartProperty(aName);
  158. DoWriteSchema(aValue);
  159. EndProperty;
  160. end;
  161. end;
  162. end;
  163. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TJSONSchemaList);
  164. var
  165. I : Integer;
  166. begin
  167. StartProperty(aName);
  168. StartArray;
  169. for I:=0 to aValue.Count-1 do
  170. begin
  171. NextElement;
  172. DoWriteSchema(aValue.Schemas[i]);
  173. end;
  174. EndArray;
  175. EndProperty;
  176. end;
  177. procedure TJSONSchemaWriter.WriteValue(aValue: TStrings);
  178. var
  179. S : String;
  180. begin
  181. StartArray;
  182. For S in aValue do
  183. begin
  184. NextElement;
  185. WriteValue(S);
  186. end;
  187. EndArray;
  188. end;
  189. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TStrings);
  190. begin
  191. StartProperty(aName);
  192. WriteValue(aValue);
  193. EndProperty;
  194. end;
  195. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TJSONData);
  196. begin
  197. StartProperty(aName);
  198. WriteValue(aValue);
  199. EndProperty();
  200. end;
  201. procedure TJSONSchemaWriter.WriteValue(aValue: TJSONData);
  202. var
  203. Enum : TJSONEnum;
  204. begin
  205. Case aValue.JSONType of
  206. jtNull : WriteValue();
  207. jtBoolean : WriteValue(aValue.AsBoolean);
  208. jtString : WriteValue(aValue.AsString);
  209. jtNumber :
  210. case TJSONNumber(aValue).NumberType of
  211. ntInteger : WriteValue(aValue.AsInteger);
  212. ntInt64 : WriteValue(aValue.AsInt64);
  213. ntFloat : WriteValue(aValue.AsFloat);
  214. ntQword : WriteValue(aValue.AsInt64);
  215. end;
  216. jtObject :
  217. begin
  218. StartObject;
  219. For Enum in aValue do
  220. WriteProperty(Enum.Key,enum.Value);
  221. EndObject;
  222. end;
  223. jtArray :
  224. begin
  225. StartArray;
  226. For Enum in aValue do
  227. begin
  228. NextElement;
  229. WriteValue(Enum.Value);
  230. end;
  231. EndArray;
  232. end;
  233. end;
  234. end;
  235. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TSchemaDependentRequiredList);
  236. var
  237. I : Integer;
  238. D : TSchemaDependentRequired;
  239. begin
  240. if aValue.Count=0 then
  241. exit;
  242. StartProperty(aName);
  243. StartObject;
  244. For I:=0 to aValue.Count-1 do
  245. begin
  246. D:=aValue[I];
  247. StartProperty(D.Name);
  248. WriteValue(D.Required);
  249. EndProperty;
  250. end;
  251. EndObject;
  252. EndProperty;
  253. end;
  254. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TJSONSchemaVocabularyList);
  255. var
  256. I : Integer;
  257. V : TJSONSchemaVocabulary;
  258. begin
  259. if aValue.Count=0 then
  260. exit;
  261. StartProperty(aName);
  262. StartObject;
  263. For I:=0 to aValue.Count-1 do
  264. begin
  265. V:=aValue[I];
  266. StartProperty(V.URL);
  267. WriteValue(V.Enabled);
  268. EndProperty;
  269. end;
  270. EndObject;
  271. EndProperty;
  272. end;
  273. procedure TJSONSchemaWriter.WriteProperty(const aName: TJSONStringType; aValue: TSchemaSimpleTypes);
  274. var
  275. St : TSchemaSimpleType;
  276. begin
  277. StartProperty(aName);
  278. StartArray;
  279. For ST in aValue do
  280. begin
  281. NextElement;
  282. WriteValue(ST.AsString);
  283. end;
  284. EndArray;
  285. EndProperty;
  286. end;
  287. procedure TJSONSchemaWriter.DoWriteSchema(aSchema: TJSONSchema);
  288. var
  289. aKeyword : TJSONSchemaKeyword;
  290. PropName : TJSONStringType;
  291. begin
  292. if (aSchema.MatchType in [smAny,smNone]) then
  293. WriteValue(aSchema.MatchType=smAny)
  294. else
  295. begin
  296. StartObject;
  297. For aKeyword in TJSONSchemaKeyword do
  298. if aSchema.HasKeywordData(aKeyWord) then
  299. begin
  300. PropName:=aKeyword.AsString;
  301. Case aKeyword of
  302. jskUnknown : ;
  303. jskId : WriteProperty(PropName,aSchema.ID);
  304. jskAnchor : WriteProperty(PropName,aSchema.Anchor);
  305. jskSchema : WriteProperty(PropName,aSchema.Schema);
  306. jskDefs : WriteProperty(PropName,aSchema.Defs);
  307. jskTitle : WriteProperty(PropName,aSchema.Metadata.Title);
  308. jskDescription : WriteProperty(PropName,aSchema.Metadata.Description);
  309. jskDefault : WriteProperty(PropName,aSchema.MetaData.DefaultValue);
  310. jskMultipleOf : WriteProperty(PropName,aSchema.Validations.MultipleOf);
  311. jskMaximum : WriteProperty(PropName,aSchema.Validations.Maximum);
  312. jskExclusiveMaximum : WriteProperty(PropName,aSchema.Validations.ExclusiveMaximum);
  313. jskMinimum : WriteProperty(PropName,aSchema.Validations.Minimum);
  314. jskExclusiveMinimum : WriteProperty(PropName,aSchema.Validations.ExclusiveMinimum);
  315. jskMaxLength : WriteProperty(PropName,aSchema.Validations.MaxLength);
  316. jskMinLength : WriteProperty(PropName,aSchema.Validations.MinLength);
  317. jskPattern : WriteProperty(PropName,aSchema.Validations.Pattern);
  318. // jskAdditionalItems : WriteProperty(PropName,aSchema.Validations.AdditionalItems);
  319. jskItems : WriteProperty(PropName,aSchema.Items);
  320. jskPrefixItems : WriteProperty(PropName,aSchema.PrefixItems);
  321. jskMaxItems : WriteProperty(PropName,aSchema.Validations.MaxItems);
  322. jskMinItems : WriteProperty(PropName,aSchema.Validations.MinItems);
  323. jskUniqueItems : WriteProperty(PropName,aSchema.Validations.UniqueItems);
  324. jskMaxProperties : WriteProperty(PropName,aSchema.Validations.MaxProperties);
  325. jskMinProperties : WriteProperty(PropName,aSchema.Validations.MinProperties);
  326. jskMaxContains : WriteProperty(PropName,aSchema.Validations.MaxContains);
  327. jskMinContains : WriteProperty(PropName,aSchema.Validations.MinContains);
  328. jskRequired : WriteProperty(PropName,aSchema.Validations.Required);
  329. jskAdditionalProperties : WriteProperty(PropName,aSchema.AdditionalProperties);
  330. jskProperties : WriteProperty(PropName,aSchema.Properties);
  331. jskPatternProperties: WriteProperty(PropName,aSchema.PatternProperties);
  332. jskPropertyNames : WriteProperty(PropName,aSchema.PropertyNames);
  333. jskDependentSchemas : WriteProperty(PropName,aSchema.DependentSchemas);
  334. jskDependentRequired : WriteProperty(PropName,aSchema.Validations.DependentRequired);
  335. jskEnum: WriteProperty(PropName,aSchema.Validations.Enum);
  336. jskType : WriteProperty(PropName,aSchema.Validations.Types);
  337. jskAllOf : WriteProperty(PropName,aSchema.AllOf);
  338. jskAnyOf : WriteProperty(PropName,aSchema.AnyOf);
  339. jskOneOf : WriteProperty(PropName,aSchema.OneOf);
  340. jskNot : WriteProperty(PropName,aSchema.NotSchema);
  341. jskFormat : WriteProperty(PropName,aSchema.Validations.Format);
  342. jskRef : WriteProperty(PropName,aSchema.Ref);
  343. jskIf : WriteProperty(PropName,aSchema.IfSchema);
  344. jskElse : WriteProperty(PropName,aSchema.ElseSchema);
  345. jskThen : WriteProperty(PropName,aSchema.ThenSchema);
  346. jskDynamicRef : WriteProperty(PropName,aSchema.DynamicRef);
  347. jskDynamicAnchor : WriteProperty(PropName,aSchema.DynamicAnchor);
  348. jskContains : WriteProperty(PropName,aSchema.Contains);
  349. jskComment : WriteProperty(PropName,aSchema.Comment);
  350. jskConst : WriteProperty(PropName,aSchema.Validations.constValue);
  351. jskUnevaluatedItems : WriteProperty(PropName,aSchema.UnevaluatedItems);
  352. jskUnevaluatedProperties : WriteProperty(PropName,aSchema.UnevaluatedProperties);
  353. jskContentEncoding : WriteProperty(PropName,aSchema.Validations.contentEncoding);
  354. jskContentMediaType : WriteProperty(PropName,aSchema.Validations.contentMediaType);
  355. jskContentSchema : WriteProperty(PropName,aSchema.Validations.contentSchema);
  356. jskExamples : WriteProperty(PropName,aSchema.Metadata.Examples);
  357. jskDeprecated : WriteProperty(PropName,aSchema.Metadata.Deprecated);
  358. jskReadOnly : WriteProperty(PropName,aSchema.Metadata.ReadOnly);
  359. jskWriteOnly : WriteProperty(PropName,aSchema.Metadata.WriteOnly);
  360. jskVocabulary : WriteProperty(PropName,aSchema.Vocabulary);
  361. end;
  362. end;
  363. EndObject;
  364. end;
  365. end;
  366. { TJSONSchemaWriterJSON }
  367. function TJSONSchemaWriterJSON.CurrentStruct: TJSONData;
  368. begin
  369. Result:=Nil;
  370. if FCount>0 then
  371. begin
  372. Result:=FStack[FCount-1];
  373. if not (Result.JSONType in StructuredJSONTypes) then
  374. Result:=Nil;
  375. end;
  376. end;
  377. procedure TJSONSchemaWriterJSON.PushData(Obj: TJSONData);
  378. var
  379. D : TJSONData;
  380. O : TJSONObject absolute D;
  381. A : TJSONArray absolute D;
  382. AddToStack : Boolean;
  383. begin
  384. AddToStack:=(Obj.JSONType in StructuredJSONTypes) or (FCount=0);
  385. D:=CurrentStruct;
  386. if (D=Nil) then
  387. begin
  388. if (FCount>0) then
  389. Raise EJSONSchemaWriter.Create(SErrNoPushOnSimpleValue);
  390. end
  391. else
  392. Case D.JSONType of
  393. jtObject:
  394. begin
  395. if FPropertyName = '' then
  396. Raise EJSONSchemaWriter.Create(SErrNoPropertyNameForPush);
  397. O.Add(FPropertyName,Obj);
  398. FPropertyName:='';
  399. end;
  400. jtArray:
  401. begin
  402. A.Add(Obj);
  403. FPropertyName:='';
  404. end;
  405. end;
  406. if AddToStack then
  407. begin
  408. if FCount=Length(FStack) then
  409. SetLength(FStack,FCount+10);
  410. FStack[FCount]:=Obj;
  411. Inc(FCount);
  412. end;
  413. end;
  414. procedure TJSONSchemaWriterJSON.PopData;
  415. begin
  416. if FCount=0 then
  417. Raise EJSONSchemaWriter.Create(SErrCannotPop);
  418. Dec(FCount);
  419. end;
  420. procedure TJSONSchemaWriterJSON.EndArray;
  421. begin
  422. PopData;
  423. end;
  424. procedure TJSONSchemaWriterJSON.EndObject;
  425. begin
  426. PopData;
  427. end;
  428. procedure TJSONSchemaWriterJSON.EndProperty;
  429. begin
  430. If CurrentStruct=Nil then
  431. Raise EJSONSchemaWriter.Create(SErrNotAtStructuredValue);
  432. end;
  433. procedure TJSONSchemaWriterJSON.NextElement;
  434. begin
  435. If CurrentStruct=Nil then
  436. Raise EJSONSchemaWriter.Create(SErrNotAtStructuredValue);
  437. end;
  438. procedure TJSONSchemaWriterJSON.StartArray;
  439. begin
  440. PushData(TJSONArray.Create);
  441. end;
  442. procedure TJSONSchemaWriterJSON.StartObject;
  443. begin
  444. PushData(TJSONObject.Create);
  445. end;
  446. procedure TJSONSchemaWriterJSON.StartProperty(const aName: string);
  447. begin
  448. if FPropertyName<>'' then
  449. Raise EJSONSchemaWriter.CreateFmt(SPropertyNameAlreadySet,[aName,FPropertyName]);
  450. FPropertyName:=aName;
  451. end;
  452. procedure TJSONSchemaWriterJSON.WriteValue(aValue: Boolean);
  453. begin
  454. PushData(TJSONBoolean.Create(aValue));
  455. end;
  456. procedure TJSONSchemaWriterJSON.WriteValue(aValue: Double);
  457. begin
  458. PushData(TJSONFloatNumber.Create(aValue));
  459. end;
  460. procedure TJSONSchemaWriterJSON.WriteValue(aValue: Int64);
  461. begin
  462. PushData(TJSONInt64Number.Create(aValue));
  463. end;
  464. procedure TJSONSchemaWriterJSON.WriteValue(aValue: Integer);
  465. begin
  466. PushData(TJSONIntegerNumber.Create(aValue));
  467. end;
  468. procedure TJSONSchemaWriterJSON.WriteValue(const aValue: String);
  469. begin
  470. PushData(TJSONString.Create(aValue));
  471. end;
  472. procedure TJSONSchemaWriterJSON.WriteValue;
  473. begin
  474. PushData(TJSONNull.Create);
  475. end;
  476. function TJSONSchemaWriterJSON.WriteSchema(aSchema: TJSONSchema): TJSONData;
  477. begin
  478. DoWriteSchema(aSchema);
  479. if (Length(FStack)=0) or Not Assigned(FStack[0]) then
  480. Raise EJSONSchemaWriter.Create(SErrNoObjectsOnStack);
  481. Result:=FStack[0];
  482. end;
  483. { TJSONSchemaWriterStream }
  484. procedure TJSONSchemaWriterStream.WriteSchema(aSchema: TJSONSchema; aStream: TStream);
  485. begin
  486. FStream:=aStream;
  487. DoWriteSchema(aSchema);
  488. end;
  489. procedure TJSONSchemaWriterStream.PushElCount;
  490. begin
  491. if FLen=Length(FCounts) then
  492. SetLength(FCounts,FLen+10);
  493. FCounts[FLen]:=0;
  494. Inc(Flen);
  495. end;
  496. procedure TJSONSchemaWriterStream.PopElCount;
  497. begin
  498. if FLen>0 then
  499. Dec(FLen);
  500. end;
  501. procedure TJSONSchemaWriterStream.IncElCount;
  502. begin
  503. if Flen>0 then
  504. Inc(FCounts[FLen-1]);
  505. end;
  506. function TJSONSchemaWriterStream.ElCount: Integer;
  507. begin
  508. Result:=FCounts[FLen-1];
  509. end;
  510. procedure TJSONSchemaWriterStream.WriteString(const aString: TJSONStringType);
  511. begin
  512. if Length(aString)>0 then
  513. FStream.WriteBuffer(aString[1],Length(aString))
  514. end;
  515. procedure TJSONSchemaWriterStream.WriteValue();
  516. begin
  517. WriteString('null');
  518. end;
  519. procedure TJSONSchemaWriterStream.WriteValue(aValue: Boolean);
  520. begin
  521. WriteString(BoolToStr(aValue,'true','false'));
  522. end;
  523. procedure TJSONSchemaWriterStream.WriteValue(aValue: Integer);
  524. begin
  525. WriteString(IntToStr(aValue));
  526. end;
  527. procedure TJSONSchemaWriterStream.WriteValue(aValue: Int64);
  528. begin
  529. WriteString(IntToStr(aValue));
  530. end;
  531. procedure TJSONSchemaWriterStream.WriteValue(aValue: Double);
  532. var
  533. s : String;
  534. begin
  535. Str(aValue,s);
  536. WriteString(S);
  537. end;
  538. procedure TJSONSchemaWriterStream.WriteValue(const aValue: String);
  539. begin
  540. WriteString('"'+StringToJSONString(aValue,StrictStrings)+'"');
  541. end;
  542. procedure TJSONSchemaWriterStream.StartProperty(const aName: string);
  543. begin
  544. if ElCount>0 then
  545. NextElement;
  546. WriteString('"'+StringToJSONString(aName,StrictStrings)+'":');
  547. IncElCount;
  548. end;
  549. procedure TJSONSchemaWriterStream.EndProperty;
  550. begin
  551. // Nothing
  552. end;
  553. procedure TJSONSchemaWriterStream.StartArray;
  554. begin
  555. WriteString('[');
  556. PushElCount;
  557. end;
  558. procedure TJSONSchemaWriterStream.EndArray;
  559. begin
  560. PopElCount;
  561. WriteString(']');
  562. end;
  563. procedure TJSONSchemaWriterStream.NextElement;
  564. begin
  565. if ElCount>0 then
  566. WriteString(',');
  567. IncElCount;
  568. end;
  569. procedure TJSONSchemaWriterStream.StartObject;
  570. begin
  571. WriteString('{');
  572. PushElCount;
  573. end;
  574. procedure TJSONSchemaWriterStream.EndObject;
  575. begin
  576. WriteString('}');
  577. PopElCount;
  578. end;
  579. end.