# Class Properties to/from MemoryBlock

**URL:** <https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859>\
**Category:** General\
**Created:** [May 15, 2020, 9:43pm UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859 "2020-05-15T21:43:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobby\_Kurell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@Bobby\_Kurell](https://forum.xojo.com/u/Bobby_Kurell)\
**Post date:** [May 15, 2020, 9:43pm UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859/1 "2020-05-15T21:43:07Z")

</div>

Hi there,  
Does any of you pros know if it would be possible to create a Class consisting of a number of Properties and the have a Method that runs through all properties and merges all properties data into a MemoryBlock for HTTP POST?, And then have it read back into the Class at the Server side?

Something like a binary JSON

The Class would be an external type, so that both iOS and Web APP can access the same Class

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [May 15, 2020, 9:49pm UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859/2 "2020-05-15T21:49:13Z")

</div>

[quote=488787:@Bobby Kurell]Hi there,  
Does any of you pros know if it would be possible to create a Class consisting of a number of Properties and the have a Method that runs through all properties and merges all properties data into a MemoryBlock for HTTP POST?, And then have it read back into the Class at the Server side?

Something like a binary JSON  
[/quote]

Short answer is yes… Besides coming up with a format for all data types (which i did) , to make the code general for any class one would need to use introspection to do the serialization. You also need to decide if you want to serialize all properties or just public ones.  
If one is supporting b array oaf records, depending on size you might want to Gzip te payload too… I wrote code that could either send it unzipped, All Gziped or Gzipping in blocks of x records for when memory is tight on client or server.

-karen

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 15, 2020, 10:23pm UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859/3 "2020-05-15T22:23:55Z")

</div>

Also see my Data Serialization example project:

[https://github.com/ktekinay/Data-Serialization](https://github.com/ktekinay/Data-Serialization)

---

<div class="post-metadata">

**Author:** ![Bobby\_Kurell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@Bobby\_Kurell](https://forum.xojo.com/u/Bobby_Kurell)\
**Post date:** [May 15, 2020, 10:42pm UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859/4 "2020-05-15T22:42:06Z")

</div>

[quote=488789:@Karen Atkocius]Short answer is yes… Besides coming up with a format for all data types (which i did) , to make the code general for any class one would need to use introspection to do the serialization. You also need to decide if you want to serialize all properties or just public ones.  
If one is supporting b array oaf records, depending on size you might want to Gzip te payload too… I wrote code that could either send it unzipped, All Gziped or Gzipping in blocks of x records for when memory is tight on client or server.

-karen[/quote]  
Good idea on the Public Properties in order to have ‘Internal’ variables. Perhaps I’m thinking too simple (or it might not be possible), but I was thinking:

Method ToMemBlock  
Var mb As MemoryBlock(0)  
Var bs as BinaryStream(mb)

for each p in class  
If p.Scope = Public  
Select case p.Type

Case Integer  
bs.WriteInt64  
Case Double  
bs.WriteDouble

…  
end Select  
end if

Return mb

And the have the opposite. The Issue will be Arrays or Properties with another ClassType, and the ‘For each’ needs to have the same order of Properties always (per build)

would this work?

---

<div class="post-metadata">

**Author:** ![Bernardo\_Monsalve](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@Bernardo\_Monsalve](https://forum.xojo.com/u/Bernardo_Monsalve)\
**Post date:** [May 16, 2020, 3:37am UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859/5 "2020-05-16T03:37:19Z")

</div>

> [@488787:@Bobby Kurell](#):
>
> The Class would be an external type, so that both iOS and Web APP can access the same Class

Is doable, I already had a class in a working project, I called “BinaryMessage”, but, doesn’t work on iOS because the String type, could be easy implement the Text type.

You need a way to ‘encode’ the data, you can check out my encoding [here](https://github.com/lbmonsalve/Xojo-BinaryMessage#encoded).

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [April 29, 2021, 4:24pm UTC](https://forum.xojo.com/t/class-properties-to-from-memoryblock/53859/6 "2021-04-29T16:24:42Z")

</div>


