Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

11 avril 2014 5 11 /04 /avril /2014 17:11

http://json.codeplex.com/

 

http://james.newtonking.com/json/help/index.html

 

 

Avec JSon.NET

  public void SerializeJSonClient(Client client)

        {

            JsonSerializer serializer = new JsonSerializer();

            serializer.NullValueHandling = NullValueHandling.Ignore;

 

            using (StreamWriter streamWriter = new StreamWriter("json.txt"))

            {

                using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter))

                {

                    serializer.Serialize(jsonWriter, client);

                }

            }

        }

        public Client DeserializeJSonClient(string path)

        {

            using (StreamReader streamReader = new StreamReader(path))

            {

                string value =  streamReader.ReadToEnd();

                return JsonConvert.DeserializeObject<Client>(value);

            }

        }

Classe générique avec  DataContractJsonSerializer

public class JsonGenericSerializer<TEntity> where TEntity : class,new()

    {

        public static void WriteFile(TEntity entity,string path)

        {

            using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))

            {

                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TEntity));

                serializer.WriteObject(stream, entity);

            }

        }

        public static TEntity ReadFile(string path)

        {

            using (FileStream stream = new FileStream(path, FileMode.Open))

            {

                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TEntity));

                StreamReader streamReader = new StreamReader(stream);

                return (TEntity)serializer.ReadObject(stream);

            }

        }

    }

La classe de l’exemple (les paramètres permettent de redéfinir notamment les noms)

  [DataContract(Name="Client")]

    public class Client 

    {

        [DataMember(Name="Id")]

        public int ClientID { get; set; }

        [DataMember(Name = "FirstName")]

        public string ClientFirstName { get; set; }

        [DataMember(Name = "LastName")]

        public string ClientLastName { get; set; }

        [DataMember(Name = "Email")]

        public string Email { get; set; }

    }

Utilisation

Client client=new Client(){ClientID=1,ClientFirstName="Marie",ClientLastName="Bellin",Email="bellinmarie@live.com"};

//   SerializeJSonClient(client);

JsonGenericSerializer<Client>.WriteFile(client,"demo.txt");

 

 

// Client result = DeserializeJSonClient("json.txt");

Client result = JsonGenericSerializer<Client>.ReadFile("demo.txt");

 

 

Le fichier généré

{"Email":"bellinmarie@live.com","FirstName":"Marie","Id":1,"LastName":"Bellin"}

Partager cet article

Repost 0
Published by Romagny13 - dans C
commenter cet article

commentaires