ahausladen / JsonDataObjects

JSON parser for Delphi 2009 and newer
MIT License
413 stars 160 forks source link

Enumerators #1

Closed MvRens closed 9 years ago

MvRens commented 9 years ago

Hi Andreas,

First of all, thanks for releasing this library! I very much appreciate the clean code and it's ease of use.

I was using superobject before, and since I like the way the for..in construct cleans up loops I added support to both TJsonArray and TJsonObject. I'd be interested in your opinion on these changes.

Included in these commits is a fix for the failing unit tests as well, if you hadn't changed that already. I added it to this branch to be sure the unit tests for the enumerators were running correctly.

Cheers, Mark

ahausladen commented 9 years ago

PChar minus PChar returns the length, not the byte size. So with your extra "div SizeOf(Char)" it becomes a "div 4" and my unit tests fail. What special Delphi version did you use where the "div SizeOf(Char)" was necessary?

ahausladen commented 9 years ago

You found a compiler bug. XE7 generates the correct code with the "div 2" but XE2 doesn't.

MvRens commented 9 years ago

I'm on XE2 (Update 4 Hotfix 1)... and while I'm typing this, your comment pops in :) Well, that explains it!

I did test it in a small console application without the casts, and that seemed to work though, returning "6":

var
  value: string;
  p1: PChar;
  p2: PChar;

begin
  value := 'Hello world!';
  p1 := PChar(value);
  p2 := p1;
  Inc(p2, 6);

  WriteLn(p1); // Hello world!
  WriteLn(p2); // world!
  WriteLn(p2 - p1); // 6
end;
MvRens commented 9 years ago

With an extra variable, it works in XE2:

        E := @PChar(@Buffer[0])[Length(Buffer)];
        P := IntToText(FValue.I, E);

        Writer.AppendValue(P, E - P);
ahausladen commented 9 years ago

An extra typecast from implicit PChar to explicit PChar worked.

MvRens commented 9 years ago

Nice!