Rioux160
Rioux160

Reputation: 5

JSON.NET JsonConvert serialization of entity return "[ ]"

i am serializing my entity object with JsonConvert.SerializeObject.

Something like that :

var test = JsonConvert.SerializeObject(MyEntityObject)

The result of my test is :

[ { "$id": "1", "Someproperty1": 1, "Someproperty2": 2, "Someproperty3": 3, "entityobject1": null, "entityobject2": null,
"entityobject3": [], "EntityKey": null } ]

The problem is with entityobject3 returning me 2 empty square brackets instead of null. It cause me problem farther in my code when im trying to deserialize it gives me a cannot implicitly convert type generic.list to entitycollection error.

Is there a way to tell JsonConver.SerializeObject to ignore those entity that cause me problem in the JsonSerializerSettings since i dont need them anyway.

Upvotes: 0

Views: 209

Answers (2)

Bonomi
Bonomi

Reputation: 2743

You can define a custom serialization/deserialization

public abstract class JsonCreationConverter<T> : JsonConverter
{
    /// <summary>
    /// Create an instance of objectType, based properties in the JSON object
    /// </summary>
    /// <param name="objectType">type of object expected</param>
    /// <param name="jObject">
    /// contents of JSON object that will be deserialized
    /// </param>
    /// <returns></returns>
    protected abstract T Create(Type objectType, JObject jObject);

    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, 
                                    Type objectType, 
                                     object existingValue, 
                                     JsonSerializer serializer)
    {
        // Load JObject from stream
        JObject jObject = JObject.Load(reader);

        // Create target object based on JObject
        T target = Create(objectType, jObject);

        // Populate the object properties
        serializer.Populate(jObject.CreateReader(), target);

        return target;
    }

    public override void WriteJson(JsonWriter writer, 
                                   object value,
                                   JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

public class MyEntityObjectConverter : JsonCreationConverter<MyEntityObject>
{
    protected override Person Create(Type objectType, JObject jObject)
    {
        if (FieldExists("entityobject3", jObject))
        {
            return new entityobject3();
        }
        else if (FieldExists("Someproperty1", jObject))
        {
            return new Someproperty1();
        }
        else
        {
            return null;
        }
    }

    private bool FieldExists(string fieldName, JObject jObject)
    {
        return jObject[fieldName] != null;
    }
}

and then call it this way:

List<MyEntityObject> myEntityObject = 
    JsonConvert.DeserializeObject<List<MyEntityObject>>(json, new MyEntityObjectConverter());

Upvotes: 1

Fred
Fred

Reputation: 3362

You can decorate your properties with an ignore anootation:

[JsonIgnore]

You should also add this to your class to tell JSON.Net that you're opting out members:

[JsonObject(MemberSerialization.OptOut)]

You can also write a custom serializer: http://blog.maskalik.com/asp-net/json-net-implement-custom-serialization/. This would be more work but you can adjust the process strictly to your needs.

Upvotes: 1

Related Questions