Author Topic: JSON routines  (Read 1249 times)

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 729
  • ObjAsm Developer
    • ObjAsm
JSON routines
« on: December 07, 2020, 08:16:05 AM »
Hi
JSON is a must have when dealing with servers or high-level languages.
I recently had to try my luck with Python, which has a large number of libraries, especially one for JSON.

To exchange data from my applications, I started writing my own JSON read/write routines.
There are many libraries out there, but I wanted to integrate the functionality seamlessly into my code.

My first attempt is a simple write routine (stringify) that is encapsulated in an object.
When I have a little more time, I will complete the read (parse) routine.
It's a work in progress.  :biggrin:

If anyone wants to try it, the source is attached.

Regards, Biterider
« Last Edit: December 14, 2020, 04:35:09 AM by Biterider »

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 729
  • ObjAsm Developer
    • ObjAsm
Re: JSON routines
« Reply #1 on: December 14, 2020, 04:33:13 AM »
Hi
Today I finished the JSON parser, which strictly follows the guidelines of https://www.json.org/.

The ANSI compilation target was a little tricky as the native JSON format is UTF8. The solution to this problem is to use wide strings internally, which are translated into the current CodePage at the end.
The WIDE compilation target was easier to implement thanks to the UTF8ToWide function.

I successfully tested the code on several JSON files that I found on the internet.

A note on numeric and boolean values: both are recognized when reading, but not translated into a value.
It is up to the host to interpret the string value and convert it to the desired format, e.g. BYTE, WORD, DWORD, QWORD, REAL4, REAL8 etc.

It remains to test the performance for the next few days, but I am confident that this is very acceptable.  :biggrin:


The source is attached to the first post.

Regards, Biterider

Biterider

  • Moderator
  • Member
  • *****
  • Posts: 729
  • ObjAsm Developer
    • ObjAsm
Re: JSON routines
« Reply #2 on: December 20, 2020, 01:00:01 AM »
Hi
Finally, I finished JSON support and added 3 routines for encoding and decoding JSON escape sequences.

JsonEscDecode: converts a string containing escape sequences into a plain wide string
JsonEscEncode: converts a wide string containing special characters into a JSON string
JsonEscEncodeSize: calculates the required byte size to allocate a JSON string.

Note: these routines are very similar to the percent encoding of URL strings https://en.wikipedia.org/wiki/Percent-encoding  :icon_idea:

Biterider


Biterider

  • Moderator
  • Member
  • *****
  • Posts: 729
  • ObjAsm Developer
    • ObjAsm
Re: JSON routines
« Reply #3 on: January 10, 2021, 05:50:53 PM »
Hi
I would like to post a link to a very handy tool written in asm by fearless to display the JSON information in the form of a tree structure.  :thumbsup:
It has already been mentioned a few times here in the forum, the direct link is:
https://github.com/mrfearless/cjsontree

Biterider


fearless

  • Member
  • ****
  • Posts: 516
    • Github
Re: JSON routines
« Reply #4 on: January 10, 2021, 08:13:29 PM »
Thanks Biterider,
Hopefully its useful to someone. It was similar to the xmltree thing that is on the forums somewhere, but more developed. Originally more as a proof of concept for using the cJSON library (https://github.com/DaveGamble/cJSON) but it grew over time with iterations to fix issues and to add in a few more features. Had plans to add some more stuff but my interest only carried it so far. I found using json preferable to xml as it seems less crowded, and doesn't use the tags syntax of xml (which can add to the size of the file/data).
fearless

Lian Li PC-O11DW Case, ASUS ROG Crosshair VIII Hero WiFi, AMD Ryzen 9 5950X, 32GB DDR4-3600MHz, MSI RX 5700XT Gaming X, NZXT Kraken Z73 360mm Cooler, Seasonic PRIME 1000W PSU

My Github  Twitter

caballero

  • Member
  • *****
  • Posts: 1617
  • Matrix - Noah
    • abre ojos ensamblador
Re: JSON routines
« Reply #5 on: January 10, 2021, 08:59:50 PM »
https://www.guru99.com/json-vs-xml-difference.html

JSON sample
Code: [Select]
{
  "student": [

     {
        "id":"01",
        "name": "Tom",
        "lastname": "Price"
     },

     {
        "id":"02",
        "name": "Nick",
        "lastname": "Thameson"
     }
  ]   
}

XML sample
Code: [Select]
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<student>
<id>01</id>
<name>Tom</name>
<lastname>Price</lastname>
</student>
<student>
<id>02</id>
<name>Nick</name>
<lastname>Thameson</lastname>
</student>
</root>

I am more used to working with xml, I did not know json, but it does not seem that there is much difference between one and the other. What I find interesting is that there seem to be nosql databases that use this structure to store data, which I have never seen and I don't find its ussefulness over sql db.

Quote
MongoDB is a document database, which means it stores data in JSON-like documents. We believe this is the most natural way to think about data, and is much more expressive and powerful than the traditional row/column model.

https://www.mongodb.com/


Quote
When people use the term “NoSQL database”, they typically use it to refer to any non-relational database. Some say the term “NoSQL” stands for “non SQL” while others say it stands for “not only SQL.” Either way, most agree that NoSQL databases are databases that store data in a format other than relational tables.

A common misconception is that NoSQL databases or non-relational databases don’t store relationship data well. NoSQL databases can store relationship data—they just store it differently than relational databases do. In fact, when compared with SQL databases, many find modeling relationship data in NoSQL databases to be easier than in SQL databases, because related data doesn’t have to be split between tables.

https://www.mongodb.com/nosql-explained


Ah, BTW, thank you for the code  :thumbsup:
« Last Edit: January 10, 2021, 10:35:07 PM by caballero »
The logic of the error is hidden among the most unexpected lines of the program