Browse Source

fcl-json: fixed StringToJSONString

mattias 3 years ago
parent
commit
5700cb56ac

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

@@ -804,7 +804,7 @@ implementation
 Uses typinfo;
 
 const
-  HexDigits = '01234567890ABCDEF';
+  HexDigits: array[0..15] of char = '0123456789ABCDEF';
 
 Resourcestring
   SErrCannotConvertFromNull = 'Cannot convert data from Null value';

+ 3 - 0
packages/fcl-json/tests/jsonconftest.pas

@@ -310,6 +310,9 @@ begin
     C.SetValue('a','abc');
     C.GetValue('a',L,'');
     AssertStrings('String',L,['abc']);
+    C.SetValue('a','a'#1#2);
+    C.GetValue('a',L,'');
+    AssertStrings('String',L,['a'#1#2]);
     C.SetValue('a',Integer(1));
     C.GetValue('a',L,'');
     AssertStrings('integer',L,['1']);

+ 32 - 0
packages/fcl-json/tests/testjsondata.pas

@@ -177,6 +177,7 @@ type
     procedure TestCreateBoolean;
     procedure TestCreateObject;
     procedure TestCreateJSONString;
+    procedure TestCreateJSONStringSpecialChars;
     procedure TestCreateJSONObject;
     procedure TestCreateNilPointer;
     procedure TestCreatePointer;
@@ -2394,6 +2395,37 @@ begin
   end;
 end;
 
+procedure TTestArray.TestCreateJSONStringSpecialChars;
+const
+  S: array[0..7] of string = (
+    'A'#1,
+    'B'#9,
+    'C'#10,
+    'D'#12,
+    'E'#13,
+    'F'#10#13,
+    'G"Foo"',
+    'H\J');
+Var
+  J : TJSONArray;
+  i: Integer;
+
+begin
+  J:=TJSonArray.Create;
+  try
+    for i:=0 to high(S) do
+      J.Add(S[i]);
+    TestItemCount(J,length(S));
+    for i:=0 to high(S) do
+      begin
+      TestJSONType(J[i],jtString);
+      end;
+    TestJSON(J,'["A\u0001", "B\t", "C\n", "D\f", "E\r", "F\n\r", "G\"Foo\"", "H\\J"]');
+  finally
+    FreeAndNil(J);
+  end;
+end;
+
 procedure TTestArray.TestCreateObject;
 
 Var