Component Documentation : Cross-Language Clients User Guide : BigMemory .NET Client : .NET Client Serialization
.NET Client Serialization
Cross-Language Serialization
Data that will be shared across languages needs to be serialized. The choice of which serializer to use depends upon your environment. Some planning and consideration needs to go into the choice of a serializer, as there are pros and cons related to each. For example, with a BSON serializer, you can manually annotate the domain object for desired attributes. On the other hand, a Google Protocol Buffer serializer can automatically generate the domain object with the annotation for the serializer for you, however, if you have an existing domain object, then using the automatic generation can be intrusive.
Your serializer/deserializer class should include the following:
*A list of all the namespaces/packages that will be used.
*A class definition consisting of the following methods/functions:
*serializeKey() - accepts the key and returns the platform-neutral format (PNF) Value
*deserializeKey() - accepts the PNF Value and returns the key
*serializeValue() - accepts the object value and returns the PNF StoredValue
*deserializeValue() - accepts the PNF StoredValue and returns the object value
Example BSON serializer
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Terracotta.Ehcache;
using Terracotta.Ehcache.Thrift;
Class definition: serializeKey()
public Terracotta.Ehcache.Thrift.Value serializeKey(string key)

{

return ValueHelper.newValue(key);
}
Class definition: deserializeKey()
public string deserializeKey(Terracotta.Ehcache. Thrift.Value serializedKey)

{

return (string)ValueHelper.findSetValue (serializedKey);
}
Class definition: serializeValue()
public StoredValue serializeValue(Recipe2 objectValue)

{

MemoryStream ms = new MemoryStream();

// serialize recipe to BSON

BsonWriter writer = new BsonWriter(ms);

JsonSerializer serializer = new JsonSerializer();

serializer.Serialize(writer, objectValue);

byte[] barray = ms.ToArray();

Dictionary<String, Object> nvPairs = new Dictionary<String, Object>();

// two indexable attributes

nvPairs["name"] = objectValue.Name;

nvPairs["bakingTime"] = objectValue.BakingTime;

// ship the bson version of the recipe as a byte array

return ValueHelper.nullSafeStoredValue(barray, nvPairs);
}
Class definition: deserializeValue()
public Recipe2 deserializeValue (Terracotta.Ehcache.Thrift.StoredValue value)

{

MemoryStream ms = new MemoryStream(value.Value. BinaryValue);

ms.Seek(0, SeekOrigin.Begin);

BsonReader reader = new BsonReader(ms);

JsonSerializer serializer = new JsonSerializer();

Recipe2 recipe = serializer.Deserialize <Recipe2>(reader);

return recipe;
}
Example Google Protocol Buffer Serializer
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Terracotta.Ehcache;
using Terracotta.Ehcache.Thrift;
Class definition: serializeKey()
public Value serializeKey(string key) { Value val = new Value(); val.StringValue = key; return val; }
public Value serializeKey(string key)

{

Value val = new Value();

val.StringValue = key;

return val;
}
Class definition: deserializeKey()
public string deserializeKey(Value serializedKey)

{

return serializedKey.StringValue;


}
Class definition: serializeValue()
public StoredValue serializeValue(RecipeProject.RecipeStructure objectValue)

{

Dictionary<String, Object> nvPairs = new

Dictionary<String, Object>();

nvPairs.Add(KEY_NAME, objectValue.name);

MemoryStream ms = new MemoryStream();

ProtoBuf.Serializer.Serialize<RecipeProject.

RecipeStructure>(ms,objectValue);

return ValueHelper.nullSafeStoredValue(ms.ToArray(), nvPairs);
}
Class definition: deserializeValue()
public RecipeProject.RecipeStructure deserializeValue (StoredValue value)

{

MemoryStream ms = new MemoryStream(value.Value.BinaryValue);

return ProtoBuf.Serializer.Deserialize<RecipeProject. RecipeStructure>(ms);


}
Single-Language Serialization
For single-language use cases, it is not necessary to create a serializer/deserializer class. Leverage .NET's native binary serialization by including the provided CSharpNativeSerializer class in cache operations. For example:
ICachemanager manager = XPlatform.CreateCacheManager(...);
ICache cache<string,string> = manage.GetCache("cachename",
new CSharpNativeSerializer<string,string>())
API documentation for the CSharpNativeSerializer is available in the /apis/csharp/apidoc/ directory of the kit. For more information about .NET native serialization, refer to http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.90%29.aspx.
Copyright © 2010-2017 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback