JSON.Net Deseralize json pair to Object Propery - c#

Ok I have WebApi application that is sending back name value pairs like so
{'FirstName':'SomeGuy'}
On the server the FirstName field is not just a string, it is a generic object that hold additional information about FirstName, and is not send back from the client.
Here is a outline of the classes
public abstract class Field
{
protected object _value;
......More Properties/Methods
public bool HasValue
{
get { return _hasValue; }
}
public object Value
{
get { return _hasValue ? _value : null; }
}
protected void SetValue(object value, bool clearHasValue = false)
{
_value = value;
_hasValue = clearHasValue ?
false :
value != null;
}
}
public class Field<T> : Field
{
..Constructors and methods
public new T Value
{
get { return _hasValue ? (T)_value : default(T); }
set { SetValue(value); }
}
}
So.. In theory I may be trying to bind to a model like
class FieldModel
{
public Field<string> FirstName { get; set; }
public Field<string> LastName { get; set; }
public Field<Decimal> Amount { get; set; }
public FieldModel()
{
FirstName = new Field<string>();
LastName = new Field<string>();
Amount = new Field<decimal>();
}
}
So here is the issue.. I want FirstName in my json object to deseralize to right property. Now if I modify the json package to {'FirstName.Value':'SomeGuy'} JSON.net works out of the box, but I really not to do that. I have been tying to make my own JsonConverter but have not been able to get that to work. So, I don't think this should be very hard, but I am a bit stuck.
EDIT
So.. I did come up with a solution that works, but I have to think there is a better way.. It uses dynamics and I have to think that I am missing an easy solution.
public class FieldConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
var internalVal = serializer.Deserialize(reader, objectType.GetGenericArguments().FirstOrDefault());
var retVal = existingValue as dynamic;
retVal.Value = internalVal as dynamic;
return retVal;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return objectType.IsSubclassOf(typeof(Field));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}

You can easily do this with JSON.NET's CustomCreationConverter. Here's an example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class PersonConverter : CustomCreationConverter<Person>
{
public override Person Create(Type objectType)
{
return new Employee();
}
}
And the usage:
string json = #"{
'Department': 'Furniture',
'JobTitle': 'Carpenter',
'FirstName': 'John',
'LastName': 'Joinery',
'BirthDate': '1983-02-02T00:00:00'
}";
Person person = JsonConvert.DeserializeObject<Person>(json, new PersonConverter());
Console.WriteLine(person.GetType().Name);
// Employee
Employee employee = (Employee)person;
Console.WriteLine(employee.JobTitle);
// Carpenter

Related

Deserialize an abstract class that has only a getter using Newtonsoft

I'm trying to deseralize JSON I'm getting:
[
{
"Name":"0",
"Health":0,
"TypeName":"SpellInfo",
"Info":{
"Effect":1,
"EffectAmount":4
}
},
{
"Name":"1",
"Health":0,
"TypeName":"MonsterInfo",
"Info":{
"Health":10,
"AttackDamage":10
}
},
...
...
]
Created a class to handle the JSON:
[System.Serializable]
public class CardDataStructure
{
public string Name;
public int Health;
public string TypeName;
public Info Info;
}
I managed to get all the info I needed but the Info. From the research I did, I created a JsonConverter from a link - https://blog.codeinside.eu/2015/03/30/json-dotnet-deserialize-to-abstract-class-or-interface/
Which is actually pretty close,
public class InfoConvert: JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Info));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
if (jo.ToString().Contains("Effect"))
{
if (jo["Effect"].Value<string>() is string)
return jo.ToObject<SpellInfo>(serializer);
}
if (jo.ToString().Contains("Health"))
{
if (jo["Health"].Value<string>() is string)
return jo.ToObject<MonsterInfo>(serializer);
}
return null;
}
}
(It would have been better to find it by 'typename' but I couldn't figure out how to do that, so went with something simple)
When checking 'jo', the properties are there and go to the correct class yet once out of the converter I get default properties and not the once the converter showed.
I can't find the link but on the Newtonsoft doc it said somewhere there's a problem with deserializing an abstract class and if the abstract class doesn't have a public setter.
Both monsterinfo and spellinfo inherit from info:
[Serializable]
public abstract class Info
{
}
The monsterinfo and spellinfo look basically the same. Problem is they don't have a public setters and I cannot change them right now.
{
[Serializable]
public class MonsterInfo: Info
{
[SerializeField]
private int m_Health;
public int Health => m_Health;
[SerializeField]
private int m_AttackDamage;
public int AttackDamage => m_AttackDamage;
}
}
So, when trying to deseralize the JSON:
string contents = File.ReadAllText(source);
contents = "{\"cards\":" + contents + "}";
JsonConverter[] converters = { new InfoConvert() };
cardsData = JsonConvert.DeserializeObject<Cards>(contents, new JsonSerializerSettings() {
Converters = converters, NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto});
*Cards is a list of CardDataStructure
Is it even possible to get the data in Info without giving them a public setter?
Best I got is all the data inside the JSON and an empty Monster/Spell Info.
At the end I just need to parse the json I'm getting, but while the 'name', 'health', 'typeinfo' are parsed correctly, info is always an empty object filled with 0s.
Edit: Corrected some things.
You should do that like this dude:
A marker interface for detecting the type or deserializing
A container class
Dto classes
//marker interface
public interface Info { }
public class HealthInfo : Info
{
public int MoreHealth { set; get; }
public int AttackDamage { set; get; }
}
public class SpellInfo : Info
{
public int Effect { set; get; }
public int EffectAmount { set; get; }
}
public class Card<T> where T : Info
{
public Card(string name, int health, T info)
{
this.Info = info;
this.Name = name;
this.Health = health;
}
public T Info { private set; get; }
public string Name { set; get; }
public int Health { set; get; }
}
public class InfoConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Card<Info>))
{
return true;
}
return false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jObject = JObject.Load(reader);
if (jObject.ContainsKey("TypeName"))
{
string typeName = jObject["TypeName"]?.ToString()?.Trim()?.ToLower();
if (typeName?.Equals("monsterinfo") == true)
{
Card<HealthInfo> deseerialized = jObject.ToObject<Card<HealthInfo>>();
return new Card<Info>(deseerialized.Name, deseerialized.Health, deseerialized.Info);
}
if (typeName?.Equals("spellinfo") == true)
{
string json = jObject.ToString();
Card<SpellInfo> deseerialized = jObject.ToObject<Card<SpellInfo>>();
return new Card<Info>(deseerialized.Name, deseerialized.Health, deseerialized.Info);
}
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And you should execute:
List<Card<Info>> list = JsonConvert.DeserializeObject<List<Card<Info>>>(jsonText, new InfoConverter());

How to serialize property as though it was a base class?

I have a object like this:
[JsonObject]
public class Thing<T> where T: class
{
[JsonProperty()]
T Data { get; set; }
[JsonProperty("error")]
public int ErrorCode { get; set; }
}
Is there a way to make it serialize it so that it would give something like this, when T is a class with a single value property for example:
{ value: "content", error: 1 }
instead of this:
{ data: { value: "content" }, error: 1 }
I sadly cannot derived from the data class.
You would need a custom converter for example:
[JsonConverter(typeof(HasValueConverter))]
public class HasValue
{
public HasValue(string value)
{
this.Value = value;
}
public string Value { get; set; }
}
[JsonObject]
public class Thing<T> where T: class
{
[JsonProperty("value")]
public T Data { get; set; }
[JsonProperty("error")]
public int ErrorCode { get; set; }
}
class HasValueConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(HasValue);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return new HasValue(JToken.Load(reader).Value<string>());
}
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
[Test]
public void Deserializes()
{
var json = "{ value: \"content\", error: 1 }";
var thing = JsonConvert.DeserializeObject<Thing<HasValue>>(json);
Assert.AreEqual("content", thing.Data.Value);
}

How to use Json.NET for model binding in mvc 5

I have the same issue as this guy posting single item in json array to .net object.
To summarize, I am sending JSON data to an action and getting null when there is only one item passed. But it works when there is more than one item.
I know this has to do with how the JSON string is constructed (list of object vs. single object) but I have no choice; I am using a limited jquery json library (that's another story)
JSON string example with a list of fields
{
"formtemplate":{
"fields":{
"field":[
{
"_class":"form-control text-input",
"_label":"Text Field",
"_name":"text-1467980984393",
"_type":"text",
"_subtype":"text"
},
{
"_class":"form-control text-input",
"_label":"Text Field",
"_name":"text-1467980999418",
"_type":"text",
"_subtype":"text"
}
]
}
}
}
JSON string example with only one field item
{
"formtemplate":{
"fields":{
"field":{
"_class":"form-control text-input",
"_label":"Text Field",
"_name":"text-1467980984393",
"_type":"text",
"_subtype":"text"
}
}
}
}
Model
public class Fields
{
public List<Field> field { get; set; }
}
public class Formtemplate
{
public Fields fields { get; set; }
}
public class CustomFields
{
public Formtemplate formtemplate { get; set; }
}
public class Field
{
public string _label { get; set; }
public string _name { get; set; }
public string _type { get; set; }
public List<Option> option { get; set; }
}
Action
[HttpPost]
public JsonResult saveCustomfields(CustomFields customfields)
{
}
I followed the advice on the link provided but it didn't seem to work, here is what I did
public class Fields
{
private List<Field> field;
public object Numbers
{
get { return field; }
set
{
if (value.GetType() == typeof(Field))
{
field = new List<Field> { (Field)value };
}
else if (value.GetType() == typeof(List<Field>))
{
field = (List<Field>)value;
}
}
}
}
In the action I get both of "Numbers" and "field" as null when there is one item or more. The solution is logical but it didn't work for me.
Update
I researched a bit more and implemented a custom converter from this link How to handle both a single item and an array for the same property using JSON.net. But when I debug; it seems like SingleOrArrayConverter is not even called
public class Fields
{
[JsonProperty("field")]
[JsonConverter(typeof(SingleOrArrayConverter<Field>))]
public List<Field> field { get; set; }
}
public class Formtemplate
{
[JsonProperty("fields")]
public Fields fields { get; set; }
}
public class CustomFields
{
[JsonProperty("formtemplate")]
public Formtemplate formtemplate { get; set; }
}
public class SingleOrArrayConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(List<T>));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Array)
{
return token.ToObject<List<T>>();
}
return new List<T> { token.ToObject<T>() };
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
But still no success, does it have to do with my action?

Deserialize nested ICollection<BaseType> in Asp.Net Web API 2 controller

I have a Web Api Controller like this one :
public IHttpActionResult Create(PaymentDTO Payment)
My DTOs are:
public class PaymentDTO
{
public int Id { get; set; }
public string type { get; set; }
public IEnumerable<TransactionDTO> Transactions { get; set; }
}
public class TransactionDTO
{
public int Id { get; set; }
public string Description { get; set; }
public string CreateTime { get; set; }
public string UpdateTime { get; set; }
}
public class SaleDTO : TransactionDTO
{
public string Total { get; set; }
public string Currency{ get; set; }
}
public class OrderDTO : TransactionDTO
{
public string State {get;set;}
}
I receive the following JSON formatted data :
{
"Type": "sale",
"Id": 101,
"transactions": [
{
"Total": "30.50",
"Currency": "USD",
"Description": "transaction description"
}
]
}
I want JSON.net to instantiate either a IEnumerable<SaleDTO> or IEnumerable<OrderDTO> based on the Type Property.
I could've used a custom type converter, but only if Type property was in TransactionDTO. But I want the Type property to be in the parent object (PaymentDTO)
Thank you in advance for your help.
You can do this with a custom JsonConverter on the PaymentDTO class:
public class PaymentDTOConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(PaymentDTO).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var obj = JObject.Load(reader);
var payment = (PaymentDTO)existingValue ?? new PaymentDTO();
// Extract the transactions.
var transactions = obj.Property("transactions") ?? obj.Property("Transactions");
if (transactions != null)
transactions.Remove();
// Populate the remaining regular properties.
using (var subReader = obj.CreateReader())
serializer.Populate(subReader, payment);
if (transactions != null)
{
// Deserialize the transactions list.
var type = PaymentDTO.GetTransactionDTOType(payment.type) ?? typeof(TransactionDTO);
using (var subReader = transactions.Value.CreateReader())
// Here we are taking advantage of array covariance.
payment.Transactions = (IEnumerable<TransactionDTO>)serializer.Deserialize(subReader, type.MakeArrayType());
}
return payment;
}
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Then apply it to your PaymentDTO class as follows:
[JsonConverter(typeof(PaymentDTOConverter))]
public class PaymentDTO
{
static Dictionary<string, Type> namesToTransactions;
static Dictionary<Type, string> transactionsToNames = new Dictionary<Type, string>
{
{ typeof(SaleDTO), "sale" },
{ typeof(OrderDTO), "order" },
};
static PaymentDTO()
{
namesToTransactions = transactionsToNames.ToDictionary(p => p.Value, p => p.Key);
}
public static string GetTransactionDTOTypeName<TTransactionDTO>() where TTransactionDTO : TransactionDTO
{
string name;
if (transactionsToNames.TryGetValue(typeof(TTransactionDTO), out name))
return name;
return null;
}
public static Type GetTransactionDTOType(string name)
{
Type type;
if (namesToTransactions.TryGetValue(name, out type))
return type;
return null;
}
public int Id { get; set; }
public string type { get; set; }
[JsonProperty("transactions")]
public IEnumerable<TransactionDTO> Transactions { get; set; }
}

Can I make Raven DB serialize an object as it would a string, if i have created an implicit type conversion operator?

I have a class that looks something like this:
public class MyClass
{
string _value;
public static implicit operator MyClass (string value)
{
return new MyClass(value);
}
MyClass(string value)
{
// Do something...
_value = value;
}
public override string ToString()
{
// Do something...
return _value;
}
}
Hence, I can use the class like this:
MyClass a = "Hello!";
But in Raven DB it will just be stored like
"SomeProperty": {}
since it has no public properties. And it is quite useless.
To solve this I would make the _value private member a public property instead, like this:
public string Value { get; set; }
and Raven DB will store
"SomeProperty": { "Value": "Hello!" }
and it will be deserializable.
But I don't want this public property. Can I somehow make Raven DB serialize and deserialize the class as was it would a string? Like:
"SomeProperty": "Hello!"
Hi I know this is old but I thought I would add some additions to Ayendes' reply to help people who like me had the same issue and spent hours looking on forums for an answer (of which there were a few but none had any example that you could follow), it's not hard to figure this out but with an example I could have solved this in 10 minutes as opposed to spending a few hours.
My problems was that we have custom value type structs in our application the example I will use is EmailAddress. Unfortunately in Ravendb we could not run queries against these types without defining a custom serialiser.
Our Value Type looked Like this:
[DataContract(Namespace = DataContractNamespaces.ValueTypes)]
public struct EmailAddress : IEquatable<EmailAddress>
{
private const char At = '#';
public EmailAddress(string value) : this()
{
if (value == null)
{
throw new ArgumentNullException("value");
}
this.Value = value;
}
public bool IsWellFormed
{
get
{
return Regex.IsMatch(this.Value, #"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*");
}
}
public string Domain
{
get
{
return this.Value.Split(At)[1];
}
}
[DataMember(Name = "Value")]
private string Value { get; set; }
public static bool operator ==(EmailAddress left, EmailAddress right)
{
return left.Equals(right);
}
public static bool operator !=(EmailAddress left, EmailAddress right)
{
return !left.Equals(right);
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
return this.Equals(new EmailAddress(obj.ToString()));
}
public override int GetHashCode()
{
return this.Value.GetHashCode();
}
public override string ToString()
{
return this.Value;
}
public bool Equals(EmailAddress other)
{
return other != null && this.Value.Equals(other.ToString(), StringComparison.OrdinalIgnoreCase);
}
}
The type of document we wanted to save and query would look something like this
public class Customer
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public EmailAddress Email { get; set; }
}
The custom serialiser to store our email as a raw string and then convert it back to its value type on retrieval looked like this:
public class EmailConverterTest : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(EmailAddress);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
EmailAddress actualAddress = new EmailAddress(reader.Value.ToString());
return actualAddress;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
EmailAddress actualAddress = (EmailAddress)value;
string stringEmail = actualAddress.ToString();
writer.WriteValue(stringEmail);
}
}
Finally I wired it up and was able to query everything as follows:
public static void serializercustom(Newtonsoft.Json.JsonSerializer serialiser)
{
serialiser.Converters.Add(new EmailConverterTest());
}
public static void TestCustomer()
{
using (var documentStore = new DefaultDocumentStore())
{
documentStore.ConnectionStringName = Properties.Settings.Default.SandBoxConnection;
documentStore.Initialize();
documentStore.Conventions.CustomizeJsonSerializer = new Action<Newtonsoft.Json.JsonSerializer>(serializercustom);
var customer = new Customer
{
Id = Guid.NewGuid(),
FirstName = "TestFirstName",
LastName = "TestLastName",
Email = new EmailAddress("testemail#gmail.com")
};
// Save and retrieve the data
using (var session = documentStore.OpenSession())
{
session.Store(customer);
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
var addressToQuery = customer.Email;
var result = session.Query<Customer>(typeof(CustomerEmailIndex).Name).Customize(p => p.WaitForNonStaleResults()).Where(p => p.Email == addressToQuery);
Console.WriteLine("Number of Results {0}", result.Count()); // This always seems to return the matching document
}
}
}
You can write a JsonConverter and teach RavenDB how you want to store the data.
After you write the converter, register it in the store.Conventions.CustomizeSerializer event.

Categories

Resources