fpjsonvalue.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. {
  2. This file is part of the Free Component Library
  3. JSON Data structure to TValue conversion
  4. Copyright (c) 2022 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 fpjsonvalue;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. fpjson, rtti;
  16. function ValueToJSON(const aValue: TValue; aType: TRttiType): TJSONData;
  17. function JSONToValue(aData: TJSONData; aType: TRttiType): TValue;
  18. Implementation
  19. uses typinfo;
  20. function ValueToJSON(const aValue: TValue; aType: TRttiType): TJSONData;
  21. var
  22. i: SizeInt;
  23. at: TRttiDynamicArrayType;
  24. begin
  25. Result := Nil;
  26. try
  27. case aType.TypeKind of
  28. tkAString,
  29. tkUString,
  30. tkWString,
  31. tkSString:
  32. Result := TJSONString.Create(aValue.AsUnicodeString);
  33. tkInteger:
  34. Result := TJSONIntegerNumber.Create(aValue.AsInteger);
  35. tkInt64,
  36. tkQWord:
  37. Result := TJSONInt64Number.Create(aValue.AsInt64);
  38. tkBool:
  39. Result := TJSONBoolean.Create(aValue.AsBoolean);
  40. tkDynArray: begin
  41. Result := TJSONArray.Create;
  42. at := aType as TRttiDynamicArrayType;
  43. for i := 0 to aValue.GetArrayLength - 1 do
  44. TJSONArray(Result).Add(ValueToJSON(aValue.GetArrayElement(i), at.ElementType));
  45. end;
  46. { ToDo: further types }
  47. end;
  48. except
  49. Result.Free;
  50. Raise;
  51. end;
  52. end;
  53. function JSONToValue(aData: TJSONData; aType: TRttiType): TValue;
  54. var
  55. _as: AnsiString;
  56. us: UnicodeString;
  57. i: Integer;
  58. i64: Int64;
  59. b: Boolean;
  60. arr: TJSONArray;
  61. varr: array of TValue;
  62. at: TRttiDynamicArrayType;
  63. begin
  64. varr:=[];
  65. Result := TValue.Empty;
  66. case aType.TypeKind of
  67. tkAString: begin
  68. _as := aData.AsString;
  69. TValue.Make(@_as, PTypeInfo(aType.Handle), Result);
  70. end;
  71. tkUString: begin
  72. us := aData.AsUnicodeString;
  73. TValue.Make(@us, PTypeInfo(aType.Handle), Result);
  74. end;
  75. tkInteger: begin
  76. i := aData.AsInteger;
  77. TValue.Make(@i, PTypeInfo(aType.Handle), Result);
  78. end;
  79. tkInt64,
  80. tkQWord: begin
  81. i64 := aData.AsInt64;
  82. TValue.Make(@i64, PTypeInfo(aType.Handle), Result);
  83. end;
  84. tkBool: begin
  85. b := aData.AsBoolean;
  86. TValue.Make(@b, PTypeInfo(aType.Handle), Result);
  87. end;
  88. tkDynArray: begin
  89. arr := aData as TJSONArray;
  90. at := aType as TRttiDynamicArrayType;
  91. SetLength(varr, arr.Count);
  92. for i := 0 to High(varr) do
  93. varr[i] := JSONToValue(arr[i], at.ElementType);
  94. Result := TValue.FromArray(aType.Handle, varr);
  95. end;
  96. { ToDo: further types }
  97. end;
  98. end;
  99. end.