JSON library (0.20.2)

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Re: JSON library (0.12)

Post by AGS »

There is an error in my previous post.

According to the json standard a - in front of a number is allowed while a + isn't.
The example should have looked like this

Code: Select all

{"key":-4.44}
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.12)

Post by StringEpsilon »

Fixed.

I also hacked in support for flat values. It's ugly and not perfect ("null{" will not be malformed), but it's certainly better than before.
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Post by StringEpsilon »

Bump: Been a while, but I just got in the mood again and updated fbJSON today.

Since my last comment, the following changed:

* Switched to MPL 2.0
* Support for deescapting \uXXXX-notation
* Implemented UTF-8 validation
* Way better errorhandling
* Some performance enhancements here and there.
* Lots of refactoring.

Latest version is 0.17

If someone is willing to test the spec-compliance, I'd be very grateful. I actually don't have any projects that need a JSON library, so my testing is a bit limited.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: JSON library (0.17)

Post by D.J.Peters »

Currently I don't need it but i'm sure in future.

It's good to know we have a solution for JSON.

Thank you for the update or upgrade.

Joshy
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

Thank you for that new release.

I try to read a JSON file but until now I have no luck. Could you give it a look?

Here is the content of the file engines.json.

[
{
"command" : "fruit_21.exe",
"name" : "Fruit",
"protocol" : "uci",
"workingDirectory" : "c:\\chess\\fruit"
},

{
"command" : "pharaon.exe",
"name" : "Pharaon",
"protocol" : "uci",
"workingDirectory" : "c:\\chess\\pharaon"
}
]


And my code, using version 0.17.

Code: Select all

declare function LoadFileAsString(byval filename as string) as string

#include once "fbJson.bi"
dim as string jsontext = LoadFileAsString("engines.json")
'print(jsontext)
dim as jsonItem item = jsonItem(jsontext)
print(item.Datatype = malformed)

function LoadFileAsString(byval filename as string) as string
  dim s as string
  if open(filename for input access read as #1) = 0 then
    close #1
    if open(filename for binary access read as #1) = 0 then
      s = space(lof(1))
      get #1,, s
      close #1
    else
      print "Unable to open '" & filename & "'"
    end if 
  else
    print "File not found '" & filename & "'"
  end if
  return s
end function
Result.
-1
I don't see where the problem is.
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Post by StringEpsilon »

Two things:

You need to trim the input. I really should do that in the class... I'll fix that.

And you found a bug with my deescaping function. If you define fbJSON_Debug, you get this errormessage:

Code: Select all

fbJSON Error: Could not de-escape 'c:\\chess\\fruit' encountered at position 3 on line 7.
The culprit is the "\\". I think I straight up forgot to implement that case. I'll get back to you within the day :)

Edit: That took no time at all.

https://github.com/StringEpsilon/fbJson ... tag/0.17.1
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

Thank you for your answer.
StringEpsilon wrote:The culprit is the "\\". I think I straight up forgot to implement that case.
I don't know why, I met exactly the same problem with Kristopher Windsor JSON parser. I tried to fix the code by myself without success. :)

Waiting the next version. ;)
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

Thank you for the new version (0.17.1).

But I still get an error.
fbJSON Error: Array was not properly closed. Expected ']' at position 3 on line 16, found '' instead.
Edit: Sorry, I had forgotten to trim the string.
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

My little test program is finished and works perfectly. Thank you.

Code: Select all

declare function LoadFileAsString(byval filename as string) as string

#define fbJSON_Debug
#include once "fbJson.bi"

dim as string jsontext = LoadFileAsString("engines.json")
jsontext = Trim(jsontext, any Chr(13, 10))
dim as jsonItem item = jsonItem(jsontext)

Assert(item.Datatype <> malformed)
Assert(item.Datatype = jsonArray)
Assert(item[0].DataType = jsonObject)

dim as jsonItem item1
for i as integer = 0 to item.Count - 1
  item1 = jsonItem(item[i])
  Assert(item1["name"].DataType = jsonString)
  ? item1["name"].Value
next i

function LoadFileAsString(byval filename as string) as string
  dim s as string
  if open(filename for input access read as #1) = 0 then
    close #1
    if open(filename for binary access read as #1) = 0 then
      s = space(lof(1))
      get #1,, s
      close #1
    else
      print "Unable to open '" & filename & "'"
    end if 
  else
    print "File not found '" & filename & "'"
  end if
  return s
end function
Fruit
Pharaon
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

There is a problem when a string value begins with "\\":

[
{
"command" : "fruit_21.exe",
"name" : "Fruit",
"protocol" : "uci",
"workingDirectory" : "\\engines\\fruit\\-"
},

{
"command" : "pharaon.exe",
"name" : "Pharaon",
"protocol" : "uci",
"workingDirectory" : "\\engines\\pharaon\\-"
}
]

Code: Select all

dim as jsonItem item1
for i as integer = 0 to item.Count - 1
  item1 = jsonItem(item[i])
  Assert(item1["name"].DataType = jsonString)
  ? item1["name"].Value
  ? item1["workingDirectory"].Value
next i
Fruit
\\engines\fruit\-
Pharaon
\\engines\pharaon\-
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Post by StringEpsilon »

You're the best :)

I finally wrote a couple more tests for this and found a couple other flaws, such as "\\\\\\" being deescaped to "\\\\\" instead of "\\\". Starting or ending a key with "\\" would have also caused an error.

I really hope escaping now works correctly. I pushed the fix to master.
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

Yes, it works. Thank you.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: JSON library (0.17)

Post by paul doe »

StringEpsilon wrote:Bump: Been a while, but I just got in the mood again and updated fbJSON today.
Thank you. This will come in very handy, and seems that you already have a tester ;)
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: JSON library (0.17)

Post by Roland Chastain »

paul doe wrote:
StringEpsilon wrote:Bump: Been a while, but I just got in the mood again and updated fbJSON today.
Thank you. This will come in very handy, and seems that you already have a tester ;)
Today I did another test. I hope you will like it. :)

fbJson-test.zip
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.18)

Post by StringEpsilon »

Put up a new version:
An shortened change log can be found on github.

Notable changes happened in AddItem() and the number validation. AddItem() was completely broken 0.17. I fixed that and I also changed how it accepts values due to related changes in the value() property and the number validation.

Code: Select all

' 0.17:
dim as jsonItem item
item.AppendChild("17\u2665")
print item[0] ' prints: "17", datatype is jsonNumber

item.AppendChild("\u2665")
' Error, item now malformed.

' 0.18:
dim as jsonItem item
item.AppendChild("17\u2665")
print item[0] ' prints: "17♥", datatype is jsonString

item.AppendChild("\u2665")
' Item[1] now "♥"
The parser will also reject some previously accepted number values:

Code: Select all

' 17:
? jsonItem("17e1e1").value ' "170, datatype jsonNumber

? jsonItem("17.1.1").value ' "17.1, datatype jsonNumber
These are now properly identified as illegal and set to malformed, with one exception: AddItem() and value() will treat such inputs as strings, mostly for convenience.

I also added made two include options: fbJson.bi for just the headers and fbJson.bas for the full fbJSON source. Makes it a bit easier to decide if you want to use it as a DLL or not without fiddling with the source.
Post Reply