Ejemplo n.º 1
0
 /// <summary>
 /// ディクショナリをダンプする。
 /// </summary>
 /// <param name="writer">出力先</param>
 /// <param name="dictionary">ディクショナリ</param>
 private static void DumpDictionary(IndentWriter writer, string paramName, JDictionary dictionary)
 {
     if (string.IsNullOrEmpty(paramName))
     {
         writer.WriteLine("{");
     }
     else
     {
         writer.WriteLine($"{paramName} : {{");
     }
     writer.Indent += 2;
     string[] keys = dictionary.Keys;
     for (int i = 0; i < keys.Length; i++)
     {
         string  key = keys[i];
         JObject obj = dictionary[key];
         if (obj == null)
         {
             writer.WriteLine($"{key}:null,");
         }
         else if (obj is JPrimitive p)
         {
             writer.WriteLine($"{key}:{p},");
         }
         else
         {
             DumpObject(writer, key, obj);
         }
     }
     writer.Indent -= 2;
     writer.WriteLine("}");
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 連想配列のデータを読み込む。
        /// </summary>
        /// <param name="reader">読み込み元</param>
        /// <returns>連想配列</returns>
        private static JDictionary ReadDictionary(LocalReader reader)
        {
            // 最初の1文字読み捨て。'{'
            reader.Consume();

            JDictionary dictionary = new JDictionary();
            string      key        = null;
            JObject     obj        = null;

            while (!reader.EndOfStream)
            {
                char c = reader.Peek();
                if (IsSpace(c))
                {
                    reader.Consume(); // Consume.
                }
                else if (c == ',')
                {
                    reader.Consume(); // Consume
                    if (!string.IsNullOrEmpty(key))
                    {
                        dictionary[key] = obj;
                    }
                    key = null;
                    obj = null;
                }
                else if (c == '}')
                {
                    reader.Consume(); // Consume.
                    if (!string.IsNullOrEmpty(key))
                    {
                        dictionary[key] = obj;
                    }
                    break;
                }
                else if (c == ':')
                {
                    if (string.IsNullOrEmpty(key))
                    {
                        throw new Exception("key not specified.");
                    }
                    reader.Consume();
                }
                else
                {
                    if (key == null)
                    {
                        key = ReadKeyString(reader);
                    }
                    else
                    {
                        obj = ReadObject(reader);
                    }
                }
            }

            return(dictionary);
        }