示例#1
0
        public async Task BuildMixedAsync(TextWriter writer, object jsonObj, int minDepth, int maxDepth, int indent)
        {
            if (minDepth < 0)
            {
                minDepth = MAXIMUM_MIN_DRILL_DOWN_DEPTH;
            }
            if (maxDepth < 0)
            {
                maxDepth = DEFAULT_MAX_DRILL_DOWN_DEPTH;
            }
            // Note that minDepth does not have to be smaller than maxDepth.
            // 0 <= minDepth <= maxDpeth+1.
            int cutoff = maxDepth - minDepth;

            bool useBeanIntrospection = this.builderPolicy.UseBeanIntrospection;

            IndentInfoStruct indentInfo   = new IndentInfoStruct(indent);
            bool             includeWS    = indentInfo.IsIncludingWhiteSpaces;
            bool             includeLB    = indentInfo.IsIncludingLineBreaks;
            bool             lbAfterComma = indentInfo.IsLineBreakingAfterComma;
            int indentSize  = indentInfo.IndentSize;
            int indentLevel = -1;

            await _buildAsync(writer, jsonObj, cutoff, maxDepth, useBeanIntrospection, includeWS, includeLB, lbAfterComma, indentSize, indentLevel);

            // tbd:  ????
            // writer.flush(); // ???
            // ????

            //        String jsonStr = writer.toString();
            //        if(log.isLoggable(Level.FINE)) log.fine("jsonStr = " + jsonStr);
        }
示例#2
0
        ///////////////////////////////////
        // JsonSerializable interface
        // Note: The default depth of AbstractJsonNodes is always 1.

        public override async Task WriteJsonStringAsync(TextWriter writer, int indent)
        {
            if (map == null)
            {
                writer.Write(Literals.NULL);
                return;
            }

            IndentInfoStruct indentInfo   = new IndentInfoStruct(indent);
            bool             includeWS    = indentInfo.IsIncludingWhiteSpaces;
            bool             includeLB    = indentInfo.IsIncludingLineBreaks;
            bool             lbAfterComma = indentInfo.IsLineBreakingAfterComma;
            int indentSize = indentInfo.IndentSize;

            // ???
            // We need a way to set the "global indent level" ....
            int    indentLevel = 0;
            string WS          = "";

            if (includeWS)
            {
                WS = " ";
            }
            string LB = "";

            if (includeLB)
            {
                LB = "\n";
            }
            string IND  = "";
            string INDX = "";

            if (indentSize > 0 && indentLevel > 0)
            {
                // IND = String.format("%1$" + (indentSize * indentLevel) + "s", "");
                IND = string.Format("{0," + (indentSize * indentLevel) + "}", "");
            }
            if (indentSize > 0 && indentLevel >= 0)
            {
                // INDX = String.format("%1$" + (indentSize * (indentLevel + 1)) + "s", "");
                INDX = string.Format("{0," + (indentSize * (indentLevel + 1)) + "}", "");
            }

            writer.Write("{");
            writer.Write(LB);
            writer.Write(INDX);
            var it      = map.Keys.GetEnumerator();
            var isFirst = true;

            while (it.MoveNext())
            {
                if (!isFirst)
                {
                    writer.Write(",");
                    if (lbAfterComma)
                    {
                        writer.Write(LB);
                        writer.Write(INDX);
                    }
                    else
                    {
                        writer.Write(WS);
                    }
                }
                string         key     = it.Current;
                JsonStringNode jsonKey = new AbstractJsonStringNode(key);
                var            node    = map[key] as JsonNode;
                writer.Write("\"");
                writer.Write(await jsonKey.ToJsonStringAsync(indent));
                writer.Write("\":");
                writer.Write(WS);
                writer.Write(await node.ToJsonStringAsync());
                isFirst = false;
            }
            writer.Write(LB);
            writer.Write(IND);
            writer.Write("}");
        }