Outils pour utilisateurs

Outils du site


lang:csharp:serialisation

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
lang:csharp:serialisation [2019/03/04 12:23] – Création avec "Sérialisation en XML" et "JSON" rootlang:csharp:serialisation [2020/04/27 08:39] (Version actuelle) – mhtml -> html root
Ligne 1: Ligne 1:
 +Il faut ajouter la référence ''System.Runtime.Serialization''.
 +
 ====Préparation des classes==== ====Préparation des classes====
 <code csharp> <code csharp>
Ligne 43: Ligne 45:
 </code> </code>
  
-<note important>''IsReference'' ne marche qu'avec la sérialisation au format XML, pas JSON.</note>+<WRAP center round important 60%> 
 +''IsReference'' ne marche qu'avec la sérialisation au format XML, pas JSON. 
 +</WRAP>
  
   * Héritage   * Héritage
Ligne 73: Ligne 77:
   by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.   by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
  
-[[https://stackoverflow.com/questions/30210393/exception-during-the-serialization-type-with-data-contract-name-is-not-expecte/30214583|Exception during the serialization - Type with data contract name is not expected]] {{ :lang:csharp:serialisation:c_-_exception_during_the_serialization_-_type_with_data_contract_name_is_not_expected_-_stack_overflow.mhtml |Archive du 04/03/2019}}+[[https://stackoverflow.com/questions/30210393/exception-during-the-serialization-type-with-data-contract-name-is-not-expecte/30214583|Exception during the serialization - Type with data contract name is not expected]] {{ :lang:csharp:serialisation:c_-_exception_during_the_serialization_-_type_with_data_contract_name_is_not_expected_-_stack_overflow_2020-04-27_8_18_17_am_.html |Archive du 13/05/2015 le 27/04/2020}}
  
-[[https://blogs.msdn.microsoft.com/youssefm/2009/04/22/understanding-known-types/|Understanding Known Types]] {{ :lang:csharp:serialisation:understanding_known_types_youssef_m_s_blog.mhtml |Archive du 04/03/2019}}+[[https://docs.microsoft.com/en-us/archive/blogs/youssefm/understanding-known-types/|Understanding Known Types]] {{ :lang:csharp:serialisation:understanding_known_types_microsoft_docs_2020-04-27_8_18_33_am_.html |Archive du 22/04/2009 le 27/04/2020}}
  
 ====Sérialisation en XML==== ====Sérialisation en XML====
Ligne 88: Ligne 92:
     using (MemoryStream ms = new MemoryStream())     using (MemoryStream ms = new MemoryStream())
     {     {
-      _batch = (BatchPoco)ser.ReadObject(reader, true);+      try 
 +      { 
 +        _batch = (BatchPoco)ser.ReadObject(reader, true); 
 +      } 
 +      catch (SerializationException) 
 +      { 
 +        // Fichier non valide. 
 +      }
     }     }
   }   }
Ligne 109: Ligne 120:
 </code> </code>
  
-[[https://docs.microsoft.com/fr-fr/dotnet/framework/wcf/samples/datacontractserializer-sample|DataContractSerializer, exemple]] {{ :lang:csharp:serialisation:datacontractserializer_exemple_microsoft_docs.mhtml |Archive du 04/03/2019}}+[[https://docs.microsoft.com/fr-fr/dotnet/framework/wcf/samples/datacontractserializer-sample|DataContractSerializer, exemple]] {{ :lang:csharp:serialisation:datacontractserializer_exemple_-_wcf_microsoft_docs_2020-04-27_8_18_42_am_.html |Archive du 03/30/2017 le 27/04/2020}}
  
 ====Sérialisation en JSON==== ====Sérialisation en JSON====
 C'est la même chose avec ''DataContractJsonSerializer''. C'est la même chose avec ''DataContractJsonSerializer''.
  
-[[https://docs.microsoft.com/fr-fr/dotnet/framework/wcf/feature-details/how-to-serialize-and-deserialize-json-data|Procédure : Sérialiser et désérialiser des données JSON]] {{ :lang:csharp:serialisation:procedure_serialiser_et_deserialiser_des_donnees_json_microsoft_docs.mhtml |Archive du 04/03/2019}}+[[https://docs.microsoft.com/fr-fr/dotnet/framework/wcf/feature-details/how-to-serialize-and-deserialize-json-data|How to use DataContractJsonSerializer]] {{ :lang:csharp:serialisation:comment_utiliser_datacontractjsonserializer_-_wcf_microsoft_docs_2020-04-27_8_19_16_am_.html |Archive du 25/03/2019 le 27/04/2020}} 
 + 
 +<WRAP center round important 60%> 
 +TODO : Cette classe est dépréciée au profit du namespace ''System.Text.Json''
 +</WRAP> 
 + 
 + 
 +====Initialisation dans le constructeur==== 
 +Le constructeur, et les propriétés initialisées dans leurs déclarations, ne sont pas appelés lors de la désérialisation. Il est nécessaire de le faire manuellement. 
 + 
 +<code csharp> 
 +private object Mutex { get; set; }; 
 + 
 +private void InitNoDataMember() 
 +
 +  Mutex = new object(); 
 +
 + 
 +[OnDeserializing] 
 +private void SetValuesOnDeserializing(StreamingContext context) 
 +
 +  InitNoDataMember(); 
 +
 + 
 +public Constructeur() 
 +
 +  InitNoDataMember(); 
 +
 +</code> 
 + 
 +Il existe : 
 +  * ''OnSerializing'' : fonction exécutée lors de la sérialisation de l'instance. 
 +  * ''OnSerialized'' : fonction exécutée après la sérialisation de l'instance. 
 +  * ''OnDeserializing'' : fonction exécutée lors de la désérialisation de l'instance. 
 +  * ''OnDeserialized'' : fonction exécutée après la désérialisation de l'instance.
  
 +En cas d'héritage, ''OnSerializing'' est appelée pour le parent, puis pour l'enfant et enfin les ''OnSerialized'' sont appelées. Puis la désérialisation passe à l'objet suivant.
lang/csharp/serialisation.1551698621.txt.gz · Dernière modification : 2019/03/04 12:23 de root