Giter VIP home page Giter VIP logo

fastjson's Introduction

fastjson

A Livecode library for Array to JSON and back again.

By combining the best of easyjson and libjson and adding in performance enhancements, fastjson improves the speed of encoding and decoding Livecode arrays to/from JSON.

Credit goes first to Mark Smith for creating the original libjson library and to Igor de Oliveira Couto who created the also excellent easyJson library.

I am combining routines from both of these libraries to give us a decent boost in performance for JSON to/from Array needs. Yes, you can go faster than this with an custom coded extension. But I find the speed of this library to be pretty impressive.


Instructions

There are 3 calls that you will make from your Livecode scripts.

  arrayToJson(tArrayData)
  jsonToArray(tJsonData, asUnicode, skipValidation)
  isJson(tJsonData)

NOTE: You will also find the following for compatibility with existing easyJson scripts

 arrayFromJson(tJsonData) (NOT TESTED)
 jsonFromArray(tArrayData) (NOT TESTED)

All other functions and commands in this script are for local use only.

To use this library in a Stack I would suggest creating a substack of your main project named fastJson. Copy and paste the text of fastjson.lc minus the first line (<?lc) into the stack script of your new substack. In the preOpenStack handler of your main stack:

 start using stack "fastJson"

For livecode server use, include "fastjson.lc" in your server script.

This project maintained on GitHub at https://github.com/bhall2001/fastjson

Please report any bugs you find to https://github.com/bhall2001/fastjson/issues

fastjson's People

Contributors

bhall2001 avatar madpink avatar mwieder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastjson's Issues

Tokenizer for + and - special case

While working on the Livecode Dictionary JSON file, an error was discovered in the Parser where the following JSON is not being parsed correctly by the Tokenizer.

...
"name":"-",
...

Tokenizer returns a "token" of "-,". This results in all data after this point being errant.

Problem in "isJson" function

on an iOS device, this complete locks up the program... here's what happens...

in this instance, the response from the site is NOT a JSON, but is instead a 404 error
the problem seems to come with line 227:
put sJson[n-3] & return & sJson[n-2] & return & sJson[n-1] & return & sJson[n] & return & sJson[n+1] & return

in the Livecode IDE, this causes the message box to open up and display some characters, but in a compiled iOS app, everything freezes when it gets to this point

removing this line fixed the problem for me, as the next step is simply to return false
not sure what that line was meant to do...

Still maintaining?

Hey Bob,

I'm making a bunch of changes to the library and wasn't sure if you were still interested in updating it.

So would you prefer I make a pull request my changes, or would you prefer I fork it and maintain on my own? Currently, Ive added my modified version to my CouchDB lib.

The changes I've been making include:
-maintaining the sequence of a JSON array in order of the numeric array it is based on
-adding an option for inserting null into a JSON array when the numeric array it is based on has gaps
-changing the "keepNumeric" option to a script local variable
-adding a "set local" type command for these options instead of passing as params

I have a few other changes as well I'm working on.

Let me know if you want me to pull or tell me to fork off ;)

JSON starting with [ does not work

Any JSON string that starts with [ does not work in my tester script. Not sure if this is a problem with the sample stack or with the library. Getting this down so I remember to investigate it.

Don't think your isNumericalArray() function does what you want.

From forums.livecode.com

It looks like you're just checking the first char of each key.
So keys like

0ThisIsZero
1001Nights

would return true.

A better filter might be

CODE: SELECT ALL
filter pArray with "[A-Za-z]"

which will return tArray not empty for any non-numeric keys.

"infinity" being detected as a number

JSONlint report mal formed data. Found the issue that a dog name "Infinity" is having their name converted to a "number" called infinity. Interesting...

"" causing performance issue

from Livecode Dictionary JSON...
...
"type":""
...

Having an empty string is causing a significant performance issue until Livecode is quite and re-launched.

Breaks with an empty JSON object

There seems to be a problem when jsonToArray() tries to parse an empty JSON object.

Example
For the following JSON
{"data":[{"pk_customer":"123","portalData":{}}]}
After running

put jsonToArray(theJson) into tResult
return arrayToJson(tResult)

We get
{"data":null}

However, if we run the same code for the slightly amended JSON
{"data":[{"pk_customer":"123","portalData":{""}}]}
We get
{"data":[{"portalData":null,"pk_customer":123}]}

According to JSONLint {} is valid JSON.

Obviously this issue is easy enough to work around using
replace "{}" with "{" & quote & quote & "}" in theJson

Thought I'd at least mention it here even if it doesn't get fixed someone might need the easy workaround in the future.

The private function isNumericArray does not work for very large datasets

The private function called isNumericArray is returning false for very large numeric arrays. The RegEx is failing to extract the keys. Tested with 500k worth of response in a single numeric array. Maybe its our LiveCode Server engine that can't use that RegEx expression.

Replacing that filter call with a slower loop will make this work and doesn't impact much on performance.

Cannot correctly create or convert an empty array

I wanted to post this as an issue because it may be an issue for others. I have a couple of quick fixes, but I am not 100% sure they need to be implemented so I am not going to create a pull request. For CouchDB the empty array is important, I believe that it also is for MongoDB. I am not sure about other uses.

Here is an example of what I am trying to produce (this is a user record for CouchDB):

{"name":"madpink","password":"trustno1","roles":[],"type":"user"}

"Roles" needs to be an array, and can only be full of strings or completely empty. Closest I can get is this script:

   put "madpink" into tData["name"]
   put "trustno1" into tData["password"]
   put "user" into tData["type"]
   put empty into tData["roles"][1]
   put jsonFromArray(tData) into tJson

This is the result:

{"type":"user","password":"trustno1","roles":[null],"name":"madpink"}

However, this errors out. For the time being I just have the following to my own script:

replace "[null]" with "[]" in tJson

Needless to say, the reverse process is also a problem, so I added this step before processing a JSON into an array:

put "{"&quote&1&quote&":null}" into tEmpty
replace "[]" with tEmpty in tJson

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.