1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| [DataContract]
public class tblOperation {
[DataMember]
public tblSource source { get { return _source; } set { _source = value; } }
...
public tblOperation Upd() {
var dcs = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(tblOperation), null, int.MaxValue, false, new MyOpeSurrogate(true), false); // En Json
System.IO.MemoryStream ms = new System.IO.MemoryStream();
dcs.WriteObject(ms, this); // Serialize l'objet
string postData = Encoding.UTF8.GetString(ms.ToArray());
...
}
}
[DataContract]
public sealed class tblSource {
private string _name;
[DataMember]
public int id { get; set; }
[DataMember(Name = "name")]
public string Name { get { return _name; } set { _name = value; } } // Nom
[DataMember]
public tblCreate create { get { return (id<1) ? new tblCreate(Name) : null; } private set { } }
...
}
[DataContract]
public class tblCreate {
[DataMember]
public string name { get; set; }
public tblCreate() { }
public tblCreate(string pName) { name = pName; }
}
public class MyOpeSurrogate : IDataContractSurrogate {
private bool toSerialise;
/// <summary>Constructeur</summary>
/// <param name="pToSerialise">Indique le sens qui sera utlisé (les API ne donnent pas le même format en GET qu'elles attendent en POST).</param>
public MyOpeSurrogate(bool pToSerialise) { toSerialise = pToSerialise; }
public Type GetDataContractType(Type type) { // var dcs = new DataContractJsonSerializer(typeof(tblContact), null, int.MaxValue, false, new MyGroupSurrogate(), false); // En Json
return type;
}
public object GetDeserializedObject(object obj, Type targetType) { // object newFamily = dcs.ReadObject(ms);
return obj;
}
public object GetObjectToSerialize(object obj, Type targetType) { // dcs.WriteObject(ms, this);
return obj;
}
} |
Partager