Giter VIP home page Giter VIP logo

picolisp-json's Introduction

Hi there ๐Ÿ‘‹

picolisp-json's People

Contributors

aw avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

picolisp-json's Issues

Invalid parsing of strings with caret (^)

I noticed when a JSON string contains a caret ^ character next to another character, the parser will convert it incorrectly.

Incorrect:

# test.json
{"test":"x^2"}

# pil +
: (decode "test.json" T)
-> (("test" . "x^R"))

Correct:

# test.json
{"test":"x^2"}

# pil +
: (decode "test.json" T)
-> (("test" . "x\^2"))

Unable to parse empty arrays with quotes

I discovered a bug in the json.l parser where it is unable to parse an empty array containing an empty string, ex:

: (load "json.l")
-> json-encode
: (println (json-decode "{\"myarray\":[\"\"],\"test\":12345}"))
(("myarray" T))
-> (("myarray" T))

Expected result:

: (load "json.l")   
-> json-encode
: (println (json-decode "{\"myarray\":[\"\"],\"test\":12345}"))
(("myarray" T) ("test" . 12345))
-> (("myarray" T) ("test" . 12345))

Null and False values are not encoded correctly

There's a bug which prevents null and false values from being encoded correctly when generating a JSON file/string.

The strings end up being encoded to an empty object {} instead, because they are returned as NIL.

Numbers can't have a leading zero

From the ECMA 404 specification, which this library should adhere to:

A number is a sequence of decimal digits with no superfluous leading zero.

The following JSON string:

{"Numbers cannot have leading zeroes": 013}

is successfully parsed to:

(("Numbers cannot have leading zeroes" . 13))

Technically it should reject the JSON as non-compliant, rather than strip the leading zeros..

Invalid encoding of special control characters

The encoder doesn't handle special control characters due to native functionality of (str) in PicoLisp:

The combination of a backslash followed by 'n', 'r' or 't' is replaced with newline ("^J"), return ("^M") or TAB ("^I"), respectively.

These characters should be converted back to their original \n\r\t form when encoded. Same applies to \b and \f which are currently encoded as ^H and ^L respectively.

Invalid encoding of true,false,null

Encoding true, false, null is incorrect. Examples:

correct

: (encode '(("test" . true)))
-> "{\"test\":true}"

incorrect

: (encode '(("test" . "true")))
-> "{\"test\":true}"

expected

: (encode '(("test" . "true")))
-> "{\"test\":\"true\"}"

Tests should be done with pointer equality to ensure true = true and "true" = "true"

Note: Decoding is fine.

Invalid encoding of control characters 0x01-0x1F

The unicode control characters 0x01 to 0x1F are not being escaped and thus not appearing in the JSON output.

The value 0x00 is NIL, so we ignore it.

Control characters between 0x01 and 0x1F, excluding the special control characters from #11 should be encoded as \u0001 to \u001F.

json~decode broken outside of 'json' namespace

The following happens with PL 15.11.0 (ubuntu 16.04 global install) and PL 17.11.19 (local install):

erik@thinkster2:~/code/pl/picolisp-json$ pil json.l +

: (json~encode '(("Hello" . "World")))
-> "{\"Hello\":\"World\"}"

: (json~decode @)                   
!? ((any (pack "json-" "Type" "-check")) "Name")   # from 'link-generic', see below
json-object-check -- Undefined
?
                
: (symbols 'json)
-> pico

json: (encode '(("Hello" . "World")))
-> "{\"Hello\":\"World\"}"

json: (decode @)                     
-> (("Hello" . "World"))

The line ((any (pack "json-" Type" "-check")) Name) seems to build a symbol (in this case json-object-check) to be called as a function on Name. When called from outside the the json namespace, it still returns json-object-check, which is local to the json namespace and thus undefined.

The following is a quick fix that (seems) to work on my local copy:

erik@thinkster2:~/code/pl/picolisp-json$ git diff json.l
diff --git a/json.l b/json.l
index c25f792..13697ea 100644
--- a/json.l
+++ b/json.l
@@ -79,7 +79,7 @@
 [de link-generic (Type Bracket Iterator)
   (let Name (car *Json)
     (when Name
-          ((any (pack "json-" Type "-check")) Name)
+          ((any (if (= (symbols) 'json) (pack "json-" Type "-check") (pack "json~json-" Type "-check")) Name)
           (unless (= Bracket Name) (eval Iterator)) )
 
     (if (= Bracket (car *Json))

But this breaks all the unit tests, as PIL_NAMESPACES is set to false for testing:

erik@thinkster2:~/code/pl/picolisp-json$ make check 
PIL_NAMESPACES=false ./test.l
# MODULE_INFO redefined

  Testing JSON encoder/decoder for PicoLisp

"Unmatched JSON brackets '{'"
[test_json.l:96] json -- Bad symbol namespace
? 

Invalid parsing of certain characters with \uNNNN

If the following strings are supplied, the JSON parser fails to decode correctly:

  • \u005c: \
  • \u005e: ^
  • \u0022: "

This is caused by the PicoLisp reader, but we can easily workaround this by escaping those characters during conversion.

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.