kirkktx

Unable to serialize/deserialize FeatureLayer to/from Json

Discussion created by kirkktx on Oct 29, 2011
Latest reply on Nov 7, 2011 by MBranscomb-esristaff
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"));
}

Outcomes