1

I have a task of deserializing serialized php arrays using only C#. I searched everywhere but I did not find a specific method or solution. How to unserialize PHP Serialized array/variable/class and return suitable object in C# this solution suggested a Sharp Serialization Library but it did not serve my purpose.

Following is the sample input data which I would like to deserialize

a:4:{s:83:"|wsInternalSchedule|acct_ar_/AccountsReceivableAgingDetail-Total/AccountsReceivable";s:69:"Accounts
 Receivable Aging Detail - Total|acct_ar_|Accounts
 Receivable";s:69:"Accounts Receivable Aging Detail -
 Total|acct_ar_|Accounts
 Receivable";a:5:{s:8:"LayoutID";s:0:"";s:7:"IsChart";s:5:"false";s:11:"Description";s:0:"";s:10:"Preference";s:13:"<Preference/>";s:22:"LayoutParamPreferences";s:729:"<LayoutParamPreferences><LayoutParamPreferences_Item><fieldName>type</fieldName><fieldValue>total</fieldValue></LayoutParamPreferences_Item><LayoutParamPreferences_Item><fieldName>company</fieldName><fieldValue>1</fieldValue></LayoutParamPreferences_Item><LayoutParamPreferences_Item><fieldName>account</fieldName><fieldValue>0220</fieldValue></LayoutParamPreferences_Item><LayoutParamPreferences_Item><fieldName>port</fieldName><fieldValue></fieldValue></LayoutParamPreferences_Item><LayoutParamPreferences_Item><fieldName>sel</fieldName><fieldValue></fieldValue></LayoutParamPreferences_Item><LayoutParamPreferences_Item><fieldName>dsn</fieldName><fieldValue></fieldValue></LayoutParamPreferences_Item></LayoutParamPreferences>";}s:72:"Accounts
 Receivable Aging Detail -
 Total|acct_ar_|wsInternalActiveLayout";s:8:"Original";s:64:"Accounts
 Receivable Aging Detail -
 Total|acct_ar_|wsInternalView";s:802:"<View><LayoutBaseDirectory>/we/templates/</LayoutBaseDirectory><LayoutLanguage>en_US</LayoutLanguage><ApplicationDirectory>acct/ar/</ApplicationDirectory><GridLayoutName>acct.ar.detailViewFormat.xml</GridLayoutName><PrintLayoutName/><CommandToExecute>acct.ARdetail</CommandToExecute><AutoPage>True</AutoPage><ExpandToBand>0</ExpandToBand><Param><ParamName>type</ParamName><ParamValue>total</ParamValue></Param><Param><ParamName>company</ParamName><ParamValue>1</ParamValue></Param><Param><ParamName>account</ParamName><ParamValue>0220</ParamValue></Param><Param><ParamName>port</ParamName><ParamValue/></Param><Param><ParamName>sel</ParamName><ParamValue/></Param><Param><ParamName>dsn</ParamName><ParamValue/></Param><User>tom</User><Group>BMW</Group><Account></Account><ApplCode>ALL</ApplCode></View>";}

My approach: I have created a data structure which will hold the key, value and datatype. However, I am unable to extract the actual key and values. I figured to extract them by splitting the entire text on ';' and ':'. However, this gets stuck during the nesting.

Can anybody suggest a specific method or is my approach wrong?

Edit:

Here is the code I have written for the splitting of the entries. phpMsg is the entire input text which has been read from a file.

       char entry_splitter = ';';           
       char section_splitter = ':';

       // Split into entries    
       string[] entries;
       try
       {
           entries = phpMsg.Trim().Split(entry_splitter);
       }
       catch (NullReferenceException nre)
       {
           entries= null;
       }

           // Split each entry into its sub sections
           if (entries != null)
           {

               string[] sections = new string[50];
               string nestings = null;
               for (int i = 0; i < (entries.Length) - 1; i++)
               {

                   if (entries[i].Contains("{"))
                   {
                       nestings = entries[i].Substring(5);

                       sections = nestings.Split(section_splitter);
                   }
                   else
                   {


                       sections = entries[i].Split(section_splitter);
                   }
Community
  • 1
  • 1
  • 1
    Could you not `unserialize()` in PHP and then `json_encode()` which I assume can be read by c#? – AbraCadaver Jul 14 '15 at 21:03
  • An excellent attempt at a first question!, while I have no idea in general, the bottom looks like XML, which there are existing parsers for in C#. Could you be a bit clearer as to what *exactly* the problem is? Show the code you are using to parse? – BradleyDotNET Jul 14 '15 at 21:03
  • @AbraCadaver yeah that is the easier and logical way. However, it needs to be done in C# purely, for reasons best known to my organization. –  Jul 14 '15 at 21:08
  • @BradleyDotNET Yeah there is XML at the bottom. However, it is not neccesarily there. All they want is to deserialize the PHP data. I use the following code for splitting. –  Jul 14 '15 at 21:09
  • string[] sections = new string[50]; string nestings = null; for (int i = 0; i < (entries.Length) - 1; i++) { if (entries[i].Contains("{")) { nestings = entries[i].Substring(5); sections = nestings.Split(section_splitter); } else { sections = entries[i].Split(section_splitter); } –  Jul 14 '15 at 21:11
  • You can use `json_encode()` to first serialize the PHP array into JSON then you can use [JavaScriptSerializer](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx) or [Json.Net](http://www.newtonsoft.com/json) to convert it back to C# array. – hyde Jul 14 '15 at 21:11
  • @hyde Thanks, I will try that! –  Jul 14 '15 at 21:20
  • Please put code into the question (and format it as such) its very hard to read in comments. Use the "edit" link at the bottom of your post. – BradleyDotNET Jul 14 '15 at 21:20
  • fwiw the "Sharp Serialization Library" seemed to work fine on your example after removing \r\n from it. – steve16351 Jul 14 '15 at 22:29
  • @steve16351 can you explain a bit more on how you did it with that Library? –  Jul 14 '15 at 22:39
  • @Abhishek Chandorkar Given a copy/paste of your input that I read in from text file, `new Serializer().Deserialize(input.Replace("\r\n", String.Empty))`, resulted in a hash table with 4 entries, with one of the entries having a nested hash table with 5 entries. Looking at your input this seemed about right. – steve16351 Jul 15 '15 at 07:52
  • @steve16351 Thanks a lot! It did work! I really appreciate your help –  Jul 15 '15 at 15:52

0 Answers0