Пример #1
0
        public void Save(JsonValue value, TextWriter tw, int level, bool hasComment, string separator, bool noIndent = false, bool isRootObject = false)
        {
            if (value == null)
            {
                tw.Write(separator);
                tw.Write("null");
                return;
            }

            // check for DSF
            var dsfValue = HjsonDsf.Stringify(this.dsfProviders, value);

            if (dsfValue != null)
            {
                tw.Write(separator);
                tw.Write(dsfValue);
                return;
            }

            switch (value.JsonType)
            {
            case JsonType.Object:
                var obj = value.Qo();
                //var kw = this.writeWsc ? obj as WscJsonObject : null;
                //var showBraces = !isRootObject || (kw != null ? kw.RootBraces : this.emitRootBraces);
                var showBraces = !isRootObject || this.emitRootBraces;
                if (!noIndent)
                {
                    this.nl(tw, level);
                }


                if (showBraces)
                {
                    tw.Write('{');
                }
                else
                {
                    level--;                  // reduce level for root
                }
                //if (kw != null)
                //{
                //	var kwl = this.getWsc(kw.Comments, "");
                //	foreach (var key in kw.Order.Concat(kw.Keys).Distinct())
                //	{
                //		if (!obj.ContainsKey(key)) continue;
                //		var val = obj[key];
                //		tw.Write(kwl);
                //		this.nl(tw, level + 1);
                //		kwl = this.getWsc(kw.Comments, key);

                //		tw.Write(escapeName(key));
                //		tw.Write(":");
                //		this.Save(val, tw, level + 1, this.testWsc(kwl), " ");
                //	}
                //	tw.Write(kwl);
                //	if (showBraces) this.nl(tw, level);
                //}
                //else
                //{
                if (obj.Count > 0)
                {
                    var skipFirst = !showBraces;
                    foreach (var pair in obj)
                    {
                        if (!skipFirst)
                        {
                            this.nl(tw, level + 1);
                        }
                        else
                        {
                            skipFirst = false;
                        }

                        if (!string.IsNullOrEmpty(pair.Value.Comment))
                        {
                            tw.Write($"// {pair.Value.Comment}");
                            this.nl(tw, level + 1);
                        }

                        tw.Write(escapeName(pair.Key));
                        tw.Write(":");
                        this.Save(pair.Value, tw, level + 1, false, " ");
                    }
                }
                else
                {
                    this.nl(tw, level);
                }

                if (showBraces)
                {
                    this.nl(tw, level);
                }

                if (showBraces)
                {
                    tw.Write('}');
                }
                if (level == 0)
                {
                    tw.Write(JsonValue.eol);
                }
                break;

            case JsonType.Array:
                int i = 0, n = value.Count;
                if (!noIndent)
                {
                    if (n > 0 && !value.Inline)
                    {
                        this.nl(tw, level);
                    }
                    else
                    {
                        tw.Write(separator);
                    }
                }
                tw.Write('[');
                WscJsonArray whiteL = null;
                string       wsl    = null;
                if (this.writeWsc)
                {
                    whiteL = value as WscJsonArray;
                    if (whiteL != null)
                    {
                        wsl = this.getWsc(whiteL.Comments, 0);
                    }
                }
                for (; i < n; i++)
                {
                    var v = value[i];
                    if (whiteL != null)
                    {
                        tw.Write(wsl);
                        wsl = this.getWsc(whiteL.Comments, i + 1);
                    }

                    if (value.Inline)
                    {
                        this.Save(v, tw, level + 1, wsl != null && this.testWsc(wsl), (i == 0) ? " " : ", ", true);
                    }
                    else
                    {
                        this.nl(tw, level + 1);
                        this.Save(v, tw, level + 1, wsl != null && this.testWsc(wsl), "", true);
                    }
                }
                if (whiteL != null)
                {
                    tw.Write(wsl);
                }
                if (n > 0 && !value.Inline)
                {
                    this.nl(tw, level);
                    tw.Write(']');
                }
                else
                {
                    tw.Write(" ]");
                }
                break;

            case JsonType.Boolean:
                tw.Write(separator);
                tw.Write(value ? "true" : "false");
                break;

            case JsonType.String:
                this.writeString(((JsonPrimitive)value).GetRawString(), tw, level, hasComment, separator);
                break;

            default:
                tw.Write(separator);
                tw.Write(((JsonPrimitive)value).GetRawString());
                break;
            }
        }
Пример #2
0
        public void Save(JsonValue value, TextWriter tw, int level, bool hasComment, string separator, bool noIndent = false, bool isRootObject = false)
        {
            if (value == null)
            {
                tw.Write(separator);
                tw.Write("null");
                return;
            }

            // check for DSF
            string dsfValue = HjsonDsf.Stringify(dsfProviders, value);

            if (dsfValue != null)
            {
                tw.Write(separator);
                tw.Write(dsfValue);
                return;
            }

            switch (value.JsonType)
            {
            case JsonType.Object:
                var           obj        = value.Qo();
                WscJsonObject kw         = writeWsc?obj as WscJsonObject:null;
                bool          showBraces = !isRootObject || (kw != null?kw.RootBraces:emitRootBraces);
                if (!noIndent)
                {
                    if (obj.Count > 0)
                    {
                        nl(tw, level);
                    }
                    else
                    {
                        tw.Write(separator);
                    }
                }
                if (showBraces)
                {
                    tw.Write('{');
                }
                else
                {
                    level--; // reduce level for root
                }
                if (kw != null)
                {
                    var kwl = getWsc(kw.Comments, "");
                    foreach (string key in kw.Order.Concat(kw.Keys).Distinct())
                    {
                        if (!obj.ContainsKey(key))
                        {
                            continue;
                        }
                        var val = obj[key];
                        tw.Write(kwl);
                        nl(tw, level + 1);
                        kwl = getWsc(kw.Comments, key);

                        tw.Write(escapeName(key));
                        tw.Write(":");
                        Save(val, tw, level + 1, testWsc(kwl), " ");
                    }
                    tw.Write(kwl);
                    if (showBraces)
                    {
                        nl(tw, level);
                    }
                }
                else
                {
                    bool skipFirst = !showBraces;
                    foreach (JsonPair pair in obj)
                    {
                        if (!skipFirst)
                        {
                            nl(tw, level + 1);
                        }
                        else
                        {
                            skipFirst = false;
                        }
                        tw.Write(escapeName(pair.Key));
                        tw.Write(":");
                        Save(pair.Value, tw, level + 1, false, " ");
                    }
                    if (showBraces && obj.Count > 0)
                    {
                        nl(tw, level);
                    }
                }
                if (showBraces)
                {
                    tw.Write('}');
                }
                break;

            case JsonType.Array:
                int i = 0, n = value.Count;
                if (!noIndent)
                {
                    if (n > 0)
                    {
                        nl(tw, level);
                    }
                    else
                    {
                        tw.Write(separator);
                    }
                }
                tw.Write('[');
                WscJsonArray whiteL = null;
                string       wsl    = null;
                if (writeWsc)
                {
                    whiteL = value as WscJsonArray;
                    if (whiteL != null)
                    {
                        wsl = getWsc(whiteL.Comments, 0);
                    }
                }
                for (; i < n; i++)
                {
                    var v = value[i];
                    if (whiteL != null)
                    {
                        tw.Write(wsl);
                        wsl = getWsc(whiteL.Comments, i + 1);
                    }
                    nl(tw, level + 1);
                    Save(v, tw, level + 1, wsl != null && testWsc(wsl), "", true);
                }
                if (whiteL != null)
                {
                    tw.Write(wsl);
                }
                if (n > 0)
                {
                    nl(tw, level);
                }
                tw.Write(']');
                break;

            case JsonType.Boolean:
                tw.Write(separator);
                tw.Write((bool)value?"true":"false");
                break;

            case JsonType.String:
                writeString(((JsonPrimitive)value).GetRawString(), tw, level, hasComment, separator);
                break;

            default:
                tw.Write(separator);
                tw.Write(((JsonPrimitive)value).GetRawString());
                break;
            }
        }