JSON library (0.20.2)

User projects written in or related to FreeBASIC.
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Re: JSON library (0.12)

Postby AGS » Apr 16, 2016 16:33

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: 37
Joined: Apr 09, 2015 20:49

Re: JSON library (0.12)

Postby StringEpsilon » Apr 16, 2016 17:12

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: 37
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Postby StringEpsilon » Apr 23, 2018 12:37

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: 7502
Joined: May 28, 2005 3:28

Re: JSON library (0.17)

Postby D.J.Peters » Apr 23, 2018 19:47

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 23, 2018 21:26

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: 37
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Postby StringEpsilon » Apr 24, 2018 10:58

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 24, 2018 11:18

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 24, 2018 11:30

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 24, 2018 12:11

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 24, 2018 14:16

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: 37
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Postby StringEpsilon » Apr 24, 2018 15:49

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 24, 2018 16:29

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

Re: JSON library (0.17)

Postby paul doe » Apr 25, 2018 3:49

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: JSON library (0.17)

Postby Roland Chastain » Apr 25, 2018 9:34

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: 37
Joined: Apr 09, 2015 20:49

Re: JSON library (0.18)

Postby StringEpsilon » Apr 27, 2018 12:13

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.

Return to “Projects”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests