Question

I'm using an older version of Sharp arch on a project and I'm trying to serialize using JSON.NET. JavascriptSerializer works, but I prefer JSON.NET for the preferences.

Here's the problem. For some reason, when I try to serialize a simple Sharp object I get the following:

// my sharp object
[Serializable]
public class Contact : Entity
{
    public virtual string EmailAddress { get; set; }
}

...

// in sharp, this is what happens to Entity
[Serializable]
public abstract class Entity : EntityWithTypedId<int> {
    protected Entity();
}

// and then into EntityWithTypedId
[Serializable]
public abstract class EntityWithTypedId<IdT> : ValidatableObject, IEntityWithTypedId<IdT> {
    protected EntityWithTypedId();

    [JsonProperty]
    [XmlIgnore]
    public virtual IdT Id { get; protected set; }

    public override bool Equals(object obj);
    public override int GetHashCode();
    protected override IEnumerable<PropertyInfo> GetTypeSpecificSignatureProperties();
    public virtual bool IsTransient();
}

When I run the following JSON convert, I only get back { "Id" : 0 } as a result.

Contact test = new Contact {
    EmailAddress = "test@test.com"
};
string result = JsonConvert.SerializeObject(test);

Any ideas on how to return the entire object contents?

Was it helpful?

Solution

The BaseObject class in S#arp Architecture had member serialisation set to OptIn, which has been removed in 2.0.

Your options are:

  • Update to Sharp Architecture 2.0
  • Use a different json serialiser, ServiceStack.Text is great.
  • Recompile Sharp Architecture 1.6 with the property removed
  • Add JsonProperty attribute to specific properties you want serialised
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top