#1 2016-01-28 10:50:24

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

Access violation when calling JsonToObject

Hello,

I'm getting an Access violation when calling JsonToObject.
Exception raise at Unit SynCommons, at function GetJSONField(P: PUTF8Char; out PDest: PUTF8Char; wasString: PBoolean=nil; EndOfObject: PUTF8Char=nil): PUTF8Char;
Line 43205 :         D^ := P^; // 3 stages pipelined process of unescaped chars

Code is really basic :

  TCRMUser = class(TPersistent)
  private
    FAdmin: Boolean;
    FFdId: Double;
    FId: Double;
    FName: RawUTF8;
    FSocietes: TArray<RawUTF8>;
    FUser: RawUTF8;
    // public

  published
    property admin: Boolean read FAdmin write FAdmin;
    property fdId: Double read FFdId write FFdId;
    property id: Double read FId write FId;
    property name: RawUTF8 read FName write FName;
    property societes: TArray<RawUTF8> read FSocietes write FSocietes;
    property User: RawUTF8 read FUser write FUser;
  end;
var
  CRMUser: TCRMUser;
  JSON: RawUTF8;
  IsValidJson: Boolean;
begin
  TJSONSerializer.RegisterClassForJSON([TCRMUser]);
  CRMUser := TCRMUser.Create;
  try
    JSON := '{"admin":false,"fdId":37,"id":17,"name":"Jean-Pierre LeGros","societes":["DODO","DROUAULT"],"User":"jplegros"}';
    JSONToObject(CRMUser, PUTF8Char(JSON), IsValidJson);
    with Memo1 do
    begin
      clear;
      Lines.Add(CRMUser.User);
    end;
  finally
    CRMUser.Free;
  end;
end;

Last edited by swierzbicki (2016-01-28 10:50:57)

Offline

#2 2016-01-28 11:20:16

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,539
Website

Re: Access violation when calling JsonToObject

Try it without generic. Replace TArray<RawUTF8> -> TRawUTF8DynArray

Offline

#3 2016-01-28 11:57:55

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

Re: Access violation when calling JsonToObject

@mpv : doesn't helps.

Offline

#4 2016-01-28 12:05:50

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

Re: Access violation when calling JsonToObject

Declaring JSON outside the procedure solved the problem. Is this expected ?

Offline

#5 2016-01-28 12:12:19

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,539
Website

Re: Access violation when calling JsonToObject

I test both variants (TArray & TRawUTF8DynArray) using XE2 - all work as expected.
Are you sure you are on the last framework version? Which version of Delphi you are using?

Offline

#6 2016-01-28 12:59:17

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

Re: Access violation when calling JsonToObject

I'm working with the trunk version (did a GIT clone this morning).
I'm using Delphi XE 10 Seattle Update 1. Issue on both x32 and x64

Offline

#7 2016-01-28 13:20:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: Access violation when calling JsonToObject

The JSON is a constant, so is stored directly in the executable.
JSONToObject() is doing in-place parsing (to avoid memory allocation), so it WRITES its input buffer.
Since the input buffer is JSON, it is a read-only memory place.
So the Access Violation.

Just force the creation of an intermediate copy:

JSONToObject(CRMUser, @JSON[1], IsValidJson);

Offline

Board footer

Powered by FluxBB