codice:
struct PHP_Serializer {
// Andrea Giamamarchi [2006/02/10]
private int __c; // string position, where indexOf and substrings should start
private string __s; // string to unserialize
/**
* public method serialize
* accepts a generic object and returns a PHP serialize
* rappresentation.
*
* this.serialize(what:Object):String
*
* @param Object generic object to serialize compatible with php
* [int,bool,double,null,string,Hashtable(array),SortedList(Object)]
* @return String php compatible rappresentation string of serialized value
*/
public string serialize(object what)
{
string __s;
if (what is int)
__s = __serializeInt32((int) what);
else if (what is double)
__s = __serializeDouble((double) what);
else if (what is bool)
__s = __serializeBoolean((bool) what);
else if (what is string)
__s = __serializeString((string) what);
else if (what is System.Collections.Hashtable)
__s = __serializeHashtable((System.Collections.Hashtable) what);
else if (what is System.Collections.SortedList)
__s = __serializeSortedList((System.Collections.SortedList)what);
else if (what == null)
__s = "N;";
else
__s = "";
return __s;
}
/**
* public method serialize
* accepts a php serialized string and returns a generic object that should be a
* [int,bool,double,string,Hashtable(from array),SortedList(from Object *),null]
* * Object will contains a key called __name__ that is the name of original serialized Object
*
* this.unserialize(what:String):Object
*
* @param String php serialized rappresentation of a variable
* @return Object C# compatible variable [int,double,bool,Hashtable,SortedList,null,string]
*/
public object unserialize(string what)
{
__c = 0;
__s = what;
return __switchFunction(__s.Substring(__c, 1));
}
/** list all private methods */
private string __serializeString(string s)
{
return "s:" + s.Length + ":\"" + s + "\";";
}
private string __serializeBoolean(bool b)
{
return "b:" + (b == false ? "0" : "1") + ";";
}
private string __serializeInt32(int i)
{
return "i:" + i + ";";
}
private string __serializeDouble(double d)
{
return "d:" + d.ToString().Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator, ".") + ";";
}
private string __serializeHashtable(System.Collections.Hashtable a)
{
return "a:" + a.Count + "{" + __serializeCollections(a) + "}";
}
private string __serializeSortedList(System.Collections.SortedList O)
{
return "O:" + O["__name__"].ToString().Length + ":\"" + O["__name__"] + "\":" + (O.Count - 1) + ":{" + __serializeCollections(O) + "}";
}
private string __serializeCollections(System.Collections.Hashtable a)
{
string __s = "";
foreach (DictionaryEntry r in a)
__s += serialize(r.Key) + "" + serialize(r.Value);
return __s;
}
private string __serializeCollections(System.Collections.SortedList O)
{
string k, v, __s = "";
foreach (DictionaryEntry r in O)
{
k = r.Key.ToString();
v = serialize(r.Value);
if (k != "__name__") {
k = serialize(k);
if(k != "" && v != "")
__s += k + "" + v;
}
}
return __s;
}
private string __unserializeString()
{
int sli, sli2;
string sls;
__c += 2;
sls = __s.Substring(__c, (__s.IndexOf(':', __c) - __c));
sli = System.Convert.ToInt32(sls);
sli2 = __c + sls.Length + 2;
__c = sli2 + sli + 2;
return __s.Substring(sli2, sli);
}
private int __unserializeInt32()
{
int sli = __s.IndexOf(';', (__c+1))-2;
int tmp = System.Convert.ToInt32(__s.Substring((__c+2),(sli-__c)));
__c = sli + 3;
return tmp;
}
private double __unserializeDouble()
{
int sli = __s.IndexOf(';', (__c + 1)) - 2;
double tmp = System.Convert.ToDouble(__s.Substring((__c + 2),(sli - __c)).Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator));
__c = sli + 3;
return tmp;
}
private bool __unserializeBoolean()
{
bool tmp = (__s.Substring(__c+2,1)=="1"?true:false);
__c += 4;
return tmp;
}
private int __uGetKforWhile() {
int a, k;
__c += 2;
a = __s.IndexOf(':', __c);
k = System.Convert.ToInt32(__s.Substring(__c, (a - __c))) + 1;
__c = a + 2;
return k;
}
private System.Collections.SortedList __unserializeCollections(System.Collections.SortedList tmp)
{
int k = __uGetKforWhile();
while (--k > 0)
tmp[__switchFunction(__s.Substring(__c, 1)).ToString()] = __switchFunction(__s.Substring(__c, 1));
return tmp;
}
private System.Collections.Hashtable __unserializeCollections(System.Collections.Hashtable tmp)
{
int k = __uGetKforWhile();
while(--k > 0)
tmp[__switchFunction(__s.Substring(__c, 1))] = __switchFunction(__s.Substring(__c, 1));
return tmp;
}
private System.Collections.Hashtable __unserializeHashtable()
{
System.Collections.Hashtable tmp = __unserializeCollections(new System.Collections.Hashtable());
++__c;
return tmp;
}
private System.Collections.SortedList __unserializeSortedList()
{
string tmp, a, o;
System.Collections.SortedList result;
tmp = "s" + __s.Substring(++__c, (__s.IndexOf(':', (__c + 3)) - __c)) + ";";
a = tmp.Substring(2,(tmp.IndexOf(':',2)-2));
o = tmp.Substring((a.Length+4),System.Convert.ToInt32(a));
__c += (tmp.Length - 3);
result = __unserializeCollections(new System.Collections.SortedList());
result["__name__"] = o;
++__c;
return result;
}
private object __switchFunction(string what)
{
object result;
switch (what)
{
case "O":
result = __unserializeSortedList();
break;
case "a":
result = __unserializeHashtable();
break;
case "s":
result = __unserializeString();
break;
case "i":
result = __unserializeInt32();
break;
case "d":
result = __unserializeDouble();
break;
case "b":
result = __unserializeBoolean();
break;
case "N":
__c += 2;
result = null;
break;
default:
result = new Object();
break;
}
return result;
}
}
cmq più che una mano, visto che funziona, volevo un parere