public void JsonApiDocument()
        {
            // tag::tojson-document[]
            Database this_DB = new Database("travel-sample");
            Database newDb   = new Database("ournewdb");

            // Get a document
            var thisDoc = this_Db.GetDocument("hotel_10025");

            // Get document data as JSON String
            var thisDocAsJsonString = thisDoc?.ToJSON(); // <.>

            // Get Json Object from the Json String
            JObject myJsonObj = JObject.Parse(thisDocAsJsonString);

            // Get Native Object (anhotel) from JSON String
            List <Hotel> thehotels = new List <Hotel>();

            Hotel anhotel = new Hotel();

            anhotel = JsonConvert.DeserializeObject <Hotel>(thisDocAsJsonString);
            thehotels.Add(anhotel);

            // Update the retrieved native object
            anhotel.Name = "A Copy of " + anhotel.Name;
            anhotel.Id   = "2001";

            // Convert the updated object back to a JSON string
            var newJsonString = JsonConvert.SerializeObject(anhotel);

            // Update new document with JSOn String
            MutableDocument newhotel =
                new MutableDocument(anhotel.Id, newJsonString); // <.>

            foreach (string key in newhotel.ToDictionary().Keys)
            {
                System.Console.WriteLine("Data -- {0} = {1}",
                                         key, newhotel.GetValue(key));
            }

            newDb.Save(newhotel);

            var thatDoc = newDb.GetDocument("2001").ToJSON(); // <.>

            System.Console.Write(thatDoc);

            // end::tojson-document[]


            //    // tag::tojson-document-output[]
            //    JSON String = { "description":"Very good and central","id":"1000","country":"France","name":"Hotel Ted","type":"hotel","city":"Paris"}
            //             type = hotel
            //             id = 1000
            //             country = France
            //             city = Paris
            //             description = Very good and central
            //             name = Hotel Ted
            //        // end::tojson-document-output[]
            //         */
        } // End JSONAPIDocument
        public void TestConflict()
        {
            ConflictResolver = new TheirsWins();
            ReopenDB();
            var doc1      = SetupConflict();
            var savedDoc1 = Db.Save(doc1);

            savedDoc1["name"].ToString().Should().Be("Scotty", "because the 'theirs' version should win");
            doc1.Dispose();
            savedDoc1.Dispose();

            ConflictResolver = new MergeThenTheirsWins();
            ReopenDB();

            var doc2 = new MutableDocument("doc2");

            doc2.SetString("type", "profile");
            doc2.SetString("name", "Scott");
            var savedDoc2 = Db.Save(doc2);

            // Force a conflict again
            var properties = doc2.ToDictionary();

            properties["type"]   = "bio";
            properties["gender"] = "male";
            SaveProperties(properties, doc2.Id);
            doc2.Dispose();

            // Save and make sure that the correct conflict resolver won
            doc2 = savedDoc2.ToMutable();
            doc2.SetString("type", "bio");
            doc2.SetInt("age", 31);

            savedDoc2.Dispose();
            savedDoc2 = Db.Save(doc2);
            doc2.Dispose();

            savedDoc2["age"].Long.Should().Be(31L, "because 'age' was changed by 'mine' and not 'theirs'");
            savedDoc2["type"].String.Should().Be("bio", "because 'type' was changed by 'mine' and 'theirs' so 'theirs' should win");
            savedDoc2["gender"].String.Should().Be("male", "because 'gender' was changed by 'theirs' but not 'mine'");
            savedDoc2["name"].String.Should().Be("Scott", "because 'name' was unchanged");
            savedDoc2.Dispose();
        }
        private MutableDocument SetupConflict()
        {
            var doc = new MutableDocument("doc1");

            doc.SetString("type", "profile");
            doc.SetString("name", "Scott");
            var savedDoc = Db.Save(doc);

            // Force a conflict
            var properties = doc.ToDictionary();

            properties["name"] = "Scotty";
            SaveProperties(properties, doc.Id);

            doc.Dispose();
            doc = savedDoc.ToMutable();
            doc.SetString("name", "Scott Pilgrim");
            return(doc);
        }
Exemple #4
0
        private bool ResolveConflict(MutableDocument updatedDoc, Document currentDoc)
        {
            var updateDocDict = updatedDoc.ToDictionary();
            var curDocDict    = currentDoc.ToDictionary();

            foreach (var value in curDocDict)
            {
                if (updateDocDict.ContainsKey(value.Key) && !value.Value.Equals(updateDocDict[value.Key]))
                {
                    updateDocDict[value.Key] = value.Value + ", " + updateDocDict[value.Key];
                }
                else if (!updateDocDict.ContainsKey(value.Key))
                {
                    updateDocDict.Add(value.Key, value.Value);
                }
            }

            updatedDoc.SetData(updateDocDict);
            return(true);
        }
        public void TestSetNestedDictionaries()
        {
            var doc    = new MutableDocument("doc1");
            var level1 = new MutableDictionaryObject();

            level1.SetString("name", "n1");
            doc.SetDictionary("level1", level1);

            var level2 = new MutableDictionaryObject();

            level2.SetString("name", "n2");
            level1.SetDictionary("level2", level2);

            var level3 = new MutableDictionaryObject();

            level3.SetString("name", "n3");
            level2.SetDictionary("level3", level3);

            doc.GetDictionary("level1").ShouldBeEquivalentTo(level1, "because that is what was inserted");
            level1.GetDictionary("level2").ShouldBeEquivalentTo(level2, "because that is what was inserted");
            level2.GetDictionary("level3").ShouldBeEquivalentTo(level3, "because that is what was inserted");
            var dict = new Dictionary <string, object> {
                ["level1"] = new Dictionary <string, object> {
                    ["name"]   = "n1",
                    ["level2"] = new Dictionary <string, object> {
                        ["name"]   = "n2",
                        ["level3"] = new Dictionary <string, object> {
                            ["name"] = "n3"
                        }
                    }
                }
            };

            doc.ToDictionary().ShouldBeEquivalentTo(dict, "because otherwise the document's contents are incorrect");

            Db.Save(doc);
            var gotDoc = Db.GetDocument("doc1");

            gotDoc.GetDictionary("level1").Should().NotBeSameAs(level1);
            gotDoc.ToDictionary().ShouldBeEquivalentTo(dict);
        }