Recent

Author Topic: A new design for a JSON Parser  (Read 3079 times)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8143
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: A new design for a JSON Parser
« Reply #15 on: August 29, 2019, 02:15:34 am »
Code: Pascal  [Select]
  1. ...
  2. AnyNode.Find('search/for/name'); // returns a node 3 levels from the current node
  3. AnyNode.Find('/search/for/name'); // returns a node 3 levels from the root node
  4.  
I think I missed the difference between the two. From your example, you go for the second after setting N to stuff node and in this case, stuff node is the root? Then what is current node here? If instead you go for the first, what will you get?

sysrpl

  • Full Member
  • ***
  • Posts: 231
Re: A new design for a JSON Parser
« Reply #16 on: August 29, 2019, 06:02:41 am »
It the same as if you were typing a file system path. If your path string brings with a forward slash, then the path identities an item starting at the root of the files system. If it does not start with a forward slash, then the path evaluates from the current directory.

So for example:

NodeA.Find('/preferences/pallets/inspector/visible').AsBoolean;
NodeA.Find('visible').AsBoolean;

The first line would search the JSON starting at the root, even if NodeA is not the root.

The second line would search for an item called "visible" directly under NodeA.

So the way it works is you can Find from the root level at any node if the path string begins with "/". This is how files paths works, it's how XPath works, and I may eventually make Find support more of XPath.



Leledumbo

  • Hero Member
  • *****
  • Posts: 8143
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: A new design for a JSON Parser
« Reply #17 on: August 29, 2019, 01:01:04 pm »
It the same as if you were typing a file system path. If your path string brings with a forward slash, then the path identities an item starting at the root of the files system. If it does not start with a forward slash, then the path evaluates from the current directory.
If that's so, recalling your example:
Code: Pascal  [Select]
  1. N := N.Find('stuff');
  2. WriteLn(N.NodeByName['/search/for/name'].AsString);
  3.  
Shouldn't output "you've found me", instead 'search/for/name' or '/stuff/search/for/name' should, but the latter doesn't care whether N points to 'stuff' node or not. Am I right or something is missing here?

krexon

  • Jr. Member
  • **
  • Posts: 72
Re: A new design for a JSON Parser
« Reply #18 on: September 25, 2019, 11:05:48 am »
@sysrpl I use your parser, because fpJsonparser can't parse JSON, when there are duplicate keys.
Everything works fine, but ...

There is a key with price (always with 4 decimal places, but last 2 digits are zero), ie.
Code: Pascal  [Select]
  1. 'price1': 0.9900
  2. 'price2': 1.9900

I get this price using such code:
Code: Pascal  [Select]
  1. p1 := n.Find('price1').AsNumber // p1: double
  2. p2 := n.Find('price2').AsNumber // p2: double
  3. ShowMessage(FloatToStr(p1+p2));

At 2 computers (Windows 10) I didn't have any problems, but at one computer (Windows 10) sometimes above code shows 0 instead 2.98

I modified code to check if parser gets proper JSON value:
Code: Pascal  [Select]
  1. p1 := n.Find('price1').AsNumber // p1: double
  2. p2 := n.Find('price2').AsNumber // p2: double
  3. pj1 := n.Find('price1').AsJSON // pj1: string
  4. pj2 := n.Find('price2').AsJSON // pj2: string
  5. ShowMessage(FloatToStr(p1+p2) + LineEnding + p1 + LineEnding + p2));

Then it shows:
0
0.9900
1.9900
Everything works fine after restarting app. Problem occures again after some time :(
So It seems that getting value AsNumber is broken

lainz

  • Hero Member
  • *****
  • Posts: 3474
    • Lainz
Re: A new design for a JSON Parser
« Reply #19 on: December 10, 2019, 12:59:13 am »
Thanks for this Library.

A thing I found that maybe can be improved is 'AsString', maybe you can do some defaults when the value is Double? Like doing automatically value.ToString?

As well maybe you can provide AsInteger? Doing internally a trunc(value)...?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7944
Re: A new design for a JSON Parser
« Reply #20 on: December 10, 2019, 12:49:58 pm »
krexon: do those computers have different locale systems?

Hansaplast

  • Hero Member
  • *****
  • Posts: 577
  • Tweaking4All.com
    • Tweaking4All
Re: A new design for a JSON Parser
« Reply #21 on: December 23, 2019, 01:48:00 pm »
I know the FCL already has a capable JSON parser, but I am writing some Amazon web service interfacing projects and wanted a smaller easier to use JSON parser to assist. I've create a new design for a JSON parser that is pretty small, yet powerful.


I've been tinkering with several JSON parsers, and just wanted to express my gratitude for this fast and very easy to use parser. It works great for my purposes.

mboxmas

  • Newbie
  • Posts: 1
Re: A new design for a JSON Parser
« Reply #22 on: January 21, 2020, 07:48:43 pm »
Hi sysrpl,

For the following JSON data

{
    "attr1": "value1",
    "attr2": {
        "attr21": "value21",
        "attr22": "value22"
    }
}

why the construct

  for C in N do
    WriteLn(C.Value);
 
for attrib1 yields only the value, but for attr2 yields the attribute-value pair?
« Last Edit: January 21, 2020, 08:01:24 pm by mboxmas »

soerensen3

  • Full Member
  • ***
  • Posts: 183
Re: A new design for a JSON Parser
« Reply #23 on: January 21, 2020, 09:37:55 pm »
Is it just me or is your page getlazarus always redirecting to youtube?
Maybe the site has been hacked?
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

GAN

  • Sr. Member
  • ****
  • Posts: 265
Re: A new design for a JSON Parser
« Reply #24 on: January 21, 2020, 10:12:55 pm »
Is it just me or is your page getlazarus always redirecting to youtube?
Maybe the site has been hacked?

Yes, redirecting to youtube.
I checked the site:

Connecting to https://www.getlazarus.org
Exception: Unexpected response status code: 500
Status: 500
Lazarus 1.8.4 FPC 3.0.4 Linux Mint Mate 17.2 x86_64 GTK-2
Zeos 7.1.3 - Sqlite 3.8.2

Foro Lazarus en español http://forum.lazarus.freepascal.org/index.php/board,73.0.html

Hansaplast

  • Hero Member
  • *****
  • Posts: 577
  • Tweaking4All.com
    • Tweaking4All
Re: A new design for a JSON Parser
« Reply #25 on: January 22, 2020, 12:43:33 pm »
Redirection here as well - sure seems "hacked" ...


For anyone is interested (I hope sysrpl doesn't mind);
I still have a copy of the JSONTools source and the documentation that I had found on getlazarus.org (saved as RTF).
This is a copy of 12/23/2019.


I'm using this version in one of my recent projects to read JSON files - I love its simplicity and its very good performance.

Thaddy

  • Hero Member
  • *****
  • Posts: 9646
Re: A new design for a JSON Parser
« Reply #26 on: January 22, 2020, 01:54:24 pm »
getlazarus has never been an official source.
I am more like donkey than shrek

soerensen3

  • Full Member
  • ***
  • Posts: 183
Re: A new design for a JSON Parser
« Reply #27 on: January 27, 2020, 02:04:33 pm »
I really like it. Especially the possibility to walk nodes from child to parent and to use absolute paths is nice.
However I'm missing some features.

- You do not have the formatjson method to format the output with spaces which makes the output less readable.
- A GetJSON function would be nice which is trivial to implement.
- There is no possibility to add existing nodes to the tree (None I could find).

Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

lainz

  • Hero Member
  • *****
  • Posts: 3474
    • Lainz
Re: A new design for a JSON Parser
« Reply #28 on: January 27, 2020, 05:14:07 pm »
getlazarus has never been an official source.

Well, yes for jsontools that is what we're talking about

But the source is at github
https://github.com/sysrpl/JsonTools/blob/master/jsontools.pas