Closed SimonDanisch closed 5 years ago
The json transit reader expects a json transit document. Since "\"^\""
is not valid json, it is not valid input to the reader.
Actually, this is the reduced case from a valid JSON transit file - I thought it made sense to remove it to the absolute minimal example that crashes with the same error ;) A valid json string with the same error is:
{"a":"^"}
Or as an escaped string:
"{\"a\":\"^\"}"
That makes more sense, however, per https://github.com/cognitect/transit-format#special-characters, ^
is reserved for transit encoding.
I see, thank you! Is it possible to escape it? Or actually, shouldn't it already be escaped inside a string? I would have thought that caching only applies to whole values or to keys.
Yes, the transit writer escapes it with '~', and the reader can then read the escaped string:
$ clj -Sdeps '{:deps {com.cognitect/transit-clj {:mvn/version "0.8.313"}}}'
Clojure 1.10.0
(require '[cognitect.transit :as transit])
(import [java.io ByteArrayInputStream ByteArrayOutputStream])
(def out (ByteArrayOutputStream. 4096))
nil
user=> java.io.ByteArrayOutputStream
user=> user=> user=> #'user/out
user=> (def writer (transit/writer out :json))
#'user/writer
user=> (transit/write writer "^")
nil
user=> (.toString out)
"[\"~#'\",\"~^\"]"
(def in (ByteArrayInputStream. (.toByteArray out)))
(def reader (transit/reader in :json))
#'user/in
user=> #'user/reader
user=> (prn (transit/read reader))
"^"
nil
That all make sense?
I see!
Just to make sure I fix the right thing:
I just need to escape [~
, ^
, `
] whenever they occur in the first position of a string.
So if I write transit, I'm fine as long as I pre-process my strings like that!
Thank you for your patience and answering this so quickly!
You've got it right, however you only have to worry about that If you're hand-crafting the string. If you use the transit writer to create it, then you don't have to worry about this. Is there something preventing you from using the transit writer?
Yes, sadly! This is for the communication between a Julia and a Clojure process, and we thought we're fine by just using a modified JSON writer in Julia, since the Julia Transit.jl package is in a bad state and not maintained. Actually, I just checked, and from what I can tell, they also don't do any string escaping, so would have still run into the same bug :D
@SimonDanisch I've nudged the transit.jl maintainers, but it's the first time in a while that it's gotten any attention so it might be a bit before it gets addressed. Keep your eyes on your PR (and that repo in general).
MWE: https://nextjournal.com/a/LEjZdbN3nL5irFe2khgCM?token=KBtQJAPvS6TVvcmXyeN5U5
Stacktrace: