Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: .NET (C#) 1.2
Community Wiki and Resources
Wiki: .NET Client Library
Download Client Library
.NET Client Library
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
1.3 Try it Out!
Chapter Sections
Chapters

1.3.6. CouchbaseClient JSON Extension Methods

If you want an easy way to read and write JSON, the CouchbaseClientExtensions class under the Couchbase.Extensions namespace provides two very basic methods, StoreJson and GetJson. Both methods depend on the open source Newtonsoft.Json library, which is already a dependency of the Couchbase .NET Library. Both methods wrap only the most basic Get and Store overloads and don't currently support CAS or TTL operations. They are included with the library for convenience and will likely be augmented in the future by a Couchbase Labs extension library.

To improve the way beer data is managed in this getting started project, add a new file Beer.cs to the project. It will contain a plain-old-CLR-object (POCO) with mappings from class properties to JSON properties. For brevity, some document properties have been omitted. Notice also that the Type property has been made read-only and forces all beer instances to be marked with the type "beer." This type information will be useful when creating views and wanting to find all "beer" documents.

public class Beer
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("abv")]
    public float ABV { get; set; }

    [JsonProperty("type")]
    public string Type
    {
       get { return "beer"; }
    }

    [JsonProperty("brewery_id")]
    public string BreweryId { get; set; }

    [JsonProperty("style")]
    public string Style { get; set; }

    [JsonProperty("category")]
    public string Category { get; set; }
}

By default, Json.NET will serialize the properties of your class in the case you created them. Because we want our properties to match the casing of the documents in the beer-sample bucket, we're going to set JSON property names in JsonProperty attributes (in the Newtonsoft.Json namespace). Again, we could store instances of this Beer class without converting them first to JSON (requires marking the class with a Serializable attribute), but that would prevent those documents from being indexed in views.

Persisting an instance as JSON is similar to how we persisted the JSON document string above. Replace the code where a JSON string was created with the code below.

var newBeer = new Beer
{
    Name = "Old Yankee Ale",
    ABV = 5.00f,
    BreweryId = "cottrell_brewing",
    Style = "American-Style Amber",
    Category = "North American Ale"
};

And to store the new instance, simply use the extension method. Result will return a Boolean indicating operation success.

var result = client.StoreJson(StoreMode.Set, key, newBeer);

Retrieving the Beer instance is also similar to retrieving a document as was demonstrated above.

var savedBeer = client.GetJson<beer><Beer>(key);</beer>

At this point, your simple Program.cs file should look something like the following:

class Program
{
    static void Main(string[] args)
    {
        var client = new CouchbaseClient();
        var key = "cottrell_brewing-old_yankee_ale";

        var newBeer = new Beer
        {
            Name = "Old Yankee Ale",
            ABV = 5.00f,
            BreweryId = "cottrell_brewing",
            Style = "American-Style Amber",
            Category = "North American Ale"
        };

        var result = client.StoreJson(StoreMode.Set, key, newBeer);

        if (result)
        {
            var savedBeer = client.GetJson<Beer>(key);
            Console.WriteLine("Found beer: " + savedBeer.Name);
        }

    }
}