Example #1
0
        static void DoJsonValue(JsonTextBuffer avb, string attrName, BaseTerm t, bool first)
        {
            string functor = t.FunctorToString;

            if (functor == PrologParser.CURL)
            {
                DoJsonObject0(avb, attrName, t, first); // is this the first object of a last?
            }
            else if (t is AltListTerm)                  // if {...} has been declared a last with wrap( '{', |, '}')
            {
                DoJsonObject0(avb, attrName, (AltListTerm)t, first);
            }
            else if (t.IsProperList)
            {
                DoJsonArray0(avb, attrName, (ListTerm)t, first);
            }
            else if (t.IsNumber || t.Arity == 0)
            {
                avb.EmitAttrValuePair(attrName, t.ToString(), first);
            }
            else
            {
                IO.Error("Not a JSON-term:\r\n{0}", t);
            }
        }
Example #2
0
 static void DoJsonArray0(JsonTextBuffer avb, string attrName, ListTerm t, bool first)
 {
     avb.AppendPossibleCommaAndNewLine(first); // is this entire []-last the first element?
     avb.EmitOpenBracket(attrName, '[');
     DoJsonArray(avb, t, true);
     avb.EmitCloseBracket(']');
 }
Example #3
0
 static void DoJsonObject0(JsonTextBuffer avb, string attrName, BaseTerm t, bool first)
 {
     avb.AppendPossibleCommaAndNewLine(first); // is this entire {}-last the first element?
     avb.EmitOpenBracket(attrName, '{');
     DoJsonObject(avb, t, true);
     avb.EmitCloseBracket('}');
 }
            public string ToJsonString(int indentDelta, int maxIndentLevel, bool noCommas, bool noQuotes)
            {
                jtb           = new JsonTextBuffer(indentDelta, maxIndentLevel, noCommas);
                this.noQuotes = noQuotes;
                DoJsonStruct(this);

                return(jtb.ToString().Dequoted());
            }
Example #5
0
 static void DoJsonObject(JsonTextBuffer avb, BaseTerm t, bool first)
 {
     foreach (BaseTerm a in t.Args)
     {
         DoJsonPair(avb, a, first);
         first = false;
     }
 }
Example #6
0
        static void DoJsonPair(JsonTextBuffer avb, BaseTerm t, bool first)
        {
            if (!(t.FunctorToString == ":" && t.Arity == 2))
            {
                IO.Error("Not a JSON-term:\r\n{0}", t);
            }

            DoJsonValue(avb, t.Arg(0).FunctorToString, t.Arg(1), first);
        }
Example #7
0
        static void DoJsonArray(JsonTextBuffer avb, ListTerm t, bool first)
        {
            if (t.IsEmptyList)
            {
                return;
            }

            DoJsonValue(avb, null, t.Arg(0), first);
            DoJsonArray(avb, (ListTerm)t.Arg(1), false);
        }
Example #8
0
        static void DoJsonObject(JsonTextBuffer avb, AltListTerm t, bool first)
        {
            if (t.IsEmptyList)
            {
                return;
            }

            DoJsonPair(avb, t.Arg(0), first);
            DoJsonObject(avb, (AltListTerm)t.Arg(1), false);
        }
Example #9
0
        public static string FormatJson(BaseTerm jsonTerm)
        {
            JsonTextBuffer result = new JsonTextBuffer();

            if (jsonTerm.FunctorToString == PrologParser.CURL)
            {
                DoJsonObject0(result, null, jsonTerm, true);
            }
            else if (jsonTerm is AltListTerm) // if {...} has been declared a last with wrap( '{', |, '}')
            {
                DoJsonObject0(result, null, (AltListTerm)jsonTerm, true);
            }
            else if (jsonTerm.IsProperList)
            {
                DoJsonArray0(result, null, ((ListTerm)jsonTerm), true);
            }
            else
            {
                IO.Error("Error in JSON term:\r\n{0}", jsonTerm);
            }

            return(result.ToString());
        }