web.api only serializing hidden fields

断了今生、忘了曾经 提交于 2019-12-11 10:03:42

问题


I'm experiencing a strange behaviour. My web.api is returning only hiddenfields from my ObjectCollection on a GET request. This is my controller:

// GET: api/UserDocuments
[Route("api/UserDocuments/User/{userName}")]
public List<DocIndex> Get(string userName)
{
    User usuari = Humanisme.User.LoadByUserName(userName);
    List<DocIndex> resposta = DocumentCollection.LoadIndexPerUsuari(usuari);
    return resposta;
}

And this is the object as it gets generated from the BOM:

    namespace Humanisme
    {
    using CodeFluent.Runtime;
    using CodeFluent.Runtime.Utilities;


    // CodeFluent Entities generated (http://www.softfluent.com). Date: Tuesday, 01 March 2016 11:52.
    // Build:1.0.61214.0820
    [System.CodeDom.Compiler.GeneratedCodeAttribute("CodeFluent Entities", "1.0.61214.0820")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DataObjectAttribute()]
    public partial class DocIndex : CodeFluent.Runtime.ICodeFluentLightEntity
    {

        private int _id = -1;

        [System.NonSerializedAttribute()]
        private Humanisme.User _user = ((Humanisme.User)(null));

        private string _lat = default(string);

        private string _lon = default(string);

        private string _etapaVital = default(string);

        private string _solvencia = default(string);

        private int _valoracio = CodeFluentPersistence.DefaultInt32Value;

        private System.DateTime _data = CodeFluentPersistence.DefaultDateTimeValue;

        private string _nom = default(string);

        public DocIndex()
        {
        }

        [System.ComponentModel.DefaultValueAttribute(((int)(-1)))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=false, Type=typeof(int))]
        [System.ComponentModel.DataObjectFieldAttribute(true)]
        public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public Humanisme.User User
        {
            get
            {
                return this._user;
            }
            set
            {
                this._user = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Lat
        {
            get
            {
                return this._lat;
            }
            set
            {
                this._lat = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Lon
        {
            get
            {
                return this._lon;
            }
            set
            {
                this._lon = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string EtapaVital
        {
            get
            {
                return this._etapaVital;
            }
            set
            {
                this._etapaVital = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Solvencia
        {
            get
            {
                return this._solvencia;
            }
            set
            {
                this._solvencia = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(CodeFluentPersistence.DefaultInt32Value)]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=false, Type=typeof(int))]
        public int Valoracio
        {
            get
            {
                return this._valoracio;
            }
            set
            {
                this._valoracio = value;
            }
        }

        [System.Xml.Serialization.XmlElementAttribute(IsNullable=false, Type=typeof(System.DateTime))]
        public System.DateTime Data
        {
            get
            {
                return this._data;
            }
            set
            {
                this._data = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Nom
        {
            get
            {
                return this._nom;
            }
            set
            {
                this._nom = value;
            }
        }

        protected virtual void ReadRecord(System.Data.IDataReader reader, CodeFluent.Runtime.CodeFluentReloadOptions options)
        {
            if ((reader == null))
            {
                throw new System.ArgumentNullException("reader");
            }
            if ((((options & CodeFluent.Runtime.CodeFluentReloadOptions.Properties) 
                        == 0) 
                        == false))
            {
                this._id = CodeFluentPersistence.GetReaderValue(reader, "Id", ((int)(-1)));
                this._user = new Humanisme.User();
                CodeFluent.Runtime.CodeFluentLightWeightPersistence.ReadRecord(reader, this._user, null, new CodeFluent.Runtime.Utilities.Pair<string, string>("Id", "User_Id"));
                this._lat = CodeFluentPersistence.GetReaderValue(reader, "Lat", ((string)(default(string))));
                this._lon = CodeFluentPersistence.GetReaderValue(reader, "Lon", ((string)(default(string))));
                this._etapaVital = CodeFluentPersistence.GetReaderValue(reader, "EtapaVital", ((string)(default(string))));
                this._solvencia = CodeFluentPersistence.GetReaderValue(reader, "Solvencia", ((string)(default(string))));
                this._valoracio = CodeFluentPersistence.GetReaderValue(reader, "Valoracio", ((int)(CodeFluentPersistence.DefaultInt32Value)));
                this._data = CodeFluentPersistence.GetReaderValue(reader, "Data", ((System.DateTime)(CodeFluentPersistence.DefaultDateTimeValue)));
                this._nom = CodeFluentPersistence.GetReaderValue(reader, "Nom", ((string)(default(string))));
            }
        }

        void CodeFluent.Runtime.ICodeFluentLightEntity.ReadRecord(System.Data.IDataReader reader)
        {
            this.ReadRecord(reader, CodeFluent.Runtime.CodeFluentReloadOptions.Default);
        }
    }
}

Calling the web.api get method returns this JSON:

[
  {
    "_id": 1,
    "_lat": null,
    "_lon": null,
    "_etapaVital": null,
    "_solvencia": null,
    "_valoracio": 0,
    "_data": "0001-01-01T00:00:00",
    "_nom": null
  }
]

Serializer (from WebApiConfig.cs)

JsonMediaTypeFormatter jsonFormatter = (JsonMediaTypeFormatter)config.Formatters.FirstOrDefault(f => f is JsonMediaTypeFormatter);
        if (jsonFormatter != null)
        {
           // jsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Include;
            jsonFormatter.UseDataContractJsonSerializer = true;
        }

回答1:


The classes generated by CodeFluent Entities are decorated by SerializableAttribute. This attribute changes the way Json.NET serialize or deserialize the object. You can configure Json.NET to ignore this attribute:

JsonMediaTypeFormatter jsonFormatter = (JsonMediaTypeFormatter)config.Formatters.FirstOrDefault(f => f is JsonMediaTypeFormatter);
if (jsonFormatter != null)
{
    jsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver()
    {
        IgnoreSerializableAttribute = true
    };
}

http://james.newtonking.com/archive/2012/04/11/json-net-4-5-release-2-serializable-support-and-bug-fixes

Json.NET now detects types that have the SerializableAttribute and serializes all the fields on that type, both public and private, and ignores the propertie

So you can use the service producer which will add the DataMemberAttribute or you can use the Json.NET Aspect to automatically add specific Json.NET attribute: Newtonsoft.Json.JsonObjectAttribute and Newtonsoft.Json.JsonPropertyAttribute.




回答2:


Finally found!

When dealing with web.api never, never, never forget to add the "Service Producer" subproducer attached to the standard BOM Producer in your model project.

You'll never notice any problem but at serializing when no attributes will be processed and only hidden properties (object fields) will be serialized at output.

Sorry for the nerd mistake, happy for the lesson learned.

Again, thanks Meziantou. You would never figured where the issue was originated, mainly because I didn't carried all the project details to the question.



来源:https://stackoverflow.com/questions/35723303/web-api-only-serializing-hidden-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!