2

I created a simple test app in Delphi XE6, I want to start using Delphi's JSON to handle requests from a custom coded web server (one I am busy with)

procedure TForm1.Button1Click(Sender: TObject);
var
  Servermethods : TServerMethods1Client;
  JsonArray : TJSONArray;
  JsonValue: TJSONValue;
  JSonObj: TJSONObject;
  JSPair: TJSONPair;
  s: String;
begin
  JSonObj := TJSONObject.Create;
  JSonObj.AddPair(TJSONPair.Create('a','abcde'));
  JsonArray := TJSONArray.Create;
  JsonArray.AddElement(JSonObj);

  JsonValue := JsonArray.Items[0];
  JSonObj := (JsonValue as TJSONObject);

  JSPair := TJSONPair(JSonObj);

  s := JSPair.JsonString.Value;
end;

When you Inspect/Evaluate any JSON object (CTRL+F7) there is simply no info, in the inspector I simply get "()" -[aka empty]-

But for arguments sake if I change the call to JSONObj.Tostring I get the full json string ({"a", "abcde"}) and thats cool thats cool but when it gets to the last line

s := JSPair.JsonString.Value;

Boom! Access Violation.

Any help would be appreciated

* Edit * What I'm actually asking i for someone to test this in XE6 to see if it's a bug? or am I missing something very obvious?

Ryno Coetzee
  • 516
  • 4
  • 13
  • Probably because JSPair is not set. – Uwe Raabe May 06 '14 at 19:31
  • Hi, thanks for the reply - The Pair is now part of the object - you'll see that JSonObj.AddPair(TJSONPair.Create('a','abcde')) so the pairs parent is the JSonObj ... IE it exists, you should then be able to access the pair Via the JsonObj like so (s := TJSONPair(JSonObj).JsonString.Value;) – Ryno Coetzee May 06 '14 at 19:34
  • 2
    But the variable JSPair is never set, so it contains mostly garbage. – Uwe Raabe May 06 '14 at 19:35
  • Hi - Sorry, I saw that small oversight, thanks for pointing it out, fixed it and amended this post but still - Access Violation – Ryno Coetzee May 06 '14 at 19:41

2 Answers2

2

The JSONPair is part of the JSONObject, so you cannot cast the JSONObject to a JSONPair. Use JSONObject.Get to get the pair.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • Thank you! I maybe looked at that method 100 times without registering the return value of the TJSONOBJECT.Get method was it's TJSONPair – Ryno Coetzee May 06 '14 at 20:09
0

It seems to me that you are also casting a TJSONValue to a TJSONPair.

The last few lines assign a standalone TJSONValue object to JSONObj. Then that is cast to a JSONPair. (in the unsafe way - there is no runtime type compatiblity check then). I think a value here is just a member of a pair and is not compatible with that otherwise.

You could try using "as" operator in that case too, might make discovering the features a lot easier for you.

I.e.

JSPair := JSonObj as TJSONPair;

should complain for the typecasting issue when run.

It would also tell if I am right to say these classes are really sitting on different "benches" of the class inheritance tree, and hence your access violation.

brezniczky
  • 492
  • 3
  • 10
  • Hi, Thank you for your answer, you where right about the typecasting and the inheritance hierarchy however @Uwe Raabe's answer was the one i'm looking for – Ryno Coetzee May 06 '14 at 20:07