Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

Gibrish with non-English characters (using the C# client and Coucbase 2.0 beta)

1 reply [Last post]
  • Login or register to post comments
Wed, 10/31/2012 - 13:19
Idan
Offline
Joined: 05/18/2012
Groups: None

I am using .NET JSON serialization to put JSON documents inside a couchbase bucket. The problem is that when I use non-English characters like this:

name = " الفورية مترجم نصوص مجاني إلى ",

I get gibrish. It doesn't dtore the utf-8 characters as it should. Is it the .NET seralizer problem or anything else

I use the following code that you recommended using:

public static class CouchbaseClientExtensions
{

public static bool StoreJson(this CouchbaseClient client, StoreMode storeMode, string key, T value) where T : class
{
var ms = new MemoryStream();
var serializer = new DataContractJsonSerializer(typeof(T));
serializer.WriteObject(ms, value);
var json = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return client.Store(storeMode, key, json);
}

public static T GetJson(this CouchbaseClient client, string key) where T : class
{
var json = client.Get(key);
var ms = new MemoryStream(Encoding.Default.GetBytes(json));
var serializer = new DataContractJsonSerializer(typeof(T));
var obj = serializer.ReadObject(ms) as T;
ms.Dispose();
return obj;
}
}

Top
  • Login or register to post comments
Fri, 11/02/2012 - 08:04
john
Offline
Joined: 01/05/2012
Groups: None

Hi Idanm

The .NET client stores strings using UTF-8. Are you seeing the data coming out of the client correctly, or are the mixed up strings in the admin console?

Also, you can try the new extension methods that use Newtonsoft.JSON for Json serialization. In the .NET Couchbase Client Beta, you'll find Couchbase.Extensions with the following class:

public static class CouchbaseClientExtensions
{
    public static bool StoreJson(this CouchbaseClient client, StoreMode storeMode, string key, object value)
    {
        var json = JsonConvert.SerializeObject(value);
        return client.Store(storeMode, key, json);
    }
 
    public static T GetJson<T>(this CouchbaseClient client, string key) where T : class
    {
        var json = client.Get<string>(key);
        return json == null ? null : JsonConvert.DeserializeObject<T>(json);
    }
}

It seems possible that the Encoding.Default.GetBytes in the extensions you're using might be at fault here...

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker