Предотвращать $id/$ref при сериализации объектов с использованием веб-API и JSON.NET

Я не могу помешать использованию Web API/JSON.NET при использовании Newtonsoft.Json.PreserveReferencesHandling.Objects при сериализации объектов. Другими словами, $id/$ref всегда используются в сериализованных объектах, несмотря на следующие настройки:

public class MvcApplication : System.Web.HttpApplication {

    protected void Application_Start () {
        WebApiConfig.Register(GlobalConfiguration.Configuration);
    }

}

public static class WebApiConfig {

    public static void Register (HttpConfiguration config) {
        JsonMediaTypeFormatter jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().Single();
        jsonFormatter.UseDataContractJsonSerializer = false;
        jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
        jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
    }

}

Любые идеи?

Ответы

Ответ 1

Если вы используете атрибуты сериализации на своих объектах (например, DataContract), из JSON.Net документации по атрибутам сериализации:

Помимо использования встроенных атрибутов Json.NET, Json.NET также ищет [SerializableAttribute] [2] (если для параметра IgnoreSerializableAttribute на DefaultContractResolver установлено значение false) [DataContractAttribute] [3], [DataMemberAttribute] [ 4] и [NonSerializedAttribute] [5]... при определении того, как JSON должен быть сериализован и десериализован.

Он также говорит следующее:

Примечание

Атрибуты Json.NET берут на себя ответственность за стандартные атрибуты сериализации .NET, например. если оба свойства JsonPropertyAttribute и DataMemberAttribute присутствуют в свойстве и оба настраивают имя, будет использовано имя из JsonPropertyAttribute.

Кажется, решение проблемы состоит в том, чтобы добавить [JsonObject(IsReference = false)] к вашему объекту (-ам) следующим образом:

[DataContract(IsReference = true)]
[JsonObject(IsReference = false)]
public class MyObject
{
    [DataMember]
    public int MyProperty { get; set; }
}

Ответ 2

Поместите это в Global.asax для настройки обработки ссылок. PreserveReferencesHandling не должен быть "All"

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;

Ответ 3

Вот несколько javascript, которые я использую для обработки объектов $id/$ref на стороне клиента:

// function to return a JSON object form a JSON.NET serialized object with $id/$ref key-values
// obj: the obj of interest.
// parentObj: the top level object containing all child objects as serialized by JSON.NET.
function getJsonNetObject(obj, parentObj) {
    // check if obj has $id key.
    var objId = obj["$id"];
    if (typeof (objId) !== "undefined" && objId != null) {
        // $id key exists, so you have the actual object... return it
        return obj;
    }
    // $id did not exist, so check if $ref key exists.
    objId = obj["$ref"];
    if (typeof (objId) !== "undefined" && objId != null) {
        // $ref exists, we need to get the actual object by searching the parent object for $id
        return getJsonNetObjectById(parentObj, objId);
    }
    // $id and $ref did not exist... return null
    return null;
}

// function to return a JSON object by $id
// parentObj: the top level object containing all child objects as serialized by JSON.NET.
// id: the $id value of interest
function getJsonNetObjectById(parentObj, id) {
    // check if $id key exists.
    var objId = parentObj["$id"];
    if (typeof (objId) !== "undefined" && objId != null && objId == id) {
        // $id key exists, and the id matches the id of interest, so you have the object... return it
        return parentObj;
    }
    for (var i in parentObj) {
        if (typeof (parentObj[i]) == "object" && parentObj[i] != null) {
            //going one step down in the object tree
            var result = getJsonNetObjectById(parentObj[i], id);
            if (result != null) {
                // return found object
                return result;
            }
        }
    }
    return null;
}

Ответ 4

[JsonIgnore] работал у меня. В рамках модели:

    [JsonIgnore] 
    public virtual ICollection<cell_order> cell_order { get; set; }

К сожалению, это необходимо сделать для каждого случая, когда это необходимо.