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
| /// <summary>Classe contrat de sérialisation http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/14/wcf-extensibility-serialization-surrogates.aspx </summary>
public class MyGroupSurrogate : IDataContractSurrogate {
public Type GetDataContractType(Type type) { // var dcs = new DataContractJsonSerializer(typeof(tblGroup), null, int.MaxValue, false, new MyGroupSurrogate(), false); // En Json
if (type == typeof(ObservableCollection<tblScope>)) {
return typeof(List<string>);
} else
return type;
}
public object GetDeserializedObject(object obj, Type targetType) { // object newFamily = dcs.ReadObject(ms);
//if (obj is List<string>) { }
return obj;
}
public object GetObjectToSerialize(object obj, Type targetType) { // dcs.WriteObject(ms, this);
if (obj is ObservableCollection<tblScope>) {
List<string> serializablePerson = (from c in (ObservableCollection<tblScope>)obj select c.value).ToList();
return serializablePerson;
} else
return obj;
}
public void GetKnownCustomDataTypes(Collection<Type> customDataTypes) { throw new NotSupportedException("unused"); }
public object GetCustomDataToExport(Type clrType, Type dataContractType) { throw new NotSupportedException("unused"); }
public object GetCustomDataToExport(System.Reflection.MemberInfo memberInfo, Type dataContractType) { throw new NotSupportedException("unused"); }
public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData) { throw new NotSupportedException("unused"); }
public System.CodeDom.CodeTypeDeclaration ProcessImportedType(System.CodeDom.CodeTypeDeclaration typeDeclaration, System.CodeDom.CodeCompileUnit compileUnit) { throw new NotSupportedException("unused"); }
}
...
//var dcs = new DataContractSerializer(typeof(tblGroup), null, int.MaxValue, false, false, new MyGroupSurrogate()); // En Xml
var dcs = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(tblGroup), null, int.MaxValue, false, new MyGroupSurrogate(), false); // En Json
System.IO.MemoryStream ms = new System.IO.MemoryStream();
dcs.WriteObject(ms, this);
string postData = Encoding.UTF8.GetString(ms.ToArray()); |
Partager