hydrobyte / McJSON

A Delphi / Lazarus / C++Builder simple and small class for fast JSON parsing.
MIT License
58 stars 20 forks source link

Excepted got "/" (Comment support) #11

Closed totyaxy closed 1 year ago

totyaxy commented 1 year ago

In principle, the JSON5 standard: https://spec.json5.org/#comments Of course, one could argue about how standard it is, but for example Json.NET supports it: https://www.newtonsoft.com/json I don't think it's too complicated to implement, you just have to ignore it when reading:

// This is a single line comment.

/ This is a multi- line comment. /

hydrobyte commented 1 year ago

Hi,

Could you check if fcl-json supports comments?

In my initial research, this was discarded because Delphi's System.JSON does not support comments.

totyaxy commented 1 year ago

Hi!

I don't think so, and I really don't, it throws an error:

uses fpjson, jsonparser;

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject); var jData : TJSONData; begin jData := GetJSON('//'); end;

hydrobyte commented 1 year ago

It seems comments are controversial already.

On the other hand, I think you can parse and exclude comments before using McJSON or any other library.

Regards,

totyaxy commented 1 year ago

I think it is very easy to identify the two types of comments, and the library would not throw errors. But I understand your point and thanks for the reply!

hydrobyte commented 1 year ago

I think I can create a new method called readComment() and then jump.

What complicates it is that comments could occur anywhere, so I have to check how many calls of this new method would be needed and also how to handle syntax errors due to comments.

I think that's why many libraries leave this aspect out.

hydrobyte commented 1 year ago

Just one last question:

Does Lazarus' fcl-json not meet your needs? I'm curious why McJSON seems more important to you than a native library.

totyaxy commented 1 year ago

I thought you were reading lines when reading from a file. Only the first two characters should be examined. It's true that a row can contain multiple keys and values, but I don't think the comment makes sense within a row. Of course, you have a better understanding of how your code works.

"Does Lazarus' fcl-json not meet your needs? I'm curious why McJSON seems more important to you than a native library."

I process around 1000 json files with tens of thousands of keys, speed is also important, and I saw the speed test that this library is very good (see readme) and fully supports Lazarus, so why not use it? :)

hydrobyte commented 1 year ago

Nice!

Actually it reads the file as a string and them it parses this string.

I'm trying to keep the code simple and fast for relative small files. It is nice to see that McJSON performance is important to you.

On the other hand, as you shown in JSON5, comments can appear inside any JSON token, so it is a little more complicated to implement.

But I'll try it soon.

Regards,

hydrobyte commented 1 year ago

Actually, after some reading:

So, I'll close this issue.

Thanks.

totyaxy commented 1 year ago

Okay, but see the my first comment. :) Thanks for taking care of it!

hydrobyte commented 1 year ago

I saw your first comment, but I recommend you see this.