void Write(BaseTerm t, bool dequote) { if (t.IsString) { BaseWriteCurrentOutput(dequote ? t.FunctorToString : '"' + t.FunctorToString + '"'); } else if (t.IsAtom) { BaseWriteCurrentOutput(dequote ? t.FunctorToString.Dequoted("'") : t.FunctorToString); } else { BaseWriteCurrentOutput(t.ToString()); } }
void DoJsonStruct(BaseTerm t) // object or array { if (t.FunctorToString == "array") { DoJsonArray(t.Arg(0)); } else if (t.IsProperList) { DoJsonObject(t); } else { IO.Error("Not a well-formed JSON-term: {0}", t.ToString()); } }
void DoJsonLiteral(BaseTerm t) { string s = t.FunctorToString; // not quoted if (noQuotes) { jtb.EmitString(s); } else { if (s.HasSignedRealNumberFormat() || s == "false" || s == "true" || s == "null") { jtb.EmitString(s); } else { jtb.EmitString(t.ToString()); } } }
void DoJsonPair(BaseTerm t) // <string> ':' <value> { if (t.Arity < 2) { IO.Error("Not a well-formed JSON-term: {0}", t.ToString()); } DoJsonLiteral(t.Arg(0)); jtb.EmitString(": "); BaseTerm arg1 = t.Arg(1); if (arg1.Arity == 0) { DoJsonLiteral(arg1); } else { jtb.Newline(); DoJsonValue(arg1); } }
static void DoJsonValue(XmlTextWriter xwr, string [] attributes, string label, BaseTerm t, ref bool contentWritten) { string functor = t.FunctorToString; if (functor == PrologParser.CURL) { DoJsonObject(xwr, attributes, t, ref contentWritten); } else if (t is AltListTerm) // if {...} has been declared a last with wrap( '{', |, '}') { DoJsonObject(xwr, attributes, (AltListTerm)t, ref contentWritten); } else if (t.IsProperList) { DoJsonArray(xwr, attributes, label, (ListTerm)t, ref contentWritten); } else if (t.IsNumber || t.Arity == 0) { xwr.WriteStartElement("value"); string value = t.ToString(); Regex re = new Regex("@{(?<id>.*)}"); Match m = re.Match(value); //if (m.Success) //{ // string id = m.Groups ["id"].Captures [0].Value; // xwr.WriteAttributeString (label, id); //} //else xwr.WriteString(value); xwr.WriteEndElement(); } else { IO.Error("Unable to convert term to XML:\r\n{0}", t); } }
public static string Format(string fmtString, BaseTerm args) { string result = fmtString; if (args is ListTerm) { ListTerm lt = (ListTerm)args; if (!lt.IsProperList) { return(null); } try { return(string.Format(result, lt.ToStringArray())); } catch { IO.Error("Error while applying arguments to format string '{0}'", result); } } else { try { return(string.Format(result, args.ToString().Dequoted("'").Dequoted("\""))); } catch { IO.Error("Error while applying arguments to format string '{0}'", result); } } return(null); }
void Write(BaseTerm t, bool dequote) { if (t.IsString) BaseWriteCurrentOutput (dequote ? t.FunctorToString : '"' + t.FunctorToString + '"'); else if (t.IsAtom) BaseWriteCurrentOutput (dequote ? t.FunctorToString.Dequoted ("'") : t.FunctorToString); else BaseWriteCurrentOutput (t.ToString ()); }