#How to deserialize this

1 messages · Page 1 of 1 (latest)

fossil stratus
#

I save my tiles in my tilemap as a string. I serialize it like this: tileId(posX, posY)tileId(posX, posY)tileId(posX, posY) ...... and so on

Like this: 11(35,-13)11(36,-13)11(37,-13)11(38,-13)11(39,-13)...

How do I deserialize? I couldnt figure out

` public string Serialize() {
var builder = new StringBuilder();

    builder.Append("main[");
    foreach (var mainTile in MainTiles) {

     
        builder.Append($"{(int) mainTile.Tile.id}({mainTile.Position.x},{mainTile.Position.y})");
    }
    builder.Append("]");
    builder.Append("background[");
    foreach (var bgTile in BackgroundTiles)
    {
        builder.Append($"{(int)bgTile.Tile.id}({bgTile.Position.x},{bgTile.Position.y})");
    }
    builder.Append("]");

    return builder.ToString();
}`
red drift
#
  1. Your format clearly includes more than tileId(tilePositionX,tilePositionY), considering it starts with main[ and ends with ]. Those also are things you need to consider during deserialization.
  2. I would strongly recommend you simply use an established format (like json, yaml, xml, ini or plain binary), instead of coming up with your own.

If you definitely want to make your own format for whatever reason, first consider what kinds of delimiters you need and how to interpret them. Also consider possible pitfalls.

For example, in your case, deserialization could look somewhat like this: ```
deserialize_block(value)
read "value" until first '['
store last value as key ("last value" would in this case be "main")
read until first ']'

deserialize_tiles(value)
while not on last character
read until first '('
store current value as tile id (current value would be "11", for example)
read until first ')'
trim '(' and ')' from current value
split current value by delimiter ','
parse first item as pos x
parse second item as pos y
```Note that you are implicitly calling ToString() on your tile's positions' components during serialization. If those are non-integral numbers (like floats), you will get different results depending on your system's Culture. Some will return something like 10.5, others 10,5. That makes using , as number delimiter unsafe, unless you force ToString to use a fixed culture that defines a custom decimal delimiter for decimal numbers.

fossil stratus
# red drift 1. Your format clearly includes more than `tileId(tilePositionX,tilePositionY)`,...

Thanks for the help man! Reason I'm using a custom format like this is because I send this over network to other players (multiplayer) so I need it to be as compressed as possible. The problem is I achieved the code through a tutorial and don't know how to do Deserialize myself 😂 I also forgot to mention i have 2 tilemaps; one is main and other is background (no colliders) so this string contains info for both. I get the algorithm you sent but I guess i need to research a bit more about string operators so i can implement it. Appreciate it Though !

red drift
#

"as compressed as possible" would be raw bytes. Not a string.

#

Because then you don't even need delimiters (except for strings), because each primitive value you can write to a byte stream directly has a fixed byte size.

fossil stratus
#

So I should compress this with MemoryStream or BinaryWriter or smth like that?

red drift
#

It's less compression and more just writing bytes without overhead.
And yeah, using a BinaryWriter to write to a MemoryStream is the best way to go about this.
Keep in mind that you'll need to Dispose of the stream/writer after you're done writing.

wicked raven
#

Better compression than JSON, I'll give you that 😄