yaml2json.pp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  4. YAML-To-JSON conversion demo.
  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. {$mode objfpc}
  12. {$h+}
  13. program yaml2json;
  14. uses sysutils, fpyaml.data, fpyaml.parser, fpyaml.json, fpjson;
  15. var
  16. YAML : TYAMLStream;
  17. JSON : TJSONData;
  18. MiniMal : Boolean;
  19. begin
  20. JSON:=Nil;
  21. YAML:=nil;
  22. Minimal:=ParamStr(1)='-m';
  23. if (Paramstr(1+Ord(Minimal))='') or (ParamStr(1)='-h') then
  24. begin
  25. Writeln('Usage: ',ExtractFilePath(ParamStr(0)),' [-m] inputfile');
  26. Writeln('-m : minimal output. Default is to format output.');
  27. Halt(Ord(ParamStr(1)<>'-h'));
  28. end;
  29. With TYAMLParser.Create(Paramstr(1+Ord(Minimal))) do
  30. try
  31. YAML:=Parse;
  32. finally
  33. Free;
  34. end;
  35. try
  36. JSON:=YAMLToJSON(YAML);
  37. if Minimal then
  38. Writeln(JSON.AsJSON)
  39. else
  40. Writeln(JSON.FormatJSON());
  41. finally
  42. YAML.Free;
  43. JSON.Free;
  44. end;
  45. end.