I using json .NET to deserialize my json string to my model.
below is what I am trying to achieve, please advice what is best way ..
When there is no data my response looks like below
json string = "{\"message\":\"SUCCESS\",\"result\":null}"
the result eventually is tied to a view. So when the response is null I would like to intialize my view with default model values. And so would like to call Default constructor on deserialization. Default constructor looks like below.
public ProfileModel()
{
this.DefaultTab = DefaultTabOption.PROFILE;
this.DataLoadPosition = new DataLoadPositionOptionsModel();
this.DataLayout = new DataLayoutOptionsModel();
this.NAData = new NADataOptionsModel();
this.DataTable = new DataDisplayOptionsModel();
}
But when there is data, the reponse looks like below.
{"message":"SUCCESS","result":{"dataLayout":{"vertical":false},"dataLoadPosition":{"cell":"B2","cursorLocation":false},"dataTable":{"decimalPts":1},"defaultTab":"BROWSE","naData":{"custom":"","naDataOption":"FORWARDFILL"}}}
In this case, I would like to call my parameterized constructor so that models are initialized correctly.
Deserialization code:
using (StreamReader reader = new StreamReader(responseStream))
{
var t = JsonConvert.DeserializeObject<T>(reader.ReadToEnd());
return t;
}
Where T is my main Model which initilialises multiple models. Below is the parameterized contructor.
public ProfileModel(DefaultTabOption defaultTabModel,
DataLoadPositionOptionsModel dataLoadPositionOption ,
DataLayoutOptionsModel dataLayoutOptios ,
NADataOptionsModel naDataOptions ,
DataDisplayOptionsModel dataTableOptions)
{
this.DefaultTab = defaultTabModel;
this.DataLoadPosition = dataLoadPositionOption;
this.DataLayout = dataLayoutOptios;
this.NAData = naDataOptions;
this.DataTable = dataTableOptions;
}
What is they best way to deserialize so that default constructor is called when null and parameterized is call when not null. I tried the ConstructorHandling, NullValueHandling but I am not able to achieve desired results.
I've a little simplified your model
public sealed class ProfileModel
{
public ProfileModel()
{
DataLayout = new DataLayoutOptionsModel();
}
public ProfileModel(DataLayoutOptionsModel dataLayout)
{
DataLayout = dataLayout;
}
public DataLayoutOptionsModel DataLayout { get; private set; }
}
public sealed class DataLayoutOptionsModel
{
public bool Vertical { get; set; }
}
public class ResultModel
{
public string Message { get; set; }
public ProfileModel Result { get; set; }
}
To choose concrete constructor, you have to implement custom JsonConverter, for example
public sealed class MyJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(ProfileModel).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
ProfileModel target;
JObject jObject = JObject.Load(reader);
JToken resultToken = jObject["Result"];
//This is result null check
if (resultToken.Type == JTokenType.Null)
{
target = new ProfileModel();
}
else
{
var optionsModel = resultToken["DataLayout"].ToObject<DataLayoutOptionsModel>();
target = new ProfileModel(optionsModel);
}
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override void WriteJson(JsonWriter writer,
object value,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
So, result serialization looks like:
[Fact]
public void Test()
{
string tinyJsonl = "{\"Message\":\"SUCCESS\",\"Result\":null}";
var defaultProfile = JsonConvert.DeserializeObject<ProfileModel>(tinyJsonl, new MyJsonConverter());
Assert.False(defaultProfile.DataLayout.Vertical);
string fullJson = "{\"Message\":\"SUCCESS\",\"Result\":{\"DataLayout\":{\"Vertical\":true}}}";
var customProfile = JsonConvert.DeserializeObject<ProfileModel>(fullJson, new MyJsonConverter());
Assert.True(customProfile.DataLayout.Vertical);
}
Related
I have an API with multiple endpoints. I'd like to add a property to all endpoint responses, without adding it to each endpoint response model individually.
Ex:
public class MyClass
{
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass
{
public string MyOtherProperty { get; set; } = "World";
}
public class MyController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
{
// implementation omitted
}
[HttpPost]
public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
{
// implementation omitted
}
}
Calling either endpoint returns a JSON representation of MyClass or MyOtherClass as appropriate - i.e.
{ "MyProperty":"Hello" } or { "MyOtherProperty":"World" }
I want to add a property, say a string ApiName, to all endpoints in the API, so that the result of the above code would be either (as appropriate)
{ "MyProperty":"Hello", "ApiName":"My awesome API" }
or
{ "MyOtherProperty":"World", "ApiName":"My awesome API" }
Is there a way to hook into the JSON-stringified result just before returning and add a top-level property like that? If so, I presume I'd have to wire it up in startup.cs, so I've been looking at app.UseEndpoints(...) methods, but haven't found anything that's worked so far. Either it's not added the property, or it's replaced the original result with the new property.
Thanks in advance!
Use Newtonsoft.Json in your net web api
Register a custom contract resolver in Startup.cs:
builder.Services.AddControllers()
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = CustomContractResolver.Instance);
The implementation:
public class CustomContractResolver : DefaultContractResolver {
public static CustomContractResolver Instance { get; } = new CustomContractResolver();
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
// add new property
...
properties.Add(newProp);
return properties;
}}
See more Json.net Add property to every class containing of a certain type
You can add a base class with the shared property. Should work for both XML and JSON.
public class MyApiClass
{
public string ApiName => "MyAwesomeApi";
}
public class MyClass : MyApiClass
{
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass : MyApiClass
{
public string MyOtherProperty { get; set; } = "World";
}
public class MyController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
{
// implementation omitted
}
[HttpPost]
public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
{
// implementation omitted
}
}
My 0.02 cents says to implement an abstract base class.
Abstract class inheritance look similar to a standard inheritance.
public class MyClass:MyAbstractClass
{
[JsonPropertyName("Class Property")]
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass:MyAbstractClass
{
[JsonPropertyName("Class Property")]
public string MyOtherProperty { get; set; } = "World";
}
However the abstract class will allow you to implement additional features in the event you need them in the future.
public abstract class MyAbstractClass{
[JsonPropertyName("API Name")]
public string ApiName{get;set;}="My Aweomse API";
//Just a thought if you want to keep track of the end point names
//while keeping your object names the same
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string EndPointName{
get{
return get_endpoint_name();
}}
private string get_endpoint_name(){
return this.GetType().Name;
}
//May as well make it easy to grab the JSON
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string As_JSON{
get {
return to_json();
}}
private string to_json(){
object _myObject = this;
string _out;
JsonSerializerOptions options =
new JsonSerializerOptions {
WriteIndented = true };
_out =
JsonSerializer.Serialize(_myObject, options);
return _out;
}
}
Probably should have implemented a generic return object, then you could just loop through the task results. I suppose you still can if you have the task return only the JSON string.
public static void run(){
Task<MyClass> _t0 = task0();
Task<MyOtherClass> _t1 = task1();
Task[] _tasks = new Task[]{_t0,_t1};
Task.WhenAll(_tasks).Wait();
Console.WriteLine(""
+$"{_t1.Result.ApiName}:\n"
+$"End Point: {_t1.Result.EndPointName}:\n"
+$"JSON:\n{_t1.Result.As_JSON}");
Console.WriteLine(""
+$"{_t0.Result.ApiName}:\n"
+$"End Point: {_t0.Result.EndPointName}:\n"
+$"JSON:\n{_t0.Result.As_JSON}");
}
private static Task<MyClass> task0(){
return Task.Run(()=>{
Console.WriteLine("Task 0 Doing Something");
return new MyClass();
});
}
private static Task<MyOtherClass> task1(){
return Task.Run(()=>{
Console.WriteLine("Task 1 Doing Something");
return new MyOtherClass();
});
}
And of course the aweosome...awesome:-) results:
Another thought is that you could implement your two different tasks as abstract methods, but that's a different conversation all together.
In addition to all of the great answers, I prefer to use Action Filter and ExpandoObject.
In Program File you should add your custom action Filter.
builder.Services.AddControllers(opt =>
{
opt.Filters.Add<ResponseHandler>();
});
and ResponseHandler acts like below:
public class ResponseHandler : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach (var propertyInfo in (context.Result as ObjectResult).Value.GetType().GetProperties())
{
var currentValue = propertyInfo.GetValue((context.Result as ObjectResult).Value);
expando.Add(propertyInfo.Name, currentValue);
}
dynamic result = expando as ExpandoObject;
result.ApiName = context.ActionDescriptor.RouteValues["action"].ToString();
context.Result = new ObjectResult(result);
}
public void OnActionExecuting(ActionExecutingContext context)
{
}
}
I'm writing a simple windows service, and I want to use a Dependency Injection.
On service start, the app read the JSON string from file, and deserialize it to object.
One of property in JSON is list of 'Bar' objects, and in 'Bar' constructor I want to inject FooService.
I suspected that following code will works, but 'service' in 'Bar' constructor is null.
Can anyone expalin me, why this code not working, and how to change it to works?
That's my code:
NinjectBinding.cs
class NinjectBindings : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<IFooService>().To<FooService>();
Bind<Bar>().ToSelf().WithConstructorArgument("service", ctx => ctx.Kernel.Get<IFooService>());
}
}
Deserializator.cs
internal class Deserializator
{
internal static void GetFilePaths()
{
var json = JsonConvert.DeserializeObject<Deserializator>("{}");///... Read from JSON File
}
public List<Bar> BarList { get; set; }
}
Bar.cs
public class Bar {
private readonly IFooService _fooService;
public Bar (IFooService service){
// here service is null
using (var kernel = new StandardKernel(new NinjectBindings())){
var kernelService = kernel.Get<IFooService>();
// here kernelService is NOT null
}
}
}
UPDATE
I found solution on https://www.newtonsoft.com/json/help/html/DeserializeWithDependencyInjection.htm
I've added NinjectResolver.cs
class NinjectResolver : DefaultContractResolver
{
private readonly IKernel _container;
public NinjectResolver(IKernel container)
{
_container = container;
}
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
if (_container.TryGet(objectType) != null)
{
JsonObjectContract contract = ResolveContact(objectType);
contract.DefaultCreator = () => _container.Get(objectType);
return contract;
}
return base.CreateObjectContract(objectType);
}
private JsonObjectContract ResolveContact(Type objectType)
{
return base.CreateObjectContract(objectType);
}
}
remove Bind from NinjectBindings, and change SerializationMethod to:
var json = JsonConvert.DeserializeObject<Deserializator>("{}", new
JsonSerializerSettings()
{
ContractResolver = new NinjectResolver(new StandardKernel(new NinjectBindings()))
});
And now - it works.
I wanted to create a class and use a method from the initialized class to change the property values of the calling instance. Somehow I have a knot in my brain and there seems to be a basic thinking error of mine. Maybe someone can help me figure it out.
Class Program
{
...
private void Initialize()
{
Zoo myZoo = new Zoo();
myZoo.Load();
Console.WriteLine(myZoo.ZooName);
}
}
and the Zoo-Class:
public class Zoo
{
public string ZooName { get; set; }
...
internal void Load()
{
Zoo myZoo = this;
using (StreamReader reader = File.OpenText(#"C:\Areas.json"))
{
JsonSerializer serializer = new JsonSerializer();
myZoo = (Zoo) serializer.Deserialize(reader, typeof(Zoo));
}
}
}
The JSON part works fine, but as soon as the Load()-method comes to an end the myZoo/this is set to NULL. Is there any possibility to use 'this' to modify the property values of the calling class instance?
You probably want to make a factory method on your class instead. This function will return a new instance of Zoo with the data from your json file.
Like this:
public class Zoo
{
public string ZooName { get; set; }
...
public static Zoo Init()
{
using (StreamReader reader = File.OpenText(#"C:\Areas.json"))
{
JsonSerializer serializer = new JsonSerializer();
var myZoo = (Zoo) serializer.Deserialize(reader, typeof(Zoo));
return myZoo;
}
}
}
In your Initialize function you can now create an instance like this:
private void Initialize()
{
var myZoo = Zoo.Init();
Console.WriteLine(myZoo.ZooName);
}
other way out.you can use ref keyword for the same.
Class Program
{
...
private void Initialize()
{
Zoo myZoo = new Zoo();
myZoo.Load(ref myZoo);
Console.WriteLine(myZoo.ZooName);
}
}
public class Zoo
{
public string ZooName { get; set; }
...
internal void Load(ref Zoo myZoo)
{
using (StreamReader reader = File.OpenText(#"C:\Areas.json"))
{
JsonSerializer serializer = new JsonSerializer();
myZoo = (Zoo) serializer.Deserialize(reader, typeof(Zoo));
}
}
}
You cannot set the this pointer. There also is no assignment operator in C# to overload.
You could copy all properties from your loaded zoo object into the this object.
A more common approach is to have a static factory method to do this for you:
public class Zoo
{
public string ZooName { get; set; }
...
public static Zoo Load(string file)
{
using (StreamReader reader = File.OpenText(file))
{
JsonSerializer serializer = new JsonSerializer();
return (Zoo) serializer.Deserialize(reader, typeof(Zoo));
}
}
}
Later call it like this:
Zoo z = Zoo.Load(#"C:\Areas.json");
I am posting this question after reading the posts available. I have an ASP.NET web api controller with following methods.
[DataContract]
public class CustomPerson
{
[DataMember]
public ulong LongId { get; set; }
}
[DataContract]
public class Employee : CustomPerson
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
}
Then in the controller
public class CustomController : ApiController
{
[HttpPost]
[ActionName("AddEmployee")]
public bool AddEmployee(Employee empInfo)
{
bool bIsSuccess = false;
// Code loginc here
bIsSuccess = true;
return bIsSuccess;
}
[HttpPost]
[ActionName("AddEmployeeCustom")]
public async Task<bool> AddEmployeeCustom()
{
string rawRequest = await Request.Content.ReadAsStringAsync();
bool bIsSuccess = false;
// Code loginc here
try
{
Employee emp = JsonConvert.DeserializeObject<Employee>(rawRequest);
}
catch (Exception ex)
{ }
return bIsSuccess;
}
}
When I call the following request to AddEmployee via soap ui the custom object is received without error i.e the empty value for LongId is ignored
{
"Name": "test1",
"Address": "Street 1",
"LongId": ""
}
When I call the AddEmployeeCustom method the runtime throws exception:
Error converting value "" to type 'System.UInt64'. Path 'LongId', line 4, position 14.
One option I read is to convert the incoming string to JObject and then create object of Employee class but I am trying to understand and mimic the behavior of default request handling mechanism when the incoming request is automatically handled by the controller and deserialized to Employee object
The problem is that your JSON is not valid for your model.
In the first method AddEmployee the process called Model Binding happens. MVC does the job of converting post content into the object. It seems its tolerant to type mismatch and forgives you the empty string.
In the second case, you try to do it yourself, and try simply run deserialization without validating input data. Newtonsoft JSON does not understand empty string and crashes.
If you still need to accept invalid JSON you may want to overwrite default deserialization process by implementing custom converter
public class NumberConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanConvert(Type objectType)
{
return typeof(ulong) == objectType;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = reader.Value.ToString();
ulong result;
if (string.IsNullOrEmpty(value) || !ulong.TryParse(value, out result))
{
return default(ulong);
}
return result;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And then specify your custom converter instance when calling deserialize
return JsonConvert.DeserializeObject<Employee>(doc.ToJson(), new NumberConverter());
I need to deserialize json for following class.
public class Test
{
public string Property { get; set; }
private Test()
{
//NOTHING TO INITIALIZE
}
public Test(string prop)
{
Property = prop;
}
}
I can create an instance of Test like
var instance = new Test("Instance");
considering my json something like
"{ "Property":"Instance" }"
How shall I create an object of Test class as my default constructor is private and I am getting object where Property is NULL
I am using Newtonsoft Json parser.
You can make Json.Net call the private constructor by marking it with a [JsonConstructor] attribute:
[JsonConstructor]
private Test()
{
//NOTHING TO INITIALIZE
}
Note that the serializer will still use the public setters to populate the object after calling the constructor.
Another possible option is to use the ConstructorHandling setting:
JsonSerializerSettings settings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Test t = JsonConvert.DeserializeObject<Test>(json, settings);
It doesn't seem like you need to take any extra steps.
Using Json.NET v6.0.8, the following C# program works inside LINQPad:
void Main()
{
var o = JsonConvert.DeserializeObject<Test>("{\"Property\":\"Instance\"}");
Debug.Assert(o.Property == "Instance",
"Property value not set when deserializing.");
}
public class Test
{
public string Property { get; set; }
private Test()
{
}
public Test(string propertyValue)
{
Property = propertyValue;
}
}
No need to create a Serializer setting and give assign ConstructorHandling here. Please remember to define the [JsonConstructor] attribute to the private constructor.
I have similar case with abstract BaseNode.cs and its concrete ComputerNode.cs implementation. You can create the classes, copy/paste the code below and do some experiment.
public abstract class BaseNode
{
[JsonConstructor] // ctor used when Json Deserializing
protected BaseNode(string Owner, string Name, string Identifier)
{
this.Name = Name;
this.Identifier = Identifier;
}
// ctor called by concrete class.
protected BaseNode(string [] specifications)
{
if (specifications == null)
{
throw new ArgumentNullException();
}
if (specifications.Length == 0)
{
throw new ArgumentException();
}
Name = specifications[0];
Identifier = specifications[1];
}
public string Name{ get; protected set; }
public string Identifier { get; protected set; }
}
public class ComputerNode: BaseNode
{
public string Owner { get; private set; }
[JsonConstructor] // not visible while creating object from outside and only used during Json Deserialization.
private ComputerNode(string Owner, string Name, string Identifier):base(Owner, Name, Identifier)
{
this.Owner = Owner;
}
public ComputerNode(string[] specifications):base(specifications)
{
Owner = specifications[2];
}
}
For JSon Read and Write following code helps -
public class Operation<T>
{
public string path;
public Operation()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "nodes.txt");
if (File.Exists(path) == false)
{
using (File.Create(path))
{
}
}
this.path = path;
}
public void Write(string path, List<T> nodes)
{
var ser = JsonConvert.SerializeObject(nodes, Formatting.Indented);
File.WriteAllText(path, ser);
}
public List<T> Read(string path)
{
var text = File.ReadAllText(path);
var res = JsonConvert.DeserializeObject<List<T>>(text);
return res;
}
}
All the best!
Today the short answer is: Rename the constructor parameter prop to property and your code will work fine.
public class Test
{
public string Property { get; }
public Test(string property)
{
Property = property;
}
}
Console.WriteLine(
JsonConvert.DeserializeObject(new Test("Instance")));
Newtonsoft.Json supports initializing properties using constructor parameters out of the box, without needing to set any additional attributes or changing any settings. The only constraint is that the parameter name needs to be a case insensitive match to the property name.
I discovered today that having a public constructor that takes parameters and no declared unparameterized constructor causes NewtonSoft to attempt to call the public constructor, the only one that it can find, since there is no explicit default constructor, and it cannot apparently find and call the default constructor provided by the framework unless it is the only constructor.
Explicitly declaring a default constructor causes NewtonSoft to call the correct (unparameterized) constructor.