I'm using C++Builder XE6 and want to get the value part of some key/value pairs in JSON like this:
#include <System.JSON.hpp>
TJSONValue* Root = TJSONObject::ParseJSONValue("{\"MyString\":\"ABC\",\"MyBool\":true,\"MyObject\":{}}");
try
{
String MyString = Root->GetValue<String>("MyString", String());
bool MyBool = Root->GetValue<bool>("MyBool", false);
TJSONObject* MyObject = Root->GetValue<TJSONObject*>("MyObject", NULL);
}
__finally
{
delete Root;
}
The code compiles, but I get the following linker errors:
[ilink32 Error] Error: Unresolved external 'System::UnicodeString __fastcall System::Json::TJSONValue::GetValue<System::UnicodeString>(const System::UnicodeString, System::UnicodeString)' referenced from ...
[ilink32 Error] Error: Unresolved external 'bool __fastcall System::Json::TJSONValue::GetValue<bool>(const System::UnicodeString, bool)' referenced from ...
[ilink32 Error] Error: Unresolved external 'System::Json::TJSONObject * __fastcall System::Json::TJSONValue::GetValue<System::Json::TJSONObject *>(const System::UnicodeString, System::Json::TJSONObject *)' referenced from ...
Any idea what I'm doing wrong?
Update
For now, I decided to write some wrapper functions:
unit JSONUtils;
interface
uses
System.JSON;
function GetJSONString (Value: TJSONValue; Path: string; Default: string = ''): string;
function GetJSONBool (Value: TJSONValue; Path: string; Default: Boolean = False): Boolean;
function GetJSONObject (Value: TJSONValue; Path: string; Default: TJSONObject = nil): TJSONObject;
implementation
function GetJSONString (Value: TJSONValue; Path: string; Default: string): string;
begin
Result := Value.GetValue<string>(Path, Default);
end;
function GetJSONBool (Value: TJSONValue; Path: string; Default: Boolean): Boolean;
begin
Result := Value.GetValue<Boolean>(Path, Default);
end;
function GetJSONObject (Value: TJSONValue; Path: string; Default: TJSONObject): TJSONObject;
begin
Result := Value.GetValue<TJSONObject>(Path, Default);
end;
end.
I'm still getting the linker errors if I try to use the Delphi generics in C++, but at least the wrapper functions work.