Skip to content

Commit

Permalink
custom serializer example
Browse files Browse the repository at this point in the history
  • Loading branch information
asimarslan committed Jun 19, 2017
1 parent 3351c85 commit 985de41
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions Hazelcast.Examples/Hazelcast.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="Collections\SetExample.cs" />
<Compile Include="Collections\ListExample.cs" />
<Compile Include="Map\MapAsyncExample.cs" />
<Compile Include="Map\MapCustomSerializerExample.cs" />
<Compile Include="Map\MapEntryProcessorExample.cs" />
<Compile Include="Map\MapIdentifiedDataSerializableExample.cs" />
<Compile Include="Map\MapListenerExample.cs" />
Expand Down
134 changes: 134 additions & 0 deletions Hazelcast.Examples/Map/MapCustomSerializerExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
//
// 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.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Hazelcast.Config;
using Hazelcast.Client;
using Hazelcast.Core;
using Hazelcast.IO;
using Hazelcast.IO.Serialization;

namespace Hazelcast.Examples.Map
{
public class MapCustomSerializerExample
{
public static void Run(string[] args)
{
var clientConfig = new ClientConfig();
clientConfig.GetNetworkConfig().AddAddress("127.0.0.1");
//clientConfig.GetNetworkConfig().AddAddress("10.0.0.2:5702");

var sc = new SerializerConfig()
.SetImplementation(new CustomSerializer())
.SetTypeClass(typeof(Person));

//Custom Serialization setup up for Person Class.
clientConfig.GetSerializationConfig().AddSerializerConfig(sc);


IHazelcastInstance client = HazelcastClient.NewHazelcastClient(clientConfig);
//All cluster operations that you can do with ordinary HazelcastInstance
IMap<string, Person> mapCustomers = client.GetMap<string, Person>("persons");
mapCustomers.Put("1", new Person("Joe", "Smith"));
mapCustomers.Put("2", new Person("Ali", "Selam"));
mapCustomers.Put("3", new Person("Avi", "Noyan"));

ICollection<Person> persons = mapCustomers.Values();
foreach (var person in persons)
{
Console.WriteLine(person.ToString());
}

HazelcastClient.ShutdownAll();
Console.ReadKey();
}
}

class CustomSerializer : IStreamSerializer<Person>
{
public int GetTypeId()
{
return 10;
}

public void Destroy()
{
//NOOP
}

public void Write(IObjectDataOutput output, Person t)
{
byte[] array;
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, t);
array = ms.ToArray();
}

output.WriteInt(array.Length);
output.Write(array);
}

public Person Read(IObjectDataInput input)
{
var bf = new BinaryFormatter();
var len = input.ReadInt();

var buffer = new byte[len];
input.ReadFully(buffer);

Person result = null;
using (var ms = new MemoryStream(buffer))
{
result = (Person) bf.Deserialize(ms);
}
return result;
}
}

[Serializable]
public class Person
{
private string name;
private string surname;

public Person(string name, string surname)
{
this.name = name;
this.surname = surname;
}


public string Name
{
get { return name; }
set { name = value; }
}

public string Surname
{
get { return surname; }
set { surname = value; }
}

public override string ToString()
{
return "Person{ name:" + name + ", surname:" + surname + " }";
}
}
}

0 comments on commit 985de41

Please sign in to comment.