(null) values for Core Data parent entity properties
Date : March 29 2020, 07:55 AM
this one helps. The Client to Order relationship is set one-to-one such that there can only be one Order object in Client.order at anyone one time. Whenever you set a new Order-->Client relationship it bumps the previous Order object out of the relationship. You have this: Client{
name:string
order<-->Order.client
}
Order{
date:date
client<-->Client.order
}
Client{
name:string
orders<-->>Order.client
}
Order{
date:date
client<<-->Client.orders
}
|
c# - Returning default values for null properties, when the parent of these properties can or can not be null
Date : March 29 2020, 07:55 AM
it helps some times There is a strategy for dealing with this, involving the "Maybe" Monad. Basically it works by providing a "fluent" interface where the chain of properties is interrupted by a null somewhere along the chain.
|
JSON.NET serialize JObject while ignoring null properties
Tag : chash , By : kbrust
Date : March 29 2020, 07:55 AM
this will help You can use a recursive helper method like the one below to remove the null values from your JToken hierarchy prior to serializing it. using System;
using Newtonsoft.Json.Linq;
public static class JsonHelper
{
public static JToken RemoveEmptyChildren(JToken token)
{
if (token.Type == JTokenType.Object)
{
JObject copy = new JObject();
foreach (JProperty prop in token.Children<JProperty>())
{
JToken child = prop.Value;
if (child.HasValues)
{
child = RemoveEmptyChildren(child);
}
if (!IsEmpty(child))
{
copy.Add(prop.Name, child);
}
}
return copy;
}
else if (token.Type == JTokenType.Array)
{
JArray copy = new JArray();
foreach (JToken item in token.Children())
{
JToken child = item;
if (child.HasValues)
{
child = RemoveEmptyChildren(child);
}
if (!IsEmpty(child))
{
copy.Add(child);
}
}
return copy;
}
return token;
}
public static bool IsEmpty(JToken token)
{
return (token.Type == JTokenType.Null);
}
}
string json = @"
{
""Foo"": {
""P1"": null,
""P2"": ""hello world"",
""P3"": null,
""P4"": {
""P1"": 1,
""P2"": null,
""P3"": null
},
""FooArray"": [
{
""F1"": null,
""F2"": null,
""F3"": null
}
]
},
""Bar"": null
}";
JToken token = JsonHelper.RemoveEmptyChildren(JToken.Parse(json));
Console.WriteLine(token.ToString(Formatting.Indented));
{
"Foo": {
"P2": "hello world",
"P4": {
"P1": 1
},
"FooArray": [
{}
]
}
}
public static bool IsEmpty(JToken token)
{
return (token.Type == JTokenType.Null) ||
(token.Type == JTokenType.Array && !token.HasValues) ||
(token.Type == JTokenType.Object && !token.HasValues);
}
{
"Foo": {
"P2": "hello world",
"P4": {
"P1": 1
}
}
}
|
Issue while passing null values to nullable properties in web api call in .netcore web api project
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Everything sent in the query string is just a string. So, when you do something like toDate=null, you're actually saying "set toDate to "null"", i.e. the string "null". The modelbinder attempts to convert all the strings to the actual types you're binding to, but there's no conversion available that can turn "null" into a null DateTime. To set the value to null, you need to either pass no value toDate= or just omit the key entirely from the query string.
|
serialize object as null based on one of its properties
Tag : .net , By : hammer_1968
Date : March 29 2020, 07:55 AM
|