Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

NCBC-155: Default to camelCase properties for JSON extensions

Change-Id: Id0564fa93e1ae49ffea4ddc9af1cd531b005a12d
Reviewed-on: http://review.couchbase.org/22520
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: John C. Zablocki <john@couchbase.com>
  • Loading branch information...
commit 6397aea8ed1a9de7ecae7f8700da1ed283e741f9 1 parent d4f8de4
@johnzablocki johnzablocki authored John C. Zablocki committed
View
1  src/Couchbase.Tests/Couchbase.Tests.csproj
@@ -51,6 +51,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigHelperTests.cs" />
+ <Compile Include="CouchbaseClientExtensionsTests.cs" />
<Compile Include="CouchbaseClientSpatialViewTests.cs" />
<Compile Include="HelperTests\DocHelperTests.cs" />
<Compile Include="HttpClientConfigTests.cs" />
View
70 src/Couchbase.Tests/CouchbaseClientExtensionsTests.cs
@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using Couchbase.Extensions;
+using Enyim.Caching.Memcached;
+
+namespace Couchbase.Tests
+{
+ [TestFixture]
+ public class CouchbaseClientExtensionsTests : CouchbaseClientTestsBase
+ {
+ [Test]
+ public void When_Serializing_Class_Without_Json_Property_Attributes_Properties_Are_Camel_Cased()
+ {
+ var thing = new Thing();
+ var key = GetUniqueKey();
+ var result = _Client.StoreJson(StoreMode.Set, key, thing);
+
+ Assert.That(result, Is.True, "Store failed");
+
+ var savedThing = _Client.Get<string>(key);
+ Assert.That(savedThing, Is.StringContaining("someProperty").And.StringContaining("someOtherProperty"));
+ }
+
+ [Test]
+ public void When_Deserializing_Class_Without_Json_Property_Attributes_Camel_Cased_Properties_Are_Mapped()
+ {
+ var thing = new Thing { SomeProperty = "foo", SomeOtherProperty = 1 };
+ var key = GetUniqueKey();
+ var result = _Client.StoreJson(StoreMode.Set, key, thing);
+
+ Assert.That(result, Is.True, "Store failed");
+
+ var savedThing = _Client.GetJson<Thing>(key);
+ Assert.That(savedThing.SomeProperty, Is.StringMatching("foo"));
+ Assert.That(savedThing.SomeOtherProperty, Is.EqualTo(1));
+
+ }
+ }
+
+ internal class Thing
+ {
+ public string SomeProperty { get; set; }
+
+ public int SomeOtherProperty { get; set; }
+ }
+}
+
+#region [ License information ]
+/* ************************************************************
+ *
+ * @author Couchbase <info@couchbase.com>
+ * @copyright 2012 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ************************************************************/
+#endregion
View
8 src/Couchbase/Extensions/CouchbaseClientExtensions.cs
@@ -5,6 +5,7 @@
using Enyim.Caching.Memcached;
using System.IO;
using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
namespace Couchbase.Extensions
{
@@ -12,7 +13,12 @@ public static class CouchbaseClientExtensions
{
public static bool StoreJson(this ICouchbaseClient client, StoreMode storeMode, string key, object value)
{
- var json = JsonConvert.SerializeObject(value);
+ var json = JsonConvert.SerializeObject(value,
+ Formatting.None,
+ new JsonSerializerSettings
+ {
+ ContractResolver = new CamelCasePropertyNamesContractResolver()
+ });
return client.Store(storeMode, key, json);
}
Please sign in to comment.
Something went wrong with that request. Please try again.