JSON Serialization C#

Today I write DAL layer that comunicate with the service with JSON.
First of all I used JavaScriptSerializer object but I found some problem with DateTime type.
When it deserialize the DateTime it consider the UTC delta and day light saving.

I live in italy TimeZone +1h and now 2014-09-11 I’m in daylight saving time (+1h)
and now I’ll show you something magic:

new JavaScriptSerializer().Serialize(new DateTime(2014,09,08))    ===>   "\"\\/Date(1410127200000)\\/\""    (string)

new JavaScriptSerializer().Deserialize<DateTime>(new JavaScriptSerializer().Serialize(new DateTime(2014,09,08)))   ===>   2014-09-07 22:00:00 (DateTime)

WTF???? I lost 2 hours!!!!
I fixed this problem using both DataContractJsonSerializer, JsonSerializer (ServiceStack) and JsonConvert (json.net)

So I decide to implement in my DAL an interface that permit me a fast swap wich serializer/converter use.

Here my code

// interface
public interface SJSONConverter
{
    string ToJson<T>(T instance);

    T FromJson<T>(string json);
}

// use JavaScriptSerializer
public class SJSONJavaScriptSerializer : SJSONConverter
{
    public string ToJson<T>(T instance)
    {
        var serializer = new JavaScriptSerializer();
        return serializer.Serialize(instance);
    }

    public T FromJson<T>(string json)
    {
        var serializer = new JavaScriptSerializer();
        return serializer.Deserialize<T>(json);
    }
}

// use DataContractJsonSerializer
public class SJSONDataContractJsonSerializer : SJSONConverter
{
    public string ToJson<T>(T instance)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var tempStream = new MemoryStream())
        {
            serializer.WriteObject(tempStream, instance);
            return Encoding.Default.GetString(tempStream.ToArray());
        }
    }

    public T FromJson<T>(string json)
    {
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            return (T)serializer.ReadObject(tempStream);
        }
    }
}

// use JsonSerializer
public class SJSONJsonSerializerServiceStack : SJSONConverter
{
    public string ToJson<T>(T instance)
    {
        var result = JsonSerializer.SerializeToString<T>(instance);
        return result;
    }

    public T FromJson<T>(string json)
    {
        var result = JsonSerializer.DeserializeFromString<T>(json);
        return result;
    }
}

// use JsonConvert
public class SJSONJsonConvertJsonNet : SJSONConverter
{
    public string ToJson<T>(T instance)
    {
        var result = JsonConvert.SerializeObject(instance);
        return result;
    }

    public T FromJson<T>(string json)
    {
        var result = JsonConvert.DeserializeObject<T>(json);
        return result;
    }
}

choose wich serializer you prefer:

SJSONConverter jsonConverter = new SJSONDataContractJsonSerializer();
SJSONConverter jsonConverter = new SJSONJsonSerializerServiceStack();
SJSONConverter jsonConverter = new SJSONJsonConvertJsonNet();
// not suggested 
SJSONConverter jsonConverter = new SJSONJavaScriptSerializer();

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in Asp.Net, c# e contrassegnata con , , , , , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...