blob: 64b64d1840a51b860b5d5d4ce83afb23064be412 [file] [log] [blame]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine or request Chrome Frame -->
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400italic,400,600' rel='stylesheet' type='text/css'>
<!-- Use title if it's in the page YAML frontmatter -->
<title>
Serialize Your Domain Objects with PdxSerializer and PdxWrapper |
Geode Native C++ Docs
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/stylesheets/all.css" rel="stylesheet" media="screen, print" />
<link href="/stylesheets/print.css" rel="stylesheet" media="print" />
<link href='/images/favicon.ico' rel='shortcut icon'>
<script src="/javascripts/all.js"></script>
</head>
<body class="docs docs_geode-native docs_geode-native_cpp docs_geode-native_cpp_113 docs_geode-native_cpp_113_serialization docs_geode-native_cpp_113_serialization_cpp-serialization docs_geode-native_cpp_113_serialization_cpp-serialization_using-pdxserializer has-subnav">
<div class="viewport">
<div class='wrap'>
<script type="text/javascript">
document.domain = "apache.org";
</script>
<!--
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.
-->
<header class="header header-layout">
<h1 class="logo">
<a href="/">
<img src="/images/Apache_Geode_logo_symbol_white.png" style="width:30px;">
Apache Geode Native C++ Documentation
</a>
</h1>
<div class="header-links js-bar-links">
<div class="btn-menu" data-behavior="MenuMobile"></div>
<div class="header-item"><a href="http://geode.apache.org">Back to Product Page</a></div>
<div class="header-item">
<a href="http://geode.apache.org/community" target="_blank">Community</a>
</div>
</div>
</header>
<div class="container">
<!--googleoff: index-->
<!--
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.
-->
<div id="sub-nav" class="js-sidenav nav-container" role="navigation">
<a class="sidenav-title" data-behavior="SubMenuMobile">Doc Index</a>
<div class="nav-content">
<ul>
<li>
<a href="/docs/geode-native/cpp/113/about-client-users-guide.html">Apache Geode Native Documentation</a>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/cpp/113/getting-started/getting-started-nc-client.html">Getting Started with the Native Library</a>
<ul>
<li>
<a href="/docs/geode-native/cpp/113/getting-started/getting-started-nc-client.html#set_up_dev_environment">Set Up Your Development Environment</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/getting-started/getting-started-nc-client.html#establish_cluster_access">Establish Access to a Geode Cluster</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/getting-started/getting-started-nc-client.html#app_dev_walkthroughs">Application Development Walkthroughs</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/getting-started/getting-started-nc-client.html#programming_examples">Programming Examples</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/getting-started/put-get-example.html">Put/Get/Remove Examples</a>
</li>
</ul>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/cpp/113/configuring/configuration.html">Configuring a Client Application</a>
<ul>
<li>
<a href="/docs/geode-native/cpp/113/configuring/system-level-configuration.html">System Level Configuration</a>
</li>
</ul>
</li>
<li>
<a href="/docs/geode-native/cpp/113/configuring/config-client-cache.html">Configuring the Client Cache</a>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/cpp/113/regions/regions.html">Configuring Regions</a>
<ul>
<li>
<a href="/docs/geode-native/cpp/113/regions/registering-interest-for-entries.html">Registering Interest for Entries</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/regions/region-attributes.html">Region Attributes</a>
</li>
</ul>
</li>
<li>
<a href="/docs/geode-native/cpp/113/serialization/data-serialization.html">Serializing Data</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/remote-queries.html">Remote Queries</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/continuous-queries.html">Continuous Queries</a>
</li>
<li class="has_submenu">
<a href="/docs/geode-native/cpp/113/security/security.html">Security: Authentication and Encryption</a>
<ul>
<li>
<a href="/docs/geode-native/cpp/113/security/authentication.html">Authentication</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/security/sslclientserver.html">TLS/SSL Client-Server Communication Encryption</a>
</li>
</ul>
</li>
<li>
<a href="/docs/geode-native/cpp/113/function-execution.html">Function Execution</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/transactions.html">Transactions</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/configuring/sysprops.html">System Properties</a>
</li>
<li>
<a href="/docs/geode-native/cpp/113/client-cache-ref.html">Client Cache XML Reference</a>
</li>
</ul>
</div>
</div>
<!--googleon: index-->
<main class="content content-layout" id="js-content" role="main">
<a id="top"></a>
<!--
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.
-->
<span style="font-weight:200;font-size:31px;" style="float:left;">
<img src="/images/Apache_Geode_logo_symbol.png" style="height:26px;">
Apache Geode Native C++
</span>
<span class="local-header version-info" style="float:right;">
<a href="https://cwiki.apache.org/confluence/display/GEODE/Release+Notes">CHANGELOG</a>
</span>
<!--
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.
-->
<h1 class="title-container" >
Serialize Your Domain Objects with PdxSerializer and PdxWrapper
</h1>
<div id="js-quick-links" >
</div>
<div class="to-top" id="js-to-top">
<a href="#top" title="back to top"></a>
</div>
<!--
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.
-->
<p>For domain objects that you cannot or do not want to modify, use the <code>PdxSerializer</code> and the <code>PdxWrapper</code> classes to serialize and deserialize the object&rsquo;s fields.</p>
<p>You register a <code>PdxSerializer</code> implementation for the entire cache, programming it for all of the domain objects that you handle in this way. This way you do not have to implement the <code>PdxSerializable</code> interface for each domain class.</p>
<p>The <code>PdxSerializer</code> allows domain classes to be serialized and deserialized as PDXs without modification of the domain class. It requires only that the domain class have a constructor accessible to the <code>PdxSerializer</code> to create an instance. The domain class will be held in a wrapper class, <code>PdxWrapper</code>.</p>
<p><code>PdxSerializer</code> has the following methods:</p>
<ul>
<li> The <code>toData</code> method returns true if the PdxSerializer was able to serialize the user object, false if not.</li>
<li> If the PdxSerializer was able to deserialize the object, the <code>fromData</code> method returns a void pointer to the user object to be wrapped in a <code>PdxWrapper</code>.</li>
</ul>
<p>When you later reference the user object, use the <code>PdxWrapper</code> class. <code>PdxWrapper</code> holds a shared reference to the object in the local cache and is used during serialization and deserialization. <code>PdxWrapper</code> acts as a container for the user domain object and needs to wrap every instance of the object that uses a registered <code>PdxSerializer</code>. The object instance will not be modified. In addition, when using <code>PdxWrapper</code>, you will need to provide a function pointer to a &ldquo;deallocator&rdquo;, which will delete the user object when the reference is no longer held.</p>
<p>The following code example defines a user object and a <code>PdxSerializer</code>. It then registers the new <code>PdxSerializer</code> and then uses <code>PdxWrapper</code> to put the object in a region and retrieve the object from a region.</p>
<pre class="highlight plaintext"><code>class UserClass
{
public:
int m_int;
string m_string;
UserClass(int intVal, string stringVal)
{
m_int = intVal;
m_string = stringVal;
}
static void deallocate(void * object, char * className)
{
if (strcmp(className, "com.example.UserClass") == 0) {
UserClass * userObject = reinterpret_cast&lt;UserClass*&gt;(object);
delete userObject;
}
}
};
class UserPdxSerializer : public PdxSerializer
{
public:
void* fromData(char * className, PdxReaderPtr pdxReader)
{
if (strcmp(className, "com.example.UserClass") != 0) {
return NULL;
}
int intVal = pdxReader-&gt;readInt("m_int");
string stringVal = pdxReader-&gt;readString("m_string");
UserClass * userObject = new UserClass(intVal, stringVal);
return (void*) userObject;
}
bool toData(void * object, char * className, PdxWriterPtr pdxWriter)
{
if (strcmp(className, "com.example.UserClass") != 0) {
return false;
}
UserClass * userObject = reinterpret_cast&lt;UserClass*&gt;(object);
pdxWriter-&gt;writeInt("m_int", userObject-&gt;m_int);
pdxWriter-&gt;writeString("m_string", userObject-&gt;m_string);
return true;
}
UserDeallocator getDeallocator(char * className)
{
if (strcmp(className, "com.example.UserClass") == 0) {
return UserClass::deallocate;
} else {
return NULL;
}
}
};
// Register a user PDX serializer
Serializable::registerPdxSerializer(new UserPdxSerializer);
// Put a user object into a region.
UserClass * userObject = new UserClass(123, "someValue");
PdxWrapperPtr pdxWrapper = new PdxWrapper(userObject, "com.example.UserClass",
UserClass::deallocate);
region-&gt;put("key", pdxWrapper);
// Get a user object from a region.
pdxWrapper = dynCast&lt;PdxWrapperPtr&gt;(region-&gt;get("key"));
UserClass * userObject = reinterpret_cast&lt;UserClass*&gt;(pdxWrapper-&gt;getObject());
</code></pre>
</main>
</div>
</div>
</div>
<div id="scrim"></div>
<div class="container">
<footer class="site-footer-links">
<!--
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.
-->
<div class="copyright">
<a href='/'>Apache Geode Documentation</a>
&copy; 2020 <a href='http://www.apache.org/'>The Apache Software Foundation</a>.
</div>
<div class="support">
Need help? <a href="http://geode.apache.org/community" target="_blank">Visit the Community</a>
</div>
</footer>
</div><!--end of container-->
</body>
</html>