jsonpack.lpr 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. program jsonpack;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}
  5. cthreads,
  6. {$ENDIF}
  7. SysUtils,
  8. Classes,
  9. JsonParser,
  10. fpJson;
  11. var
  12. AFileName: String;
  13. AConfig: TJSONData;
  14. AStream: TFileStream;
  15. AOptions: TFormatOptions;
  16. begin
  17. if ParamStr(1) = '-c' then
  18. begin
  19. AOptions:= AsCompactJSON;
  20. end
  21. else if ParamStr(1) = '-d' then
  22. begin
  23. AOptions:= [foDoNotQuoteMembers]
  24. end
  25. else begin
  26. WriteLn;
  27. WriteLn(ExtractFileName(ParamStr(0)), ' <Options> <json-file>');
  28. WriteLn;
  29. WriteLn('Options:');
  30. WriteLn(' -c compress json-file');
  31. WriteLn(' -d decompress json-file');
  32. WriteLn;
  33. Exit;
  34. end;
  35. AFileName:= ParamStr(2);
  36. AStream:= TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
  37. try
  38. AConfig:= GetJSON(AStream, True);
  39. finally
  40. AStream.Free;
  41. end;
  42. try
  43. with TStringList.Create do
  44. try
  45. Text:= AConfig.FormatJSON(AOptions);
  46. SaveToFile(AFileName);
  47. finally
  48. Free;
  49. end;
  50. finally
  51. AConfig.Free;
  52. end;
  53. end.