NetCore unified treatment webapi return null to ""

Original: NetCore unified treatment webapi return null to ""

Database parts table field allows null values, field type the code entity class corresponding nullable type Nullable <>, such as int?, DateTime?, Null value field serialized return values ​​are null, the front end of the corresponding field assignment requires do null value judgment, with the thought of global replace null to ""

Find online programs are rewritten DefaultContractResolver, but this method can only solve the string type null → "" problem, not valid for other types of air.

Research on the lower git Newtonsoft.Json and .netcore MVC source code

End-use custom JsonOutputFormatter ways:

public class MyJsonOutputFormatter : JsonOutputFormatter
        {
            public MyJsonOutputFormatter(JsonSerializerSettings serializerSettings) : base(serializerSettings, ArrayPool<char>.Shared)
            {
            }
            public new JsonSerializerSettings SerializerSettings => base.SerializerSettings;
            protected override JsonWriter CreateJsonWriter(TextWriter writer)
            {
                if (writer == null)
                {
                    throw new ArgumentNullException(nameof(writer));
                }
                var jsonWriter = new NullJsonWriter(writer)
                {
                    ArrayPool = new JsonArrayPool<char>(ArrayPool<char>.Shared),
                    CloseOutput = false,
                    AutoCompleteOnClose = false
                };
                return jsonWriter;
            }
        }
        public class NullJsonWriter : JsonTextWriter
        {
            public NullJsonWriter(TextWriter textWriter):base(textWriter)
            {
            }
            public override void WriteNull()
            {
                this.WriteValue(String.Empty);
            }
        }

 

Modify code inside Startup

// services.AddMvc (). AddJsonOptions (the Option => {
             //     // configuration capitalization issue, the default is the first letter lowercase, the need to change the configuration according to the project
             //     // option.SerializerSettings.ContractResolver = new new Newtonsoft.Json. Serialization.DefaultContractResolver ();
             //     // configuration serialization format for the time stamp
             //     // option.SerializerSettings.ContractResolver new new NullToEmptyStringResolver = ();
             //     option.SerializerSettings.DateFormatString = "the MM-dd-YYYY HH: mm: SS ";
             // }); 
            services.AddMvc (config => 
            { 
                var Settings = new newJsonSerializerSettings ();
                 // set the first letter in lower case, then if not changed DefaultContractResolver 
                settings.ContractResolver = new new CamelCasePropertyNamesContractResolver ();
                 // uniformly set the date format in JsonResult     
                settings.DateFormatString = " YYYY the MM-dd-HH: mm: SS " ; 
                config.OutputFormatters.Insert ( 0 , new new MyJsonOutputFormatter (Settings)); 
            });

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11582289.html