Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've got some business objects below that I can serialize and deserialize successfully with Json.NET. I don't see a simple way to do this with Esri's SOESupport however. I'm happy with Json.NET, but I'd like to leverage Esri's IGeometry conversion routines. Is there some way to get SOESupport to work for this?

Update:

Since it doesn't look like Esri can do this, I'm trying to use Json.NET. See my related issue on stackoverflow:

How can I deserialize with TypeNameHandling.Objects in Json.NET Silverlight?

public class Animal
{
    public string Name { get; set; }
}
public class Dog: Animal 
{
    public bool LikesBones { get; set; }
}
public class Cat: Animal 
{
    public bool LikesMice { get; set; }
}
public class Zoo
{
    public int ID { get; set; }
    private List<Animal> m_Animals = new List<Animal>();
    public List<Animal> Animals { get { return m_Animals; } set { m_Animals = value; } }
}

Is there some way to get Esri's JsonObject to support this sort of test?

public static void Test1()
{
    // use Json.NET
    Zoo z1 = new Zoo() { ID = 1 };
    z1.Animals.Add(new Dog() { Name = "Fido", LikesBones = true });
    z1.Animals.Add(new Cat() { Name = "Felix", LikesMice = false });
    string s1 = Util.Serialize(z1);                

    Debug.Print(s1);
    var z2 = Util.Deserialize<Zoo>(s1);
    string s2 = Util.Serialize(z2);
    Debug.Print(s2);
    if (s1 != s2)
        Debug.Print("failed");
    else
        Debug.Print("ok");
}

I can apply settings in Json.NET that force the typename to be serialized, allowing polymorphism for the list of Animals. How can I implement these same functions with SOESupport?

public static string Serialize(object o)
{
    JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.TypeNameHandling = TypeNameHandling.Objects;
    return JsonConvert.SerializeObject(o, Formatting.Indented, settings);
}
public static T Deserialize<T>(string s)
{
    JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.TypeNameHandling = TypeNameHandling.Objects;
    return JsonConvert.DeserializeObject<T>(s,settings);
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Short version is "I don't think so". You might be able to extend ESRI's conversion classes, but frankly, it'd be a whole lot easier to make custom Json type converters with Json.NET, and attribute your geometry fields as needed.

public class Animal
{
    public string Name { get; set; }

    [JsonConverter(typeof(ESRIGeomConverter))]
    public IGeometry location { get; set; }
}

public class ESRIGeomConverter : JsonConverter
{
    public override CanConvert( Type objectType )
    {
        return true;
    }

    public override Object ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer )
    {
        // use ESRI conversion to get an IGeometry from the reader:
        // Conversion.ToGeometry(jsonPoint, esriGeometryType.esriGeometryPoint)
    }

    public override void WriteJson(JsonWriter writer, Object value, JsonSerializer serializer )
    {
        // use ESRI conversion to get Json for the IGeometry in "value"
        // SOESupport.Conversion.ToJsonObject((IGeometry)value)
    }
}
share|improve this answer
    
Thanks Herb. Since I'm using the same class library within both the silverlight app and the SOE, I can't have any dependencies on arcobjects. Instead, I'll put the conversion routines in a separate assembly. I hope people developing javascript clients will be able to consume my service using methods described here. If not, it seems like I might as well use SOAP. –  Kirk Kuykendall May 2 '11 at 14:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.