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