Exemple #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;
            }
            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;
            }
        }
Exemple #2
0
 /// <summary>Gets the JsonObject from a JsonValue.</summary>
 public static JsonObject Qo(this JsonValue json)
 {
     try { return((JsonObject)json); }
     catch { throw failQ(json, "Qo"); }
 }
Exemple #3
0
 /// <summary>Gets the JsonArray from a JsonValue.</summary>
 public static JsonArray Qa(this JsonValue json)
 {
     try { return((JsonArray)json); }
     catch { throw failQ(json, "Qo"); }
 }
Exemple #4
0
        static Exception failQ(JsonValue forObject, string op)
        {
            string type = forObject != null?forObject.JsonType.ToString().ToLower() : "null";

            return(new Exception("JsonUtil." + op + " not supported for type " + type + "!"));
        }
Exemple #5
0
 /// <summary>Gets the JsonArray from a JsonValue.</summary>
 public static JsonArray Qa(this JsonValue json)
 {
     return((JsonArray)json);
 }
Exemple #6
0
 /// <summary>Gets the JsonObject from a JsonValue.</summary>
 public static JsonObject Qo(this JsonValue json)
 {
     return((JsonObject)json);
 }
Exemple #7
0
        public void Save(JsonValue value, TextWriter tw, int level, bool hasComment)
        {
            switch (value.JsonType)
            {
            case JsonType.Object:
                var           obj = value.Qo();
                WscJsonObject kw  = WriteWsc?obj as WscJsonObject:null;
                if (level > 0)
                {
                    nl(tw, level);
                }
                tw.Write('{');
                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(":");
                        var nextType = val != null?(JsonType?)val.JsonType:null;
                        if (nextType != JsonType.Array && nextType != JsonType.Object)
                        {
                            tw.Write(" ");
                        }
                        if (val == null)
                        {
                            tw.Write("null");
                        }
                        else
                        {
                            Save(val, tw, level + 1, testWsc(kwl));
                        }
                    }
                    tw.Write(kwl);
                    nl(tw, level);
                }
                else
                {
                    foreach (JsonPair pair in ((JsonObject)value))
                    {
                        nl(tw, level + 1);
                        tw.Write(escapeName(pair.Key));
                        tw.Write(":");
                        var nextType = pair.Value != null?(JsonType?)pair.Value.JsonType:null;
                        if (nextType != JsonType.Array && nextType != JsonType.Object)
                        {
                            tw.Write(" ");
                        }
                        if (pair.Value == null)
                        {
                            tw.Write("null");
                        }
                        else
                        {
                            Save(pair.Value, tw, level + 1, false);
                        }
                    }
                    nl(tw, level);
                }
                tw.Write('}');
                break;

            case JsonType.Array:
                if (level > 0)
                {
                    nl(tw, level);
                }
                tw.Write('[');
                int          i = 0, n = value.Count;
                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);
                    }
                    if (v.JsonType != JsonType.Array && v.JsonType != JsonType.Object)
                    {
                        nl(tw, level + 1);
                    }
                    if (v != null)
                    {
                        Save(v, tw, level + 1, wsl != null && testWsc(wsl));
                    }
                    else
                    {
                        tw.Write("null");
                    }
                }
                if (whiteL != null)
                {
                    tw.Write(wsl);
                }
                nl(tw, level);
                tw.Write(']');
                break;

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

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

            default:
                tw.Write(((JsonPrimitive)value).GetRawString());
                break;
            }
        }
Exemple #8
0
 internal static string Stringify(IEnumerable <IHjsonDsfProvider> dsfProviders, JsonValue value)
 {
     foreach (var dsf in dsfProviders)
     {
         try
         {
             var text = dsf.Stringify(value);
             if (text != null)
             {
                 if (text.Length == 0 || text.FirstOrDefault() == '"' || text.Any(c => isInvalidDsfChar(c)))
                 {
                     throw new Exception("value may not be empty, start with a quote or contain a punctuator character except colon: " + text);
                 }
                 return(text);
             }
         }
         catch (Exception e)
         {
             throw new Exception("DSF-" + dsf.Name + " failed; " + e.Message, e);
         }
     }
     return(null);
 }