Gibrish with non-English characters (using the C# client and Coucbase 2.0 beta)
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;
}
}
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...