JsonDocument.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. unit JsonDocument;
  2. interface
  3. uses
  4. System.Classes, System.JSON, System.SysUtils, System.IOUtils;
  5. type
  6. TOnFilenameChanged = reference to procedure(ASender: TObject);
  7. TJsonDocument = class
  8. private
  9. FFilename: string;
  10. FLastWriteTime: TDateTime;
  11. FJsonRoot: TJsonValue;
  12. FOnFilenameChanged: TOnFilenameChanged;
  13. procedure SetJsonRoot(const AValue: TJsonValue);
  14. procedure SetFilename(const AValue: string);
  15. public
  16. property Filename: string read FFilename write SetFilename;
  17. property LastWriteTime: TDateTime read FLastWriteTime;
  18. property JsonRoot: TJsonValue read FJsonRoot write SetJsonRoot;
  19. property OnFilenameChanged: TOnFilenameChanged read FOnFilenameChanged write FOnFilenameChanged;
  20. public
  21. constructor Create;
  22. destructor Destroy; override;
  23. procedure New(AValue: TJsonValue = nil);
  24. function Open(const AFileName: string; AUseBool: Boolean = False; ARaiseExc: Boolean = False): Boolean;
  25. procedure Save(const AFileName: string; AIndentation: Integer = 4; AEncodeBelow32: Boolean = True; AEncodeAbove127: Boolean = False; AWriteBOM: Boolean = False);
  26. end;
  27. implementation
  28. uses Logger, jsonhelper, pub;
  29. constructor TJsonDocument.Create;
  30. begin
  31. inherited;
  32. FJsonRoot := nil;
  33. FFilename := '';
  34. end;
  35. destructor TJsonDocument.Destroy;
  36. begin
  37. FJsonRoot.Free;
  38. inherited;
  39. end;
  40. procedure TJsonDocument.SetJsonRoot(const AValue: TJsonValue);
  41. begin
  42. if FJsonRoot = AValue then
  43. Exit;
  44. FJsonRoot.Free;
  45. FJsonRoot := AValue;
  46. end;
  47. procedure TJsonDocument.SetFilename(const AValue: string);
  48. begin
  49. //if FFilename = AValue then
  50. // Exit;
  51. FFilename := AValue;
  52. if FFilename <> '' then
  53. begin
  54. for var i := g_pub.Config['recent'].Count - 1 downto 0 do
  55. begin
  56. if (i > 10) or (g_pub.Config['recent'][i] = FFilename) then
  57. begin
  58. g_pub.Config['recent'][i].Delete;
  59. end;
  60. end;
  61. g_pub.Config['recent'].Add(FFilename);
  62. g_pub.Config['recent'].LastItem.MoveToFirst;
  63. g_pub.SaveConfig;
  64. end;
  65. if Assigned(FOnFilenameChanged) then
  66. FOnFilenameChanged(Self);
  67. end;
  68. procedure TJsonDocument.New(AValue: TJsonValue = nil);
  69. begin
  70. if AValue = nil then
  71. JsonRoot := TJSONObject.Create
  72. else
  73. JsonRoot := AValue;
  74. Filename := '';
  75. end;
  76. function TJsonDocument.Open(const AFileName: string; AUseBool: Boolean = False; ARaiseExc: Boolean = False): Boolean;
  77. var
  78. LValue: TJsonValue;
  79. begin
  80. g_Logger.Info('Open File: ' + AFileName);
  81. Result := False;
  82. try
  83. LValue := TJsonValue.ParseJSONValue(TFile.ReadAllText(AFileName, TEncoding.UTF8), AUseBool, True);
  84. if LValue = nil then
  85. begin
  86. Exit;
  87. end;
  88. Filename := AFileName;
  89. JsonRoot := LValue;
  90. FLastWriteTime := TFile.GetLastWriteTime(FFilename);
  91. Result := True;
  92. except
  93. on E: Exception do
  94. begin
  95. g_Logger.Error('fail to open file: ' + E.Message);
  96. if ARaiseExc then
  97. begin
  98. raise Exception.Create(E.Message);
  99. end;
  100. end;
  101. end;
  102. end;
  103. procedure TJsonDocument.Save(const AFileName: string; AIndentation: Integer = 4; AEncodeBelow32: Boolean = True; AEncodeAbove127: Boolean = False; AWriteBOM: Boolean = False);
  104. var
  105. strs: TStrings;
  106. begin
  107. g_Logger.Info('Save File: ' + AFileName);
  108. strs := TStringList.Create;
  109. try
  110. strs.WriteBOM := AWriteBOM;
  111. strs.Text := JSONFormat(FJsonRoot, AIndentation, AEncodeBelow32, AEncodeAbove127);
  112. strs.SaveToFile(AFileName, TEncoding.UTF8);
  113. Filename := AFileName;
  114. finally
  115. strs.Free;
  116. end;
  117. end;
  118. end.