fpjsonjs.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. {
  2. This file is part of the Free Component Library
  3. Use native parser for parsing JSON Data structures
  4. Copyright (c) 2020 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit fpjsonjs;
  12. {$mode objfpc}
  13. interface
  14. uses
  15. Classes, Types, fpjson;
  16. Function JSValueToJSONData(aValue : JSValue) : TJSONData;
  17. Procedure HookGetJSONCallBack;
  18. Procedure UnHookGetJSONCallBack;
  19. implementation
  20. Uses JS;
  21. function JSValueToJSONData(aValue: JSValue): TJSONData;
  22. Var
  23. Obj: TJSObject Absolute aValue;
  24. v : JSValue;
  25. S : String;
  26. begin
  27. Case GetValueType(aValue) of
  28. jvtNull : Result:=CreateJSON;
  29. jvtBoolean : Result:=CreateJSON(Boolean(aValue));
  30. jvtString : Result:=CreateJSON(String(aValue));
  31. jvtFloat : Result:=CreateJSON(TJSONFloat(aValue));
  32. jvtInteger:
  33. if (NativeInt(aValue)>Maxint) or (NativeInt(aValue)<-MaxInt) then
  34. Result:=CreateJSON(NativeInt(aValue))
  35. else
  36. Result:=CreateJSON(NativeInt(aValue));
  37. jvtArray :
  38. begin
  39. Result:=CreateJSONArray([]);
  40. for v in TJSValueDynArray(aValue) do
  41. TJSONArray(Result).Add(JSValueToJSONData(v));
  42. end;
  43. jvtObject :
  44. begin
  45. Result:=CreateJSONObject(Nil);
  46. For S in TJSObject.getOwnPropertyNames(Obj) do
  47. TJSOnObject(Result).Add(S,JSValueToJSONData(Obj.Properties[S]));
  48. end;
  49. end;
  50. end;
  51. Procedure JSONFromString(Const aJSON : TJSONStringType; Const AUseUTF8 : Boolean; Out Data : TJSONData);
  52. Var
  53. Msg : String;
  54. aValue : JSValue;
  55. begin
  56. msg:='';
  57. try
  58. aValue:=TJSJSON.Parse(aJSON);
  59. except
  60. On ES : TJSSyntaxError do
  61. Msg:=ES.message;
  62. on E : TJSError do
  63. Msg:=E.Message ;
  64. on O : TJSObject do
  65. Msg:='Unknown error : '+TJSJSON.stringify(O);
  66. else
  67. asm
  68. var b = new SyntaxError;
  69. console.log(SyntaxError.prototype.isPrototypeOf(b));
  70. if ($e.hasOwnProperty('message')) {
  71. Msg = '' || $e.message;
  72. }
  73. end;
  74. end;
  75. if Msg<>'' then
  76. Raise EJSON.Create('Error parsing JSON: '+Msg);
  77. // We do this outside the try..except so that in case of errors, we get the original exception.
  78. Data:=JSValueToJSONData(aValue);
  79. end;
  80. Procedure JSONFromStream(AStream : TStream; Const AUseUTF8 : Boolean; Out Data : TJSONData);
  81. Var
  82. SS : TStringStream;
  83. begin
  84. SS:=TStringStream.Create('');
  85. try
  86. SS.CopyFrom(aStream,0);
  87. JSONFromString(SS.DataString,False,Data);
  88. finally
  89. SS.Free;
  90. end;
  91. end;
  92. procedure HookGetJSONCallBack;
  93. begin
  94. SetJSONParserHandler(@JSONFromStream);
  95. SetJSONStringParserHandler(@JSONFromString);
  96. end;
  97. Procedure UnHookGetJSONCallBack;
  98. begin
  99. SetJSONParserHandler(Nil);
  100. SetJSONStringParserHandler(Nil);
  101. end;
  102. initialization
  103. HookGetJSONCallBack;
  104. end.