public void TestReplaceDictionary() { var doc = new MutableDocument("doc1"); var profile1 = new MutableDictionaryObject(); profile1.SetString("name", "Scott Tiger"); doc.SetDictionary("profile", profile1); doc.GetDictionary("profile").ShouldBeEquivalentTo(profile1, "because that is what was set"); var profile2 = new MutableDictionaryObject(); profile2.SetString("name", "Daniel Tiger"); doc.SetDictionary("profile", profile2); doc.GetDictionary("profile").ShouldBeEquivalentTo(profile2, "because that is what was set"); profile1.SetInt("age", 20); profile1.GetString("name").Should().Be("Scott Tiger", "because profile1 should be detached now"); profile1.GetInt("age").Should().Be(20, "because profile1 should be detached now"); profile2.GetString("name").Should().Be("Daniel Tiger", "because profile2 should be unchanged"); profile2.GetValue("age").Should().BeNull("because profile2 should be unchanged"); Db.Save(doc); var gotDoc = Db.GetDocument("doc1"); gotDoc.GetDictionary("profile") .Should() .NotBeSameAs(profile2, "because a new MutableDocument should return a new instance"); var savedProfile2 = gotDoc.GetDictionary("profile"); savedProfile2.GetString("name").Should().Be("Daniel Tiger", "because that is what was saved"); }
public void TestGetValueFromNewEmptyDictionary() { DictionaryObject dict = new MutableDictionaryObject(); dict.GetInt("key").Should().Be(0, "because that is the default value"); dict.GetLong("key").Should().Be(0L, "because that is the default value"); dict.GetDouble("key").Should().Be(0.0, "because that is the default value"); dict.GetBoolean("key").Should().Be(false, "because that is the default value"); dict.GetDate("key").Should().Be(DateTimeOffset.MinValue, "because that is the default value"); dict.GetBlob("key").Should().BeNull("because that is the default value"); dict.GetValue("key").Should().BeNull("because that is the default value"); dict.GetString("key").Should().BeNull("because that is the default value"); dict.GetDictionary("key").Should().BeNull("because that is the default value"); dict.GetArray("key").Should().BeNull("because that is the default value"); dict.ToDictionary().Should().BeEmpty("because the dictionary is empty"); var doc = new MutableDocument("doc1"); doc.SetDictionary("dict", dict); Db.Save(doc); var gotDoc = Db.GetDocument("doc1"); dict = gotDoc.GetDictionary("dict"); dict.GetInt("key").Should().Be(0, "because that is the default value"); dict.GetLong("key").Should().Be(0L, "because that is the default value"); dict.GetDouble("key").Should().Be(0.0, "because that is the default value"); dict.GetBoolean("key").Should().Be(false, "because that is the default value"); dict.GetDate("key").Should().Be(DateTimeOffset.MinValue, "because that is the default value"); dict.GetBlob("key").Should().BeNull("because that is the default value"); dict.GetValue("key").Should().BeNull("because that is the default value"); dict.GetString("key").Should().BeNull("because that is the default value"); dict.GetDictionary("key").Should().BeNull("because that is the default value"); dict.GetArray("key").Should().BeNull("because that is the default value"); dict.ToDictionary().Should().BeEmpty("because the dictionary is empty"); }
public void JsonApiDictionary() { var ourdbname = "ournewdb"; if (Database.Exists(ourdbname, "/")) { Database.Delete(ourdbname, "/"); } Database dbNew = new Database(ourdbname); // tag::tojson-dictionary[] // Get dictionary from JSONstring var aJSONstring = "{'id':'1002','type':'hotel','name':'Hotel Ned','city':'Balmain','country':'Australia','description':'Undefined description for Hotel Ned','features':['Cable TV','Toaster','Microwave']}".Replace("'", "\""); var myDict = new MutableDictionaryObject(json: aJSONstring); // <.> // use dictionary to get name value var name = myDict.GetString("name"); // Iterate through keys foreach (string key in myDict.Keys) { System.Console.WriteLine("Data -- {0} = {1}", key, myDict.GetValue(key).ToString()); } // end::tojson-dictionary[] /* * // tag::tojson-dictionary-output[] * * mono-stdout: Data -- id = 1002 * mono-stdout: Data -- type = hotel * mono-stdout: Data -- name = Hotel Ned * mono-stdout: Data -- city = Balmain * mono-stdout: Data -- country = Australia * mono-stdout: Data -- description = Undefined description for Hotel Ned * mono-stdout: Data -- features = Couchbase.Lite.MutableArrayObject * * // end::tojson-dictionary-output[] */ } /* end of func */