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.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: JSON library (0.17)

Post by jj2007 »

Code: Select all

? jsonItem("17.1.1").value ' "17.1, datatype jsonNumber
Not a trivial question, see e.g. 27.04.2018, 14:24:55

But I agree with your reasoning. They are "parsable" but not single numbers.
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.17)

Post by StringEpsilon »

It's a matter of compliance, not opinion. Values like dates and times must be serialized as strings, not numbers.

I just reread the paragraph and realized I missed some cases. Oh well.

BTW, if you're curious:

The representation of numbers is similar to that used in most
programming languages. A number is represented in base 10 using
decimal digits. It contains an integer component that may be
prefixed with an optional minus sign, which may be followed by a
fraction part and/or an exponent part. Leading zeros are not
allowed.

A fraction part is a decimal point followed by one or more digits.

An exponent part begins with the letter E in uppercase or lowercase,
which may be followed by a plus or minus sign. The E and optional
sign are followed by one or more digits.
https://tools.ietf.org/html/rfc8259#section-6
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.19)

Post by StringEpsilon »

I remembered reading about a testsuite for JSON parsers and decided to test fbJson against that.

Fixed a few issues:

https://github.com/StringEpsilon/fbJson ... s/tag/0.19

I might redeclare this as 1.0 later, if nothing comes up. I still need to sift through the "i_" tests of the suite to check if everything these cover is handled as intended (e.g. reject all invalid UTF-8 no matter what).
dbickin
Posts: 59
Joined: Aug 03, 2005 16:40

Re: JSON library 1.01

Post by dbickin »

Couldn't find a dedicated support forum for this library elsewhere, so I will ask here.

I am trying to use this library, and I like what I see. It works EXCEPT that it does not like the data below. The source is a array of objects like this. It can read all the objects except this one. It appears to stop parsing when it gets to it.

Now, I did see that it had a unicode character in it. The "u2019" was originally "\u2019". But removing the slash does not take the problem away.

I tried taking away a piece at a time, and it started parsing when I removed "lon":0.0. However, when I only removed the "lon":0.0, it still didn't work, so it is a combination of elements??

For testing, I am using this code. The events.count only counts up until the first instance of the problem data, and when it prints the individual event, it returns a blank string.

Code: Select all

#include "fbjson.bas"

dim events as jsonitem
dim event as jsonitem
dim json as string
dim i as integer

open "meetup1.json" for input as #1
line input #1, json
close #1
print "raw data:"
print mid(json,1,50);"..."
print "---------"

events.parse(json)
print events.count; " events found"
for i = 0 to events.count -1
  event = events[i]
  print "EVENT (";i;")"
  print event.tostring
  print "============"
next
Any thoughts on how I can get the library to parse this? I only need about five of the keys from the object.

Thanks,
David

Code: Select all

[{"created":1558483584000,"duration":19800000,"id":"261658924","name":"O u2019 WE GO PADDLE the OSWEGO JUNE 4","rsvp_limit":12,"date_in_series_pattern":false,"status":"upcoming","time":1570370400000,"local_date":"2019-10-06","local_time":"10:00","rsvp_open_offset":"PT495H","rsvp_close_offset":"PT13H","updated":1558492693000,"utc_offset":-14400000,"waitlist_count":0,"yes_rsvp_count":1,"venue":{"id":17727712,"name":"Oswego Lake Parking","lat":0.0,"lon":0.0,"repinned":true,"address_1":"lake oswego rd and sooy rd","city":"washington","country":"us","localized_country_name":"USA","zip":"","state":"NJ"},"group":{"created":1318600152000,"name":"Outdoor Club of South Jersey Canoeing","id":2647602,"join_mode":"approval","lat":39.95000076293945,"lon":-74.91999816894531,"urlname":"Outdoor-Club-of-South-Jersey-Canoeing","who":"Members","localized_location":"Mount Laurel, NJ","state":"NJ","country":"us","region":"en_US","timezone":"US/Eastern"},"link":"https://www.meetup.com/Outdoor-Club-of-South-Jersey-Canoeing/events/261658924/","description":"<p>O u2019 WE GO PADDLE the OSWEGO.We meet at Oswego Lake Put-In. Pack Lunch. Volunteer shuttle Call or email to confirm participation Leader: </p> ","how_to_find_us":"To get to the meeting spot turn down Lake Oswego Road from Rt 563 .3152 Green Bank Chatsworth Rd Chatsworth, NJ 08019 39.704050, -74.527237","visibility":"public"}]
StringEpsilon
Posts: 42
Joined: Apr 09, 2015 20:49

Re: JSON library (0.20.2)

Post by StringEpsilon »

@dbickin:

Sorry for the late response. You can get a faster reply if you open a github issue:

https://github.com/StringEpsilon/fbJson/issues

I can't find any errors with your JSON, using the latest version from master of fbJSON.

Can you explain in more detail what goes wrong, what version you use and what you tried to solve the issue? Examples of the JSON snippets that work vs. don't work would help.

You can also add this define above the include to get debug information from fbJSON:

Code: Select all

#define fbJSON_debug
#include "fbJson.bas"
That would help me a great deal to diagnose the issue, even if I can't replicate it directly, which I can't. I tested your code with commit d2747ade52a. It prints "1 event found" and the object as a string. Just like I'd expect. Version 1.0.1 also seems to work just fine (commit 25b51391d6e).

It might be something operating system related...? What OS and version of FBC are you using?
dbickin
Posts: 59
Joined: Aug 03, 2005 16:40

Re: JSON library (0.20.2)

Post by dbickin »

Thanks for looking at this.

I recompiled with the define and am getting:

Code: Select all

fbJSON Error: Invalid number '0' encountered at position 449 one line 1.
fbJSON Error: Expected key at position 455 on line 1, found ":" instead.
Position 449 appears to be the end of "lat":0.0

The program ends up giving me "1 event" but the string returned is blank.

In the full original file, I get something like 126 events found, with the full string for the first 125, and a blank string for the 126th. (And the hundreds of records after this are apparently ignored.)

I am running Windows 10 64 bit, and the 32 bit fbc 1.0.6

David

PS. Just tried test program on 64 bit linux with 64 bit fbc, and same results. It chokes at position 449.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: JSON library (0.20.2)

Post by badidea »

Hi, I am trying to find the value of a certain key in a JSON string, but I am not getting any where.
The toString() function seems to work (although last key is repeated).

Test code:

Code: Select all

#define fbJSON_debug
#include once "fbJson.bas"

dim as string item1Str = !"{\"system\":{\"set_relay_state\":{\"state\":1}}}"
print item1Str
dim as jsonItem item1 = jsonItem(item1Str)
print item1.toString()

dim as string item2Str = !"{\"time\":{\"get_time\":{\"year\":2000,\"month\":1,\"mday\":2,\"hour\":2,\"min\":53,\"sec\":13,\"err_code\":0}}}"
print item2Str
dim as jsonItem item2 = jsonItem(item2Str)
print item2.toString()

print "item2[0].Key = "; item2[0].Key
print "item2[0].Value = "; item2[0].Value
print !"item2[\"month\"].Key = "; item2["month"].Key
print !"item2[\"month\"].Value = "; item2["month"].Value
Output:

Code: Select all

{"system":{"set_relay_state":{"state":1}}}
{
  "system" : {
    "set_relay_state" : {
      "state" : 
    },
    "state" : 
  }
}
{"time":{"get_time":{"year":2000,"month":1,"mday":2,"hour":2,"min":53,"sec":13,"err_code":0}}}
{
  "time" : {
    "get_time" : {
      "year" : 2000,
      "month" : 1,
      "mday" : 2,
      "hour" : 2,
      "min" : 53,
      "sec" : 13,
      "err_code" : 
    },
    "err_code" : 
  }
}
item2[0].Key = time
item2[0].Value = 
item2["month"].Key = fbJSON Error: Key '' not found in object 
Is it possible to do what I want or have I completely misunderstood the purpose of this library?
rickh57
Posts: 7
Joined: Jan 28, 2021 13:43

Re: JSON library (0.20.2)

Post by rickh57 »

I entered a bug on GitHub for an issue with parsing numbers immediately followed by a closing brace. (https://github.com/StringEpsilon/fbJson/issues/10)
Post Reply