none
Class/Property Level DataContractJsonSerializer Recognized Attribute

    Question

  • Hi, Everybody...

    I wrote a generic method that takes a TRequest class and serializes it into a JSON string.  See code below.

            public string ObjectToJsonString<TRequest>(TRequest request)
            {
                var memoryStream = new MemoryStream();
                var dataContractJsonSerializer = new DataContractJsonSerializer(typeof(TRequest));
                var streamReader = new StreamReader(memoryStream);
                dataContractJsonSerializer.WriteObject(memoryStream, request);
                memoryStream.Position = 0;
                return streamReader.ReadToEnd();
            }

    TRequest is any class that contains public string properties.  See example below.

        public class SomeRequest
        {
            public string someRequestProperty{ get; set; }
            public string someOtherRequestProperty{ get; set; }
            public string yetAnotherRequestProperty{ get; set; }
            public string andAnotherRequestProperty{ get; set; }
        }

    When serialized, it returns the following JSON string.  See example below.

    Note: JSON string contains NO new lines.  It is displayed here for readability.

    {
       "someRequestProperty": "someRequestPropertyValue",
       "someOtherRequestProperty": "someOtherRequestPropertyValue",
       "yetAnotherRequestProperty": null,
       "andAnotherRequestProperty": "andAnotherRequestPropertyValue"
    }

    Note: If any property(ies) is(are) not initialized (i.e., default value = null) or purposely set to null, the method returns a JSON string with explicitly specificed null value property(ies).

    Is there a class or property level attribute that tells the DataContractJsonSerializer NOT to generate explicitly specified null value property(ies)? 

    Or is there a DataContractJsonSerializer method that can be overridden whereby if the value of a property is not null, then serialize the property.  Otherwise return (i.e., do nothing)?

    See desired JSON string below.

    {
       "someRequestProperty": "someRequestPropertyValue",
       "someOtherRequestProperty": "someOtherRequestPropertyValue",
       "andAnotherRequestProperty": "andAnotherRequestPropertyValue"
    }
    Note: yetAnotherRequestProperty is unspecified and therefore "assumed" to be null.

    Friday, February 09, 2018 7:12 PM

All replies

  • Hi Informatosaurus,

    For your requirement, it seems you want to serialize an object to string and ignore the null value.

    I would suggest you try code below:

                var request = new SomeRequest {
                    andAnotherRequestProperty = "andAnotherRequestProperty",
                    someOtherRequestProperty = "andAnotherRequestProperty",
                    //someRequestProperty= "andAnotherRequestProperty",
                    yetAnotherRequestProperty= "andAnotherRequestProperty"
                };
                string result = JsonConvert.SerializeObject(request, new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 12, 2018 5:10 AM
    Moderator
  • Hi, Tao...

    It seems like you are using Json.NET.  I don't have Json.NET and Json.NET is not an option.

    I need to be able to do this within the .NET Framework.

    If you know how, please let me know.

    Thank-you for taking the time and for your suggestion.

    Monday, February 12, 2018 6:46 PM
  • Hi Informatosaurus,

    If so, you may try to add [DataMember(EmitDefaultValue = false)] attribute to every property.

    SomeRequest

        [DataContract]
        public class SomeRequest
        {
            [DataMember(EmitDefaultValue = false)]
            public string someRequestProperty { get; set; }
            [DataMember(EmitDefaultValue = false)]
            public string someOtherRequestProperty { get; set; }
            [DataMember(EmitDefaultValue = false)]
            public string yetAnotherRequestProperty { get; set; }
            [DataMember(EmitDefaultValue = false)]
            public string andAnotherRequestProperty { get; set; }
        }

    Call Code

            private void NullProperty_Click(object sender, EventArgs e)
            {
                var request = new SomeRequest {
                    andAnotherRequestProperty = "andAnotherRequestProperty",
                    someOtherRequestProperty = "andAnotherRequestProperty",
                    //someRequestProperty= "andAnotherRequestProperty",
                    yetAnotherRequestProperty= "andAnotherRequestProperty"
                };
                //string result = JsonConvert.SerializeObject(request, new JsonSerializerSettings
                //{
                //    NullValueHandling = NullValueHandling.Ignore
                //});
                string result = ObjectToJsonString(request);
            }
            public string ObjectToJsonString<TRequest>(TRequest request)
            {
                var memoryStream = new MemoryStream();
                var dataContractJsonSerializer = new DataContractJsonSerializer(typeof(TRequest));
                
                var streamReader = new StreamReader(memoryStream);
                dataContractJsonSerializer.WriteObject(memoryStream, request);
                memoryStream.Position = 0;
                return streamReader.ReadToEnd();
            }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 13, 2018 3:18 AM
    Moderator