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);
                   }
10
  • 1
    Could you not unserialize() in PHP and then json_encode() which I assume can be read by c#? Commented Jul 14, 2015 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? Commented Jul 14, 2015 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. Commented Jul 14, 2015 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. Commented Jul 14, 2015 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); } Commented Jul 14, 2015 at 21:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.