Closed totyaxy closed 1 year ago
Hi!
Good idea!
I'll implement these Check()
variations into next commit.
Thanks!
Thank you!
However, then the solution could be a bit more multifaceted, there could also be a LastErrorPos public variable. So the simple boolean version of Check could also be used. But I thought about this basically because during normal loading (LoadFromFile) an exception with the error position actually comes up, but it would be good to know the exact error position there as well. It would be good to process the error position programmatically, because "4500 error pos" does not say much to the user working in json. However, based on the position, I can show him a detail from the file (string) where the error is (part of the string that he can search for, possibly the line number).
These are only suggestions.
You can even the hand over the error in the exception (I'm sure you know how to do it, just an example, and I think the LastErrorPos class variable is simpler solution):
type
EMcJsonException = class(Exception)
public
constructor Create(const msg: string; const aLastErrorPos: integer = -1);
private
FLastErrorPos: integer;
public
property LastErrorPos: integer read FLastErrorPos;
end;
constructor EMcJsonException.Create(const msg: string; const aLastErrorPos: integer = -1);
begin
inherited Create(msg);
FLastErrorPos := aLastErrorPos;
end;
usage:
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
try
raise EMcJsonException.Create('Damaged json', 1200);
except
on E: EMcJsonException do
begin
s := '"%s" Bad line count: "%d"';
ShowMessage(Format(s, [E.Message, E.LastErrorPos]));
end;
end;
end;
Hi! Good idea! I'll implement these
Check()
variations into next commit. Thanks!
Hi!
I didn't see it in the next version, but it would be nice, indeed, thanks :)
H!
Thank you, I made a mistake in the json database file, and got an exception:
Error while parsing text: "line break" at pos "1051"
It is good. :) Because In my current use, the most common error is when the json structure is damaged.
The next step, if I got the bad number pos directly, whithout I need to parse(?) the E.Message (E.LastErrorPos see above?). So it should be separated from the error message, which could be a separate TException anyway.
Now SParsingErrors there are:
Thank you!
I temporarily solved the error position in the following way:
try
Result := Json.CheckException(JsonStr, True);
except
on E: Exception do
begin
Memo.Write('...Json database INVALID!');
Memo.Write('Json library error message: '+E.Message);
if UTF8Pos('at pos', E.Message)>0 then
begin
Memo.Write('');
Memo.Write('You can jump to the error pos for example in Notepad++ : Search/Goto/Offset (Ctrl-G)');
end;
end;
end;
Nice solution.
"temporarily" solution :)
How much nicer would this be for example (see above):
on E: EMcJsonDamagedStructureException do
begin
Memo.Write('Json damaged, positon: '+E.DamagedStructurePos');
end;
Hi!
I would like to use the check function, but the problem is that it hides the error position (without debugger).
Idea, integer result:
and then the original function stay, and use it:
... or other way, if you have time.