Unable to serialize/deserialize FeatureLayer to/from Json

4062
1
10-29-2011 12:38 PM
Labels (1)
KirkKuykendall
Occasional Contributor III
I'm trying to serialize some featurelayers to a local file, and then deserialize them in Visual Studio at design time to use as a design time datacontext.  However, I can't find a way to serialize or deserialize.


When I try to serialize, I get this exception:
System.Runtime.Serialization.InvalidDataContractException was unhandled
  Message=Type 'ESRI.ArcGIS.Client.FeatureLayer' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.


At the WriteObject call in this code:
private void button1_Click(object sender, RoutedEventArgs e)
{
    var fLayer = new FeatureLayer();
    fLayer.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/2";
    fLayer.Initialized += (s, ea) =>
        {                   
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(FeatureLayer));
            ser.WriteObject(ms, fLayer); // exception here
            ms.Position = 0;
            StreamReader sr = new StreamReader(ms);
            string json = sr.ReadToEnd();
            Debug.Print(json);
            fLayer.ToString();
        };
    fLayer.Initialize();
}


When I try to deserialize, I get this Exception :
System.ArgumentException was unhandled
  Message=layerDefinition
Parameter name: Property was not found during JSON deserialization.


When I  call FeatureLayer.FromJson:

private void button1_Click(object sender, RoutedEventArgs e)
{
    WebClient wc = new WebClient();
    wc.OpenReadCompleted += (s, ea) =>
        {                    
            using (StreamReader sr = new StreamReader(ea.Result))
            {
                string json = sr.ReadToEnd();
                Debug.Print(json);
                var fLayer = FeatureLayer.FromJson(json);
                Debug.Print(fLayer.LayerInfo.Fields.Count.ToString());
            }
        };
    wc.OpenReadAsync(new Uri(@"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/2?f=pjson"));
}
0 Kudos
1 Reply
MichaelBranscomb
Esri Frequent Contributor
Hi,


Feature layer serialization does not support WCF serialization (DataContract attributes) �?? the properties of Feature layer are not attributed to do this.

To solve this you may need to write your own serialization by reading and writing values during serlialization and controlling what it is that you want to save. For a feature layer it may make the most sense to save the url to the feature service and any other setting  (like ondemand mode etc). If you want to work with WCF serialization then you could create a wrapper class with attribution in place that can copy these properties and then be saved. Similarly in reverse when its instantiated you can manufacture a Feature Layer by setting back the properties.

Cheers

Mike
0 Kudos