2017-02-06 42 views
0

反序列化SEXP我使用SEXP錯誤從字符串

序列下面ocaml的記錄類型
module User = 
struct 
    type id = int (*Uuid.t*) [@@deriving sexp] 
module UserName = 
struct 
    type id = string [@@deriving sexp] 
    type userId = {user_id: User.id} [@@deriving sexp] 
    type eff = Add of userId | GetId [@@deriving sexp] 
    let effToString x = Sexp.to_string (sexp_of_eff x) 
    let stringToEff x = (*let y = string_before x 24 in 
         eff_of_sexp (sexp_of_string (string_after y 1))*) 
         eff_of_sexp (Sexp.of_string x) 
    let id_to_str x = x 
end 

上述記錄的示例SEXP看起來像(Add((user_id 756438)))

我轉換SEXP爲字符串用於在其嵌入使用

let effToString x = (*Sexp.to_string_hum*) Sexp.to_string (sexp_of_eff x)

我試圖反序列化SEXP字符串返回到使用記錄類型JSON下面的代碼

let stringToEff x = eff_of_sexp (sexp_of_string x) 

,我發現了錯誤

Fatal error: exception (Sexplib.Conv.Of_sexp_error (Failure "Microblog_app_runtime.UserName.eff_of_sexp: unexpected sum tag") "\"(Add((user_id 941952)))\"")

我試圖消除逃脫引號,但它仍然給了我同樣的錯誤。有人可以解釋錯誤嗎?

回答

1

您應該使用Sexp.of_string而不是sexp_of_string字符串解析:

let stringToEff x = eff_of_sexp (Sexp.of_string x) 

例如,

# effToString (Add {user_id = "941952"}) |> stringToEff;; 
- : eff = Add {user_id = 941952} 

還有就是兩者之間的細微差別。該Sexp.of_string將解析的任意字符串到SEXP數據結構,而sexp_of_string是字符串的SEXP表示(即,原子),參見,sexp_of_intsexp_of_char

+0

試過,我仍然得到同樣的錯誤。 – Kapil

+0

努力嘗試:)我在測試之前測試了它,當然它工作正常。 – ivg

+0

我認爲問題出在{user_id =「941952」}。我編輯了這個問題以提供更多的上下文。 user_id是一個整數字段。你可以嘗試做這個改變嗎? – Kapil