fmod LOCS2JSON is
  inc MAUDE2JSON-AUX .

***(
{ "stype" : "loc",
   "name" : String,           // its name (unique in a KB)
   "metadata" : JObject
}
)

  vars  modname qid  : Qid .
  vars str mdstr jelts mdelt : String .
  var next : FindResult .
  var sexp? : Sexp? .

  var alist : SexpL .
  var  defn : String .
  var bindings : TBinds .

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


op locs2json : Qid -> String .
eq locs2json(modname) = locs2json(upOpDecls(modname,false),"") .

op locs2json : OpDeclSet String -> String .
eq locs2json(none,jelts) = fillTemplate(jobjTemplate, ("%jelts" :~ jelts)) .
eq locs2json(od:OpDecl ods:OpDeclSet,jelts) 
     = locs2json(ods:OpDeclSet, 
         jelts 
         + (if jelts == "" then "" else ", " fi) 
         + loc2json(od:OpDecl) ) . 

op loc2json : OpDecl -> String .
ceq loc2json(op qid : nil -> t:Type [atts:AttrSet]. )
   = fillTemplate(locJsonTemplate, bindings)
  if  mdstr := getMetaData(atts:AttrSet)
  /\ ixs(next,sexp?) := readSexp(mdstr,0,length(mdstr)) 
  /\ alist := (if sexp? :: Sexp then sexp2l(sexp?) else nil fi) 
  /\ mdelt :=  alist2json(alist,nil,"") 
  /\ bindings := (("%stype" :~ squote("loc")), 
                  ("%name" :~ squote(string(qid))),
                  ("%metadata" :~ mdelt )) .


endfm

***(
{
"TLR9C":{"description":"IL1R complex","stype":"loc","name":"TLR9C"},
"CLi":{"description":"","stype":"loc","name":"CLi"},
})

