Salve, è il mio secondo giorno in C# e stò tentando di imparare, quindi vi chiederei di non insultarmi 
Visto che mi stò esercitando ho pensato, come prima cosa, di provare a fare una classe che secondo me un domani potrà tornarmi utile per l'implementazione in .NET di AJSHP ( sistema alternativo ajax che sfrutta alla base l' oggetto javascript PHP_Serializer .. sempre scritto da me
) in modo da inviare e ricevere dati con tipo assegnato senza dover per forza passare per JSON.
Maggiori dettagli li trovate sul sito, ma non sono qui per fare pubblicità o spam, sono qui per sapere cosa ne pensate di questa mia prima classe in C# .
Amici mi hanno già detto che è incomprensibile ... quindi vi spiego subito perchè ho scelto questi nomi:
__ => lo uso in prefisso per metodi o variabili private
u => lo utilizzo per indicare prefisso metodo di unserializzazione
s => lo uso per indicare un metodo di serializzazione
i => intero
b => booleano
d => double
a => Array [Hashtable per voi]
O => Oggetto [SortedList per voi]
N => null
questi i tipi di variabili in serializzazione PHP e compatibili con JavaScript e la PHP_Serializer
di qui
__us => metodo di deserializzazione stringa ... e così via
__ss => metodo di serializzazione stringa .. e così via
ultimo,
__c , variabile che indica da quale numero di carattere cercare
__s , stringa , in unserializzazione, di riferimento, dove operare
Ecco la classe (alleluia)
Codice PHP:
class PHP_Serializer {
// Andrea Giamamrchi [2006/02/09]
private int __c;
private string __s;
public PHP_Serializer() {}
public string serialize(object what)
{
string __s;
if (what is int)
__s = __si((int) what);
else if (what is double)
__s = __sd((double) what);
else if (what is bool)
__s = __sb((bool) what);
else if (what is string)
__s = __ss((string) what);
else if (what is System.Collections.Hashtable)
__s = __sa((System.Collections.Hashtable) what);
else if (what is System.Collections.SortedList)
__s = __sO((System.Collections.SortedList)what);
else if (what == null)
__s = "N;";
else
__s = "";
return __s;
}
public object unserialize(string what)
{
__c = 0;
__s = what;
return __switchFunction(__s.Substring(__c, 1));
}
private string __ss(string s)
{
return "s:" + s.Length + ":\"" + s + "\";";
}
private string __sb(bool b)
{
return "b:" + (b == false ? "0" : "1") + ";";
}
private string __sN(object N)
{
return "N;";
}
private string __si(int i)
{
return "i:" + i + ";";
}
private string __sd(double d)
{
return "d:" + d.ToString().Replace(
System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator, ".") + ";";
}
private string __sa(System.Collections.Hashtable a)
{
return "a:" + a.Count + "{" + __sct(a) + "}";
}
private string __sO(System.Collections.SortedList O)
{
return "O:" + O["__name__"].ToString().Length + ":\"" + O["__name__"] + "\":" + (O.Count - 1) + ":{" + __sct(O) + "}";
}
private string __sct(System.Collections.Hashtable a)
{
string __s = "";
foreach (DictionaryEntry r in a)
__s += serialize(r.Key) + "" + serialize(r.Value);
return __s;
}
private string __sct(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 __us()
{
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 __ui()
{
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 __ud()
{
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 __ub()
{
bool tmp = (__s.Substring(__c+2,1)=="1"?true:false);
__c += 4;
return tmp;
}
private object __uN()
{
__c += 2;
return null;
}
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 __uct(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 __uct(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 __ua()
{
System.Collections.Hashtable tmp = __uct(new System.Collections.Hashtable());
++__c;
return tmp;
}
private System.Collections.SortedList __uO()
{
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 = __uct(new System.Collections.SortedList());
result["__name__"] = o;
++__c;
return result;
}
private object __switchFunction(string what)
{
object result;
switch (what)
{
case "O":
result = __uO();
break;
case "a":
result = __ua();
break;
case "s":
result = __us();
break;
case "i":
result = __ui();
break;
case "d":
result = __ud();
break;
case "b":
result = __ub();
break;
case "N":
result = __uN();
break;
default:
result = new Object();
break;
}
return result;
}
}
ed ecco un pò di codice per testarla:
Codice PHP:
void Page_Load() {
PHP_Serializer php = new PHP_Serializer();
int i = (int)php.unserialize("i:123;");
Response.Write("Integer => " + i + "
");
Response.Write("Serialized => " + php.serialize(i) + "<hr />");
double d = (double)php.unserialize("d:0.123;");
Response.Write("Double => " + d + "
");
Response.Write("Serialized => " + php.serialize(d) + "<hr />");
bool b = (bool)php.unserialize("b:1;");
Response.Write("Boolean => " + b + "
");
Response.Write("Serialized => " + php.serialize(b) + "<hr />");
string s = (string)php.unserialize("s:4:\"test\";");
Response.Write("String => " + s + "
");
Response.Write("Serialized => " + php.serialize(s) + "<hr />");
Hashtable a = (Hashtable)php.unserialize("a:2:{i:0;s:5:\"test1\";s:2:\"k1\";s:5:\"test2\";}");
Response.Write("Array => " + a[0] + ", " + a["k1"] + "
");
Response.Write("Serialized => " + php.serialize(a) + "<hr />");
SortedList o = (SortedList)php.unserialize("O:7:\"TestPub\":2:{s:10:\"public_var\";s:5:\"test3\";}");
Response.Write("Object => " + o["public_var"] + " => " + o["__name__"] + "
");
Response.Write("Serialized => " + php.serialize(o) + "<hr />");
}
mi dite cosa ne pensate ??? grazie mille