简体   繁体   English

C#手动反序列化

[英]C# Manually Deserialization

I want to know if that's possible to manually parse a serialized binary which has been serialized in c#. 我想知道是否有可能手动解析已在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, 我的最终目标是解析已在c#中序列化的多维数组的序列化二进制文件,并在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. 注意:我不是要在Java中反序列化对象,我想知道二进制序列化对象的结构,因此我可以按照自己想要的方式对其进行解析。

As suggested, XML can be great tool for this, here is a small demo example: 如建议的那样,XML可能是一个很好的工具,这是一个小的演示示例:

C# serialization: C#序列化:

// 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: 基于序列化的XML,Java反序列化:

// 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 [0]:“ 0”“ 1”“ 2”“ 3”“ 4”“ 5”“ 6”“ 7”“ 8”“ 9”

InnerList[1]: "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" 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 [2]:“ 20”“ 21”“ 22”“ 23”“ 24”“ 25”“ 26”“ 27”“ 28”“ 29”

InnerList[3]: "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" 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 [4]:“ 40”“ 41”“ 42”“ 43”“ 44”“ 45”“ 46”“ 47”“ 48”“ 49”

InnerList[5]: "50" "51" "52" "53" "54" "55" "56" "57" "58" "59" 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 [6]:“ 60”“ 61”“ 62”“ 63”“ 64”“ 65”“ 66”“ 67”“ 68”“ 69”

InnerList[7]: "70" "71" "72" "73" "74" "75" "76" "77" "78" "79" 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 [8]:“ 80”“ 81”“ 82”“ 83”“ 84”“ 85”“ 86”“ 87”“ 88”“ 89”

InnerList[9]: "90" "91" "92" "93" "94" "95" "96" "97" "98" "99" 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. 这是使用XML可以完成的非常简单的演示。 I am not doing error handling in this code. 我没有在此代码中进行错误处理。


Edit: The Java DOM builder is not directly from the JDK. 编辑:Java DOM构建器不是直接来自JDK。 You need to download it from their website: http://www.jdom.org/downloads/ and import the jre file to your Java project. 您需要从他们的网站下载它: http : //www.jdom.org/downloads/并将jre文件导入到您的Java项目中。 I have also included the import statements needed for this operation. 我还包括了此操作所需的import语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM