2024 Clojure状态调查!中分享您的想法。

欢迎!有关如何工作的更多信息,请参阅关于页面。

0
data.json
重新标记

大多数EOF会产生有用的错误信息,如JSON错误(字符串内的文件末尾)。但对象和数组读取器不处理EOF并抛出不友好的异常。

(clojure.data.json/read-str "{")由于read-key中的以下行抛出错误Value out of range for char: -1
(throw (Exception. (str "JSON error (non-string key in object), found " (char c) ", expected \"")))
(char c)抛出异常,因为c是-1。

这是通过在read-key中处理-1情况来修复的。

(defn- read-key [^PushbackReader stream]
  (let [c (int (next-token stream))]
    (if (= c (codepoint \"))
      (let [key (read-quoted-string stream)]
        (if (= (codepoint \:) (int (next-token stream)))
          key
          (throw (Exception. "JSON error (missing `:` in object)"))))
      (codepoint-case c
        \} nil
        -1 (throw (Exception. "JSON error (end-of-file inside object)"))
        (throw (Exception. (str "JSON error (non-string key in object), found `" (char c) "`, expected `\"`")))))))

(clojure.data.json/read-str "{\"\":\"\"")给出错误的错误信息:JSON错误(对象中缺少条目)

这是通过在read-object中处理EOF来修复的。

  (codepoint-case (int (next-token stream))
    \, (recur r)
    \} (persistent! r)
    -1 (throw (Exception. "JSON error (end-of-file inside object)"))
    (throw (Exception. "JSON error (missing entry in object)"))))

(clojure.data.json/read-str "[")抛出错误JSON错误(意外的字符):￿(意外字符是(char 65535))。这是因为在read-array中,-1被推回流,并且-1被推为65535。

修复是处理read-array中的EOF。

(defn- read-array [^PushbackReader stream options]
  ;; Expects to be called with the head of the stream AFTER the
  ;; opening bracket.
  ;; Only handles array value.
  (let [c (int (next-token stream))]
    (codepoint-case c
      \] []
      \, (throw (invalid-array-exception))
      -1 (throw (Exception. "JSON error (end-of-file inside array)"))
      (do (.unread stream c)
          (read-array* stream options)))))

2 答案

0
 
最佳答案

登录为 https://clojure.atlassian.net/browse/DJSON-57

注意,需要考虑性能影响。最好将异常构造移动到单独的函数中,以减少字节码大小并使得代码更易于内联。

0

添加了一个包含更改摘要的补丁 链接

我们通过向jira问题添加补丁文件进行工作。看起来你已经有一个CA签名了,你可以根据以下信息请求一个jira账户,以提供补丁:https://clojure.org/dev/dev#_becoming_a_contributor,提交一个贡献者支持请求,然后你可以直接将补丁文件添加到jira。
...