
fmod MAUDE2JSON-AUX is
  inc MY-META-ALL .
  inc SEXP-AUX .
  inc STRING-LIST .
  inc SVAL .
  inc ESET .

**** for testing
op getODbySN : Qid Qid Qid -> OpDeclSet .
ceq getODbySN(m:Qid,s:Qid,o:Qid) = (op o:Qid : nil -> s:Qid [atts:AttrSet].) 
if (op o:Qid : nil -> s:Qid [atts:AttrSet].) ods:OpDeclSet
       := upOpDecls(m:Qid,false) .

op getMDbySN : Qid Qid Qid -> SexpL .
ceq getMDbySN(m:Qid,s:Qid,o:Qid) = alist
if (op o:Qid : nil -> s:Qid [atts:AttrSet].) ods:OpDeclSet
      :=  upOpDecls(m:Qid,false)
/\  mdstr := getMetaData(atts:AttrSet)
/\ ixs(next,sexp?) := readSexp(mdstr,0,length(mdstr)) 
/\ alist := (if sexp? :: Sexp then sexp2l(sexp?) else nil fi) .

var next : FindResult .
vars sexpl alist alist1 sexpl0 : SexpL .
var aexp sexp : Sexp .
var sexp? : Sexp? .
vars mdstr tag val default jelt jeltx eltstr elt  vals str sstr : String .
vars omits : StringList .
var es : ESet .

  sort StringESet .
  op `{_,_`} : String ESet -> StringESet [ctor] .
  op se2s : StringESet -> String .
  op se2e : StringESet -> ESet .
  eq se2s({str,es}) = str .
  eq se2e({str,es}) = es .


op jspecTemplate : -> String .
eq jspecTemplate = 
"\n %name:{\"stype\" : %stype,\n\
\t \"name\" : %name,\n\
\t %jrest\
}" .

op jobjTemplate : -> String .
op jarrTemplate : -> String .
op jeltTemplate : -> String .

eq jobjTemplate = "{ %jelts }" .
eq jarrTemplate = "[ %jvals ]" .
eq jeltTemplate = "%tag : %jval" .  

**** malformed or missing metadata
  op badTemplate : -> String .
  eq badTemplate = 
"\n %name:{\"stype\" : \"BAD\",\n\
\t \"name\" : %name,\n\
\t \"badstr\" : %badstr\n}" 
.


op sep? : String String -> String .
eq sep?(str,sstr) = (if str == "" then "" else sstr fi) .

op alookupd : SexpL String String -> String .
eq alookupd(alist [a(tag) a(val)] alist1,tag,default) = val .
eq alookupd(alist,tag,default) = default [owise] .


****     [tag  vals]* omits jeltstr
**** val is a(str) or [s1 ... sk]  assume s1 is a(tag) si is a(val) or [....]
op alist2json : SexpL StringList String -> String .
eq alist2json(nil,omits,eltstr) 
       = fillTemplate(jobjTemplate,("%jelts" :~ eltstr)) .
ceq alist2json([a(tag) sexpl] alist,omits,eltstr) 
       = alist2json(alist,omits,eltstr + jeltx) 
if jelt := (if occurs(tag, omits)
        then ""
        else fillTemplate(jeltTemplate,
             (("%tag" :~ squote(tag)), ("%jval" :~ sexpl2jval(sexpl))) )
        fi)
/\ jeltx := (if (occurs(tag, omits) or eltstr == "")
          then jelt
          else ", " + jelt fi) .
        
eq alist2json(aexp alist,omits,eltstr) 
        = alist2json(alist,omits,eltstr) [owise] .

op sexpl2jval : SexpL -> String .
eq sexpl2jval(nil) = squote("") .
**** eq sexpl2jval(a(elt)) = squote(elt) .
eq sexpl2jval(sexp) = sexp2jval(sexp) .
**** eq sexpl2jval(a(elt) aexp sexpl) = sexpl2jarr(aexp sexpl,squote(elt)) .
eq sexpl2jval(sexp aexp sexpl) = sexpl2jarr(aexp sexpl,sexp2jval(sexp)) .

**** assume initialized with first array element so "," won't dangle
op sexpl2jarr : SexpL String -> String .
eq sexpl2jarr(nil,vals) = fillTemplate(jarrTemplate, ("%jvals" :~ vals)) .
eq sexpl2jarr(sexp sexpl,vals) 
      = sexpl2jarr(sexpl,vals + sep?(vals,", ") + sexp2jval(sexp)) . 

***(
eq sexpl2jarr(a(elt) sexpl,vals) 
      = sexpl2jarr(sexpl,vals + sep?(vals,", ") + squote (elt)) . 
eq sexpl2jarr([sexpl0] sexpl,vals)  = 
     sexpl2jarr(sexpl,vals + sep?(vals,", ") + alist2json(sexpl0,nil,"")) .
)
op sexp2jval : Sexp -> String .
eq sexp2jval(a(elt)) = squote(elt) .
eq sexp2jval([sexpl]) = sexpl2jarr(sexpl,"") .

endfm


