0

I want to know if that's possible to manually parse a serialized binary which has been serialized in c#.

My end goal is to parse a serialized binary of a multi-dimensional array which has been serialized in c# and parse it in java,

I want to know if there is any algorithm/cheat-sheet that help me to understand the structure of a serialized binary?

Any pointers/hints greatly appreciated.

NOTE: I am not looking to deserialize the serialized object in Java, I want to know the structure of a binary serialized object, so i can parse it in a way that i want.

Emily Wong
  • 297
  • 3
  • 10
  • 2
    Wouldn't it be way easier to just use JSON / XML or your custom binary format that would be language agnostic? – Fildor Feb 18 '19 at 14:07
  • I'm not sure if it's worth the effort. You could always try XML serialization, granted it's slower, but it's much easier to perform. You could XML serialize the arrays and easily get them back, better so, your code can be reused quite easily – Everyone Feb 18 '19 at 14:08
  • 2
    Binary serialization works different in different programming languages. So you can not serialize an object in C# and deserialize it in Java. There is not in-built feature available in JAVA or C#. You need to write your own serialization mechanisms in both the languages. Else you can use XML or JSON serializaiton. – Chetan Feb 18 '19 at 14:08
  • On another thought, you could also manually serialize using your own algorithm and just reverse the process on the other end. I do not recommend this unless you really know your stuff around serialization and even then you may end up with a lot of issues. – Everyone Feb 18 '19 at 14:10
  • Possible duplicate of [Deserializing C# Binary in Java](https://stackoverflow.com/questions/18277869/deserializing-c-sharp-binary-in-java) – Pete Kirkham Feb 18 '19 at 14:11
  • Do you need to do this during runtime of both systems? Then maybe something like a messagequeue with clients for both languages would be an option? – Fildor Feb 18 '19 at 14:15
  • I dont want to deserialize it into an object in java, i only want to parse its variables in java. and I cant use xml serialization as it doesnt support multidimensional array – Emily Wong Feb 18 '19 at 14:23
  • the best is, i want to know what is the structure of a serialized byte, so i can parse it in the way that i want. – Emily Wong Feb 18 '19 at 14:24
  • Is that "multidimensional" or "jagged"? – Fildor Feb 18 '19 at 14:38
  • public static List> this is what i want to serialize. – Emily Wong Feb 18 '19 at 14:40
  • 2
    ?? That's _perfect_ for XML or JSON! – Fildor Feb 18 '19 at 14:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188614/discussion-between-emily-wong-and-fildor). – Emily Wong Feb 18 '19 at 15:40
  • JSON or Protocol buffer format would be a much better solution since both formats are platform-independent and thoroughly documented. That being said, see [.Net Where to find the official specification of the BinaryFormatter serialization format?](https://stackoverflow.com/q/2044111/3744182) but also [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182). – dbc Feb 18 '19 at 19:35
  • @dbc, In my project I cant use json, i dont want to add external libraries.. is there any way to code it within C# default imports? – Emily Wong Feb 19 '19 at 07:27
  • @EmilyWong XML doesn't support multidimensional arrays? Last I checked, XML supports literally EVERYTHING. I used it for databases, nosql databases, arrays of arrays, dictionaries, you name it. What you're asking for (deserialize in Java) is not a task you'd want to engage unless you do the binary serialization yourself. As I said before, that aint easy. – Everyone Feb 19 '19 at 21:50
  • @Everyone, Could you please write me a snippet of code to XML serialize the following variable? public static List> testList = new List>(); I cant figure it out. returning error always. – Emily Wong Feb 20 '19 at 01:32
  • @EmilyWong I just posted an answer to give you an understand of how it's done. Can be easily changed to any other type (int instead of string for example) – Everyone Feb 20 '19 at 10:14

1 Answers1

1

As suggested, XML can be great tool for this, here is a small demo example:

C# serialization:

// NEEDED IMPORTS
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

static void Main(string[] args)
{
  // build a list of lists from 0 to 99 divided on 
  // 10 inner list each with 10 elements
  List<List<string>> list = new List<List<string>>();
  for(int i=0; i<10; i++)
  {
    list.Add(new List<string>());
    for (int j = i * 10; j < i * 10 + 10; j++)
      list[i].Add(j.ToString());
  }

  // serialize to xml 
  XmlSerializer ser = new XmlSerializer(list.GetType());
  TextWriter writer = new StreamWriter("serialized.xml");
  ser.Serialize(writer, list);
}

Sample output:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ArrayOfString>
    <string>0</string>
    <string>1</string>
    <string>2</string>
    <string>3</string>
    <string>4</string>
    <string>5</string>
    <string>6</string>
    <string>7</string>
    <string>8</string>
    <string>9</string>
  </ArrayOfString>
  
  ...

Based on the serialized XML, Java deserialization:

// NEEDED IMPORTS
import java.io.*;
import java.util.*;
import org.jdom2.*;
import org.jdom2.input.*;

public static void main(String[] args) throws JDOMException, IOException {
    // build DOM from the XML file - generic for all XML files
    File fXmlFile = new File("serialized.xml"); // file we created in C#
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(fXmlFile);
    Element root = document.getRootElement();
    
    // build List<List<String>> using expected format
    if(!root.getName().equals("ArrayOfArrayOfString")){
        System.out.println("invalid root element");
        return; 
    }
    
    List<List<String>> list = new ArrayList<>();
    
    List<Element> children = root.getChildren();
    for(int i = 0; i<children.size(); i++){
        Element child = children.get(i);
        if(child.getName().equals("ArrayOfString")){
            List<String> innerList = new ArrayList<>();
            list.add(innerList);
            List<Element> innerChildren = child.getChildren();
            for(int j=0; j < innerChildren.size(); j++){
                Element elem = innerChildren.get(j);
                if(elem.getName().equals("string"))
                    innerList.add(elem.getValue());
            }
        }
    }
    
    for(int i = 0; i<list.size(); i++){
        System.out.print(String.format("InnerList[%d]: ", i));
        List<String> innerList = list.get(i);
        for(int j=0; j<innerList.size(); j++)
            System.out.print(String.format("\"%s\" ",innerList.get(j)));
        System.out.println();
    }
}

Output:

InnerList[0]: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"

InnerList[1]: "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"

InnerList[2]: "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"

InnerList[3]: "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"

InnerList[4]: "40" "41" "42" "43" "44" "45" "46" "47" "48" "49"

InnerList[5]: "50" "51" "52" "53" "54" "55" "56" "57" "58" "59"

InnerList[6]: "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"

InnerList[7]: "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"

InnerList[8]: "80" "81" "82" "83" "84" "85" "86" "87" "88" "89"

InnerList[9]: "90" "91" "92" "93" "94" "95" "96" "97" "98" "99"

This is a very simple demonstration of what can be done using XML. I am not doing error handling in this code.


Edit: The Java DOM builder is not directly from the JDK. You need to download it from their website: http://www.jdom.org/downloads/ and import the jre file to your Java project. I have also included the import statements needed for this operation.

Community
  • 1
  • 1
Everyone
  • 1,751
  • 13
  • 36