how to set 'ItemNullValueHandling' value dynamically in c# - c#

I am have requirement about to show property in response base on the class property value.If we are initializing the class by default all the properties all initializing. so, I want to ignore the properties based on the property.
Here is my class
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
Public class Student
{
public string name{get; set;}
public string address{get;set;}
public bool success{get;set;}
}
In the above class, Based on the value of success.I have to decide to show the properties in response even if they are null
I thought one solution was keeping
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
but dynamically i have to set the value of ItemNullValueHandling i didn't know that how to set dynamically can any one please suggest me.
Thanks in Advance!

You wont be able to dynamically change the serialisation settings using attributes as they are fixed at compile time.
However, you can pass an instance of JsonSerializerSettings to the Deserialize() method as shown below.
var showAllProperties = student.success;
var serializerSettings = new JsonSerializerSettings {
NullValueHandling = showAllProperties ? NullValueHandling.Include : NullValueHandling.Ignore;
}
var result = JsonConvert.SerializeObject(student, serializerSettings)

Related

How to avoid JsonConvert.PopulateObject enqueue on lists [duplicate]

This question already has answers here:
Json.Net PopulateObject Appending list rather than setting value
(1 answer)
How to apply ObjectCreationHandling.Replace to selected properties when deserializing JSON?
(1 answer)
Closed 1 year ago.
I have this silly class:
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public List<int> NumbersList { get; set; }
}
And I have this two jsons (the second one coming out from a conditional serialization trough ShouldSerialize)
var json1 = "{\"Name\":\"Joe\",\"Surname\":\"Satriani\",\"Age\":40,\"NumbersList\":[10,20,30]}";
var json2= "{\"Name\":\"Mike\",\"NumbersList\":[40,50,60]}";
Also, I have a silly class to display the results:
private void showResult(object theClass)
{
var result = JsonConvert.SerializeObject(theClass);
Debug.WriteLine("result: " + result);
}
Now, I create a class with the first json:
var myPerson = JsonConvert.DeserializeObject<Person>(json1);
And everything is fine: the class gets all the values it should:
showResult(myPerson);
result: {"Name":"Joe","Surname":"Satriani","Age":40,"NumbersList":[10,20,30]}
Now, I want to apply the second json to the already existing class instance:
JsonConvert.PopulateObject(json2, myPerson);
But... this is what I get:
showResult(myPerson);
result: {"Name":"Mike","Surname":"Satriani","Age":40,"NumbersList":[10,20,30,40,50,60]}
So, as far as I understand, the PopulateObject correctly rewrites, as expected, the standard field properties (because I don't get "JoeMike" as Name, I get only "Mike" and this if fine), however, it appends list/indexed ones, because I don't obtain "40,50,60" but "10,20,30,40,50,60"...
So, my question is: is there a way to avoid PopulateObject to deliberately append List/Index/Enumerable objects ?
In the JsonSerializerSettings class, set the ObjectCreationHandling value to ObjectCreationHandling.Replace. With this setting, the information is no longer appended but copied.
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
ObjectCreationHandling = ObjectCreationHandling.Replace
};
JsonConvert.PopulateObject(json2, myPerson, jsonSerializerSettings);
ObjectCreationHandling setting

Ignore some of the properties during Json.NET serializing but required on others

I am using the Json.Net to serialize an object.
This is the code:
var jsonText = JsonConvert.SerializeObject(objectToSerialize, Formatting.Indented,
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
The DefaultValueHandling set to ignore so the result string do not contain properties with default values.
In the Serialized class, there is a unique property that should be on the result string anyway,
So i'm using an attribute to make sure the property will be there:
[JsonProperty(PropertyName = "componentMainVersion", Required = Required.Always)]
public ushort Version { get; set; }
The problem is that the property do not generated to the string.
And i'm getting an exception when deserializing the string back to object:
"Newtonsoft.Json.Schema.JsonSchemaException: 'Required properties are
missing from object: Version..."
How can I make sure the property will be in the result string anyway (default value or not)?
Try the below code
[JsonProperty(PropertyName = "componentMainVersion", DefaultValueHandling = DefaultValueHandling.Include)]
public ushort Version { get; set; }
Required is a different property which makes sure that value for the property is required always

How do I use the generated JsonProperty Name = name from JSON file to access a single element in a corresponding C# Class

How do I use the (generated) JsonProperty Name = name from JSON file to access a single element in a corresponding C# Class?
Here is what I am trying to do …
I have a Xamarin “classic” Newtonsoft application where I load a JSON file into a C# class MagicTime.
Here is the first part of my class - This was generated automatically by the newtonsoft web site using my JSON file
public partial class MagicTime
{
[JsonProperty("Accel Delay")]
public long AccelDelay { get; set; }
[JsonProperty("Accel Period")]
public long AccelPeriod { get; set; }
[JsonProperty("Alt DT Step")]
public long AltDtStep { get; set; }
[JsonProperty("Beep On Command")]
public bool BeepOnCommand { get; set; }
[JsonProperty("Bunt Step")]
public long BuntStep { get; set; }
[JsonProperty("Bunt Timeout")]
This is how load/deserialize an instant where loadp is string variable containing the contents of the JSON file
MagicTime MT = JsonConvert.DeserializeObject<MagicTime>(loadp );
This works fine
In the application I modify the values of some data element e.g
Looking at the above – MT.AccelDelay = 21;
I then reverse the process and write /Serialize I out
That work too.
Now I have an new requirement to use the JsonProperty name to access the corresponding C# data item
It the example above I want to use [JsonProperty("Accel Delay")] to access the corresponding c# element MT.AccelDelay.
I have seen examples where a JSON string is loaded into JObject to do this but not my case where is no (that I can see) JObject
You can use Newtonsoft's ContractResolver for this purpose, as it defines how Json.NET maps from c# objects to JSON objects.
First, define the following extension method:
public static partial class JsonExtensions
{
static readonly IContractResolver defaultResolver = new JsonSerializer().ContractResolver;
public static T GetJsonPropertyValue<T>(object obj, string propertyName, IContractResolver resolver = null)
{
resolver = resolver ?? defaultResolver;
var contract = resolver.ResolveContract(obj.GetType());
if (contract is JsonObjectContract objectContract)
{
var property = objectContract.Properties.GetClosestMatchProperty(propertyName);
if (property == null)
throw new JsonException(string.Format("Unknown property {0}", propertyName));
return (T)property.ValueProvider.GetValue(obj);
}
throw new JsonException(string.Format("Invalid contract {0}", contract));
}
}
And now you can do:
var accelDelay = JsonExtensions.GetJsonPropertyValue<long>(MT, "Accel Delay");
If you don't know the type in advance, you can just do:
var accelDelay = JsonExtensions.GetJsonPropertyValue<object>(MT, "Accel Delay");
And if you are using camel casing, do:
var resolver = new CamelCasePropertyNamesContractResolver();
var accelDelay = JsonExtensions.GetJsonPropertyValue<object>(MT, "Accel Delay", resolver);
Notes:
If the incoming obj maps to something other than a JSON object (e.g. an array or primitive) then GetJsonPropertyValue() will throw an exception. It will also throw if the property is set-only.
If you need a way to discover all the JSON property names for a given type, see Get a list of JSON property names from a class to use in a query string, especially this answer.
Demo fiddle here.

C# DataContractJsonSerializer ReadObject not throwing error if member is missing

I have a user class like
public class User
{
public string UserName {get;set;}
public string Application {get;set;}
}
Now, I am using it like
var jsonSerializer = new DataContractJsonSerializer(typeof(User));
var objApp = (User)jsonSerializer.ReadObject(new MemoryStream(Encoding.Unicode.GetBytes(JsonInput)));
But my JSON JsonInput doesn't contain both values for example Application is not available in the JSON. This still serializes with on only UserName.
The JSON and class above is an example, there are quite a few members for me to check them individually !
I want to make sure, the JSON contains all the members of the class, if not , throw error.
But I can't seem to find way. What am I missing here?
If you use Newtonsoft (which you can install with Install-Package Newtonsoft.Json) - You can set the MissingMemberHandling property of the settings;
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MissingMemberHandling = MissingMemberHandling.Error;
Then pass it;
var userObj = JsonConvert.DeserializeObject<User>(jsonInput, settings);
If you must use Datacontract instead, you can decorate your User object with required attributes;
[DataMember(IsRequired=true)]
public string? RequiredProperty { get; set; }

Serializing published content in umbraco

I am currently writing an api for a custom application my company is writing. Part of this involves getting published content out in JSON format. When I try serializing ipublishedcontent directly it obviously attempts to serialize all of the umbraco data and relations that I simply don't need (in fact it fails with a stack overflow). Is there a way to get just the custom properties from an item of content without specifying the fields?
I am using webapi and passing it objects to serialize itself and I'm using a dynamic to manually specify the fields. The Product type which I'm initially selecting into is from modelsbuilder. My code currently looks a little like this:
public object Get(string keywords = "")
{
// Get Data from Umbraco
var allProducts = Umbraco.TypedContent(1100).Children.Select(x => new Product(x));
if (keywords != "")
{
allProducts = allProducts.Where(x => x.Name.Contains(keywords));
}
return allProducts.Select(x => new
{
id = x.Id,
name = x.Name,
price = x.Price
});
}
It seems to me that there should be a simple way to do this without having to create a dynamic with just the fields I want but I can't work it out. I just don't want to have to change my code every time the document type in umbraco changes!
You can use Ditto to map your data into an object.
Create an object with properties that match the alias's of your fields (case insensitive)
public class Product{
public int id {get;set;}
public string name {get;set;}
public string price {get;set;}
}
Then map a single or collection of IPublishedContent objects using .As
return allProducts.As<Product>();
You can use the UmbracoProperty attribute to specify the alias too if it is different than you need for your json or use the JsonProperty attribute to change the name on serialize.
Take a look at the code in the MemberListView - it does a similar thing while retrieving Members without knowing in advance what the properties on the MemberType will be:
https://github.com/robertjf/umbMemberListView/blob/master/MemberListView/Models/MemberListItem.cs
For example:
[DataContract(Name = "content", Namespace = "")]
public class MemberListItem
{
// The following properties are "known" - common to all IPublishedContent
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "contentType")]
public IContentType ContentType { get; set; }
// This one contains a list of all other custom properties.
private Dictionary<string, string> properties;
[DataMember(Name = "properties")]
public IDictionary<string, string> Properties
{
get
{
if (properties == null)
properties = new Dictionary<string, string>();
return properties;
}
}
}
MemberListView converts to this from a list of SearchResult using AutoMapper, but you could just as easily map it from IPublishedContent.

Categories

Resources