JSONItem replacement

  1. ‹ Older
  2. 5 years ago

    Kem T

    25 Aug 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    I've included those unit tests, and they all pass. There was no change to the class code so I just replaced the release and did not update the version number.

  3. Kem T

    25 Aug 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    Famous last words...

    Now at 2.7. This has better Strict checking of numbers ("2.e5" will not be allowed with Strict, for example) and an IsLoading method that will report if the object is currently loading. The latter is for threads because, now, if you try to load into the same object from a second thread, you will get an exception.

    Most importantly, I dealt with aborted loads. Before, if you tried to load from a string with a bad value, it would load everything until that bad value. Now, the object will revert to the point where you started if the load is aborted.

    Finally, if a string contains a control character or escaped random character ("\w"), it will be accepted UNLESS Strict = True.

  4. Greg O

    25 Aug 2014 Xojo Inc

    @Kem T This has better Strict checking of numbers ("2.e5" will not be allowed with Strict, for example)

    I'm confused. The spec defines number as:

    int
    int frac
    int exp
    int frac exp

    Why wouldn't 2.e5 be valid?

  5. Tim H

    25 Aug 2014 Pre-Release Testers Portland, OR USA

    It looks malformed to me. Should be 2.0e5 or just 2e5.

  6. Jeremy C

    25 Aug 2014 Pre-Release Testers, Xojo Pro Ohio, USA

    2.e5 is invalid according to spec, the parsing diagram says if decimal point is included, it must be followed by a number.

    Other languages (for example):

    // JavaScript
    > JSON.parse('{"wage":2.e5}')
    SyntaxError: Unexpected token e
        at Object.parse (native)
        at repl:1:7
        at REPLServer.self.eval (repl.js:110:21)
        at Interface.<anonymous> (repl.js:239:12)
        at Interface.EventEmitter.emit (events.js:95:17)
        at Interface._onLine (readline.js:202:10)
        at Interface._line (readline.js:531:8)
        at Interface._ttyWrite (readline.js:760:14)
        at ReadStream.onkeypress (readline.js:99:10)
        at ReadStream.EventEmitter.emit (events.js:98:17)
    > JSON.parse('{"wage":2.0e5}')
    { wage: 200000 }
    
    // Python
    >>> import json
    >>> json.loads('{"wage":2.e5}')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
        return _default_decoder.decode(s)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
        obj, end = self.scan_once(s, idx)
    ValueError: Expecting , delimiter: line 1 column 10 (char 9)
    >>> json.loads('{"wage":2.0e5}')
    {u'wage': 200000.0}
    
    // Ruby
    irb(main):005:0> JSON.parse('{"wage":2.e5}')
    JSON::ParserError: 757: unexpected token at '{"wage":2.e5}'
    	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/json/common.rb:155:in `parse'
    	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/json/common.rb:155:in `parse'
    	from (irb):5
    	from /usr/bin/irb:12:in `<main>'
    irb(main):006:0> JSON.parse('{"wage":2.0e5}')
    => {"wage"=>200000.0}
  7. Kem T

    25 Aug 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    We (meaning with the help of @Jeremy C) tested against other implementations to be sure, and an integer-dot-exp comes back as an error. Should be int-exp-int or int-dot-int-exp-int.

  8. Jeremy C

    25 Aug 2014 Pre-Release Testers, Xojo Pro Ohio, USA

    Can't edit. Meant to say, section 8 of spec says "It may have a . (U+002E) prefixed fractional part." but the parsing diagram makes this more clear (to me at least).

  9. Alwyn B

    25 Aug 2014 Pre-Release Testers, Xojo Pro Johannesburg, South Africa

    @ChristianSchmitz By the way, Kem do you handle duplicate key names well?

    Just an option (with the best of intention to help everyone, or to be corrected if needed) on duplicate keys, because I think this is a very important concept to consider before you start designing your awesome JSON data sets.

    I work with a lot with JSON on three different platforms, Xojo, VB6 and JavaScript, to represent and transport very complex on/between completely different platforms. JSON is an exceptionally great specification to represent key:value pairs in xplat environments.

    Having duplicate keys is an bad idea IMO, and a parser should not allow duplicate keys. If you need to use duplicate keys, then you really should look at reworking your data schema using arrays.

    And example...

    {
        "a" : "x",
        "a" : "y"
    }

    should rather represented stored as...

    {
        "a" : ["x", "y"]
    }

    Even if some languages allows for duplicate keys, not all languages do, and you will eventually bump your head in frustration when you start transmitting your data to other platforms, if you have duplicate keys in your schema.

    Would like to hear if you agree or disagree on this?

  10. Alwyn B

    26 Aug 2014 Pre-Release Testers, Xojo Pro Johannesburg, South Africa

    Pardon all the typos... forum doesn't allow me to edit... so here goes..

    Just an opinion...

    I work a lot with JSON on..., to represent and transport very complex DATA on/between completely different platforms.

  11. Alwyn B

    26 Aug 2014 Pre-Release Testers, Xojo Pro Johannesburg, South Africa

    should rather be stored as...

  12. Kem T

    26 Aug 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    Now 2.8.

    I realized the native class ToString is a computed property, not a method, and there is a method called Serialize(data as JSONItem) As String that handles the actual work. I'm not sure if the latter is supposed to be exposed, but whatever, I changed my class to match. ToString is now a computed property so it can be examined in the debugger, and Serialize can be called with a JSONItem_MTC if you prefer (but I recommend against and will hide or change this if the native is changed).

    No functional changes in this one.

  13. 4 years ago

    Kem T

    21 Aug 2015 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    I just updated to v.2.81 and that fixes a bug in the Remove method, among other things. (Thanks marco-at-citec.)

  14. Kem T

    13 Dec 2015 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    I just released v.2.9. This adds support for Text (something the native version does not support as I write this) and updates the tests for 64-bit.

  15. 3 years ago

    Kem T

    1 May 2016 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut
    Edited 3 years ago

    I just released v.3.0 of JSONItem_MTC , the drop in replacement for the classic framework JSONItem.

    This version does a few things. First, I did away with the pragmas to disable background tasks and checks. Why? First, it didn't make much difference in speed anyway. Second, it would tie things up when encoding large JSON.

    Second, I made memory usage more efficient when turning the JSON into a string. Rather than using one ever-expanding MemoryBlock, it creates a series of MemoryBlocks to hold the output and joins them at the end. As such, it can serialize a 500 MB JSON without issue. (Larger strings will give Xojo fits, it seems.)

    I also added a few more tests to the harness project.

    If you're using JSONItem, I encourage you to consider this instead. JSONItem has a few issues that have been solved by this class, as illustrated by the included unit tests. (See the README for some of the details.) And it's faster to boot.

    Edit: Oh yeah, I also added an Operator_Subscript so you can both assign and retrieve values from an Array-type JSON with a simple index. For example:

    dim j as new JSONItem_MTC( "[1, 2]" )
    
    dim i as integer = j( 0 )
    j( 1 ) = true
  16. Jim S

    1 May 2016 Pre-Release Testers Columbia, South Carolina, USA

    @Kem T If you're using JSONItem, I encourage you to consider this instead.

    In checking a few of my apps I found one that uses your Kaju (which I appreciate very much.) It has 18 places where "JSONitem" is used, 17 are within the Kaju class.

    Should I change all 18 to use "JSONitem_MTC?

    Thanks again you and Luke and Jeremy Cowger for KAJU.

  17. Kem T

    1 May 2016 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    The JSON in Kaju is well controlled and fairly small, and speed is not a big issue, which is why I didn't use it there. I'd leave that alone.

  18. 11 months ago

    Kem T

    12 Sep 2018 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    I just released JSONItem_MTC v.4.0. While there have been no functional changes to that class, the project now includes M_JSON, and that includes ParseJSON_MTC and GenerateJSON_MTC. As you might guess, those do what the new framework Xojo.Data. functions do, but with the classic (soon to be API 2.0) framework. In other words, rather than working with Text, Xojo.Core.Dictionary, and Auto(), they work with String, Dictionary, and Variant().

    GenerateJSON_MTC also has an optional parameter to produce "pretty" output.

    As always, the project can be found at:

    https://github.com/ktekinay/JSONItem_MTC

  19. 7 months ago

    Kem T

    23 Jan 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    The JSONItem_MTC project has been update to 4.1. This is a minor change that only affects debugging. When the keys of JSON objects are stored internally, they used to be converted to/from hex to preserve case-sensitivity. Now they are stored in the form key-HEX.

  20. 6 months ago

    Tim S

    14 Feb 2019 Pre-Release Testers, Xojo Pro Phoenix Arizona USA

    This is really fantastic Kem! I had the same problem and you solved it!
    Thank you for sharing your hard work!

    Tim

  21. 2 weeks ago

    Kem T

    Aug 7 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut
    Edited 2 weeks ago

    I've updated the project to 4.1.1. It turns out that M_JSON was not encoding embedded quotes or backslashes, a mistake so embarrassing that I wish @Thom M had caused it instead of merely reporting it. Alas, I am left with nobody else to blame, and hope it didn't cause any headaches.

    Again, the project is at:

    https://github.com/ktekinay/JSONItem_MTC

or Sign Up to reply!