| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>Serialization system</title><link rel="stylesheet" href="./asciidoctor.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/spidermonkey/tutorial/serializing.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/spidermonkey/tutorial/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"><i class="fa fa-navicon" aria-hidden="true"></i></div><h1>Serialization system</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#writing-your-own-serializer">Writing your own serializer</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="admonitionblock warning"><table><tr><td class="icon"><i class="fa icon-warning" title="Warning"></i></td><td class="content"><div class="paragraph"><p>This article covers a deprecated <abbr title="Application Programming Interface">API</abbr>! See <a href="../../jme3/advanced/networking.html">networking</a> for current documentation.</p></div></td></tr></table></div>
- <div class="paragraph"><p>In this lesson you’ll learn about a pretty advanced system of SpiderMonkey. Why so early, you may ask; it’s because it is an important aspect of SpiderMonkey, that you need to understand to effectively network your games.</p></div>
- <div class="paragraph"><p>Let’s start with a general explanation of how the serialization system works. SpiderMonkey’s Serializer class is the entry point for everything serializing. Serializing is the act of translating an object into bytes, so it can be transferred over the network. SpiderMonkey does this by having Serializer classes (they extend Serializer itself), and having some static methods available in the Serializer class. A serializer does not exist without a class it can serialize - this means that Serializers need to be registered with a class. For example, the String type is registered to the StringSerializer class. Without the String type being registered, there’d be no instance of StringSerializer. So! Let’s get down to business!</p></div></div></div>
- <div class="sect1"><h2 id="writing-your-own-serializer">Writing your own serializer</h2><div class="sectionbody"><div class="paragraph"><p>There is going to be a situation where you need to serialize something yourself, whether you like it or not. We’re going through how you’re going to do, by writing a entirely new Serializer - please note that this Serializer is not necessary in SpiderMonkey, since SpiderMonkey can serialize Serializable, and InetAddress4 implements Serializable (though it does save a LOT of bytes by doing it yourself). The field that makes an InetAddress4 an InetAddress4 is the IP address, so that’s what we’re going to serialize. Let’s start by going through the basics of extending the Serializer class:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">Inet4AddressSerializer</span> <span class="directive">extends</span> Serializer {
- <span class="directive">public</span> <span class="predefined-type">InetAddress</span> readObject(<span class="predefined-type">ByteBuffer</span> data, <span class="predefined-type">Class</span> c) <span class="directive">throws</span> <span class="exception">IOException</span>
- <span class="keyword">return</span> <span class="predefined-constant">null</span>;
- }
- <span class="directive">public</span> <span class="type">void</span> writeObject(<span class="predefined-type">ByteBuffer</span> buffer, <span class="predefined-type">Object</span> object) <span class="directive">throws</span> <span class="exception">IOException</span> {
- <span class="predefined-type">InetAddress</span> address = (<span class="predefined-type">InetAddress</span>)object;
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>As you can see, you have to extend Serializer and implement the methods T readObject(ByteBuffer, Class) and writeObject(ByteBuffer, Object). These are the methods that actually do the job. Obviously, writeObject is used when sending, and readObject is used when reading. The next part is just Java coding - you just kind of have to know the <abbr title="Application Programming Interface">API</abbr> of those objects you’re serializing to convert into bytes. This one’s really simple though ;)</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">Inet4AddressSerializer</span> <span class="directive">extends</span> Serializer {
- <span class="annotation">@Override</span>
- <span class="directive">public</span> <T> T readObject(<span class="predefined-type">ByteBuffer</span> data, <span class="predefined-type">Class</span><T> c) <span class="directive">throws</span> <span class="exception">IOException</span> {
- <span class="type">byte</span><span class="type">[]</span> address = <span class="keyword">new</span> <span class="type">byte</span>[<span class="integer">4</span>];
- data.get(address);
- <span class="keyword">return</span> (T)<span class="predefined-type">Inet4Address</span>.getByAddress(address);
- }
- <span class="annotation">@Override</span>
- <span class="directive">public</span> <span class="type">void</span> writeObject(<span class="predefined-type">ByteBuffer</span> buffer, <span class="predefined-type">Object</span> object) <span class="directive">throws</span> <span class="exception">IOException</span> {
- <span class="predefined-type">Inet4Address</span> address = (<span class="predefined-type">Inet4Address</span>)object;
- buffer.put(address.getAddress());
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>So now you’ve got this serializer, and you don’t know what to do with it. Well, you need to register it to a class, and what other class would you want to register it to, than Inet4Address?</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>Serializer.registerClass(Inet4Address.class, new Inet4AddressSerializer());</code></pre></div></div>
- <div class="paragraph"><p>And now you can use the Inet4Address anywhere in a Message! Now we’ll test this Serializer, and see if we can get the IP on the other side:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">class</span> <span class="class">AddressMessage</span> <span class="directive">extends</span> Message {
- <span class="directive">public</span> <span class="predefined-type">Inet4Address</span> addr;
- <span class="directive">public</span> AddressMessage(<span class="predefined-type">Inet4Address</span> addr) { <span class="local-variable">this</span>.addr = addr; }
- }
- ...
- client.send(<span class="keyword">new</span> AddressMessage(<span class="predefined-type">Inet4Address</span>.getByName(<span class="string"><span class="delimiter">"</span><span class="content">google.com</span><span class="delimiter">"</span></span>)));</code></pre></div></div>
- <div class="paragraph"><p>Results in a message being received, which prints out as:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>/66.102.13.106</code></pre></div></div>
- <div class="paragraph"><p>And there you go, that’s the end of this tutorial!</p></div>
- <div class="paragraph"><p>Next tutorial you’ll learn about a simple, but powerful feature - compression.</p></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2017-10-10 02:51:28 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
- apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
- indexName: 'jmonkeyengine',
- inputSelector: '#doc-search',
- debug: false // Set debug to true if you want to inspect the dropdown
- });</script></body></html>
|