コード例 #1
0
 public void Path()
 {
     Assert.That(writer.NextEntryPath, Is.EqualTo(""));
     writer.StartObject();
     Assert.That(writer.NextEntryPath, Is.EqualTo("???"));
     writer.AddProperty("a");
     Assert.That(writer.NextEntryPath, Is.EqualTo("a"));
     writer.WriteValue().Null();
     Assert.That(writer.NextEntryPath, Is.EqualTo("???"));
     writer.AddProperty("b");
     Assert.That(writer.NextEntryPath, Is.EqualTo("b"));
     writer.StartObject();
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.???"));
     writer.AddProperty("c");
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c"));
     writer.StartArray(TrwValueType.Undefined);
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[0]"));
     writer.WriteValue().SInt32(123);
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[1]"));
     writer.StartObject();
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[1].???"));
     writer.AddProperty("d");
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[1].d"));
     writer.StartArray(TrwValueType.Float64);
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[1].d[0]"));
     writer.EndArray();
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[1].???"));
     writer.EndObject();
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.c[2]"));
     writer.EndArray();
     Assert.That(writer.NextEntryPath, Is.EqualTo("b.???"));
     writer.EndObject();
     Assert.That(writer.NextEntryPath, Is.EqualTo("???"));
     writer.EndObject();
     Assert.That(writer.NextEntryPath, Is.EqualTo(""));
 }
コード例 #2
0
        public static void WriteDynamic(this ITrwWriter writer, object obj)
        {
            switch (obj)
            {
            case null:
                writer.WriteValue().Null();
                break;

            case bool b:
                writer.WriteValue().Bool(b);
                break;

            case int i:
                writer.WriteValue().SInt32(i);
                break;

            case double d:
                writer.WriteValue().Float64(d);
                break;

            case string s:
                writer.WriteValue().String(s);
                break;

            case IEnumerable <KeyValuePair <string, object> > dict:
                writer.StartObject();
                foreach (var kvp in dict)
                {
                    writer.AddProperty(kvp.Key);
                    writer.WriteDynamic(kvp.Value);
                }
                writer.EndObject();
                break;

            case IEnumerable <object> list:
                writer.StartArray(TrwValueType.Undefined);
                foreach (var item in list)
                {
                    writer.WriteDynamic(item);
                }
                writer.EndArray();
                break;
            }
        }