Browse Source

* Allow BOM check

git-svn-id: trunk@48926 -
michael 4 years ago
parent
commit
b4d37f78ac
2 changed files with 18 additions and 2 deletions
  1. 1 1
      packages/fcl-json/src/jsonconf.pp
  2. 17 1
      packages/fcl-json/src/jsonscanner.pp

+ 1 - 1
packages/fcl-json/src/jsonconf.pp

@@ -31,7 +31,7 @@ uses
   SysUtils, Classes, fpjson, jsonscanner, jsonparser;
 
 Const
-  DefaultJSONOptions = [joUTF8,joComments];
+  DefaultJSONOptions = [joUTF8,joComments,joBOMCheck];
 
 type
   EJSONConfigError = class(Exception);

+ 17 - 1
packages/fcl-json/src/jsonscanner.pp

@@ -51,7 +51,7 @@ type
 
   EScannerError = class(EParserError);
 
-  TJSONOption = (joUTF8,joStrict,joComments,joIgnoreTrailingComma,joIgnoreDuplicates);
+  TJSONOption = (joUTF8,joStrict,joComments,joIgnoreTrailingComma,joIgnoreDuplicates,joBOMCheck);
   TJSONOptions = set of TJSONOption;
 
 Const
@@ -141,10 +141,26 @@ end;
 
 constructor TJSONScanner.Create(Source: TStream; AOptions: TJSONOptions);
 
+  procedure SkipStreamBOM;
+  Var
+    OldPos : integer;
+    Header : array[0..3] of byte;
+  begin
+    OldPos := Source.Position;
+    FillChar(Header, SizeOf(Header), 0);
+    if Source.Read(Header, 3) = 3 then
+      if (Header[0]=$EF) and (Header[1]=$BB) and (Header[2]=$BF) then
+        exit;
+    Source.Position := OldPos;
+  end;
+
+
 Var
   S : RawByteString;
 
 begin
+  if (joBOMCheck in aOptions) then
+    SkipStreamBom;
   S:='';
   SetLength(S,Source.Size-Source.Position);
   if Length(S)>0 then