blob: aa069c1275e05d7bec0a2f0bf7b1a997ea7febb8 [file] [log] [blame]
---
title: Implement the IDataSerializable Interface
---
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
To store your own data types in the cache, you implement the <%=vars.product_name%> `IDataSerializable` interface.
Examples follow the procedure.
**Procedure**
1. Implement the `ToData` function that serializes your data:
``` pre
void ToData(DataOutput output)
```
The `ToData` function is responsible for copying all of the data fields for the object to the object stream. The `DataOutput` class represents the output stream and provides methods for writing the primitives in a network byte order.
2. Implement the `FromData` function that consumes a data input stream and repopulates the data fields for the object:
``` pre
void fromData (DataInput& input)
```
The `DataInput` class represents the input stream and provides methods for reading input elements. The `FromData` function must read the elements of the input stream in the same order that they were written by `ToData`.
3. Implement the `ClassId` function to return an integer which is unique for your class (in the set of all of your user-defined classes).
## Simple BankAccount Class
This example shows a simple class, `BankAccount`, that encapsulates the two `ints`, `customerId` and `accountId`:
``` pre
public class BankAccount
{
private int m_customerId;
private int m_accountId;
public int Customer
{
get
{
return m_customerId;
}
}
public int Account
{
get
{
return m_accountId;
}
}
public BankAccount(int customer, int account)
{
m_customerId = customer;
m_accountId = account;
}
}
```
## Implementing a Serializable Class
To make `BankAccount` serializable, you implement the `IDataSerializable` interface as shown in this example:
``` pre
public class BankAccount : IDataSerializable
{
private int m_customerId;
private int m_accountId;
public int Customer
{
get
{
return m_customerId;
}
}
public int Account
{
get
{
return m_accountId;
}
}
public BankAccount(int customer, int account)
{
m_customerId = customer;
m_accountId = account;
}
// Our TypeFactoryMethod
public static IDataSerializable CreateInstance()
{
return new BankAccount(0, 0);
}
#region IDataSerializable Members
public void ToData(DataOutput output)
{
output.WriteInt32(m_customerId);
output.WriteInt32(m_accountId);
}
public void FromData(DataInput input)
{
m_customerId = input.ReadInt32();
m_accountId = input.ReadInt32();
return this;
}
public UInt32 ClassId
{
get
{
return 11;
}
}
public UInt32 ObjectSize
{
get
{
return (UInt32)(sizeof(Int32) + sizeof(Int32));
}
}
}
```