I have an event that gives me a JSON string:
...
public delegate void DataReceivedHandler(string jsonString);
...
public event DataReceivedHandler OnDataReceived = null;
...
if(OnDataReceived != null)
{
OnDataReceived(jsonString);
}
...
That JSON string could be one of 3 different complex objects: LogOnMessage, LogOffMessage, or DataRequest. Each message has a unique set of fields and properties.
How can I determine which object type the JSON string resolves to?
I know I can write a method that iterates through the JProperty.Name of the JObject and find a match by iterating through my collection of objects and their meta-data, but my gut tells me this is a common challenge to be solved so it must be built in to Newtonsoft JSON .NET somewhere that I simply am overlooking or not understanding. It's probably better and faster than my solution would be too...
I was finally able to detect object type by using JObjects and JsonSchemas.
Steps I Took:
Added a Schema property to my message objects that exposed a _schema field. The first time the property is called, it populates _schema with the return value of JsonSchemaGenerator.Generate(object o).
Convert the JSON string into a JObject via the JObject.Parse() static method.
There is an extension method in Newtonsoft.Json.Schema.Extensions that can compare a JObject to a JsonSchema and determine if they match.
Please note: the methods above have been moved to a separate Newtonsoft.Schema library. So my recommendation is to utilize the latest and greatest library.
private Newtonsoft.Json.Schema.JsonSchema _schema;
public static Newtonsoft.Json.Schema.JsonSchema Schema
{
get
{
if (_schema == null)
{
Newtonsoft.Json.Schema.JsonSchemaGenerator generator = new Newtonsoft.Json.Schema.JsonSchemaGenerator();
_schema = generator.Generate(typeof(DataResponse));
}
return _schema;
}
}
...
Newtonsoft.Json.Linq.JObject message = Newtonsoft.Json.Linq.JObject.Parse(json);
if(Newtonsoft.Json.Schema.Extensions.IsValid(message, DataResponse.Schema))
{...}
else if (Newtonsoft.Json.Schema.Extensions.IsValid(message, ServerStatus.Schema))
{...}
...
Related
I've been working on a C# program that serializes and deserializes some data in JSON format: however, now I have to radically change the type of a pre-existing property, from string to Dictionary(string,string[]).
Is there a way to make it so that, if the program tries to deserialize a document where the field is still a string, it performs some operations to convert it to a Dictionary? Or will the old documents become completely unusable?
For reference, the functions I've been using for reading/writing are the following:
Serialization:
JsonConvert.SerializeObject(this, Formatting.Indented);
Deserialization:
JsonConvert.DeserializeObject<T>(content);
EDIT:
As a simplified version of what I have to do, I have a class with a structure similar to this:
class ExampleClass // V1
{
public string CustomProperty { get; set; }
}
And I need it, after the update, to look like this:
class ExampleClass // V2
{
public Dictionary<string, string[]> CustomProperty { get; set; }
}
What I want is, if I try to deserialize a JSON with the V1 version of the class, instead of raising an exception, that string value will be used to populate the Dictionary (the basic plan is to put a default key and for the array the V1 value split by a default character: I doubt the exact details of the conversion would make much of a difference though). Afterwards, if I try to serialize the resulting object, it will only show the Dictionary, and not the original string.
You could keep two versions of the class you are deserializing into, or you could use a custom JsonConverter and JsonConverterAttribute to deal with the two different ways of deserializing your data.
Either way you choose, you will need some way of identifying what type of document or data type you have, and then you will need an if statement to switch to the appropriate deserialization method. The easiest way of determining this really depends on your situation.
There are some ways on how to handle such a contract change
Keep an old string property, create a new one with another name for the dictionary. Implement the lazy population for the dictionary property
Change the type of the existing property but create a new one and provide [JsonPropertyAttribute] with an old name. Implement the lazy population for the dictionary property
Use JsonConverterAttribute
Have multiple versions of the class (my preferred way)
In the end, I've managed to solve the issue by explicitly writing the deserialization function, like this:
public static ExampleClass DeserializeData(string content)
{
try
{
return JsonConvert.DeserializeObject<ExampleClass>(content);
}
catch
{
// If it's in this section, it's trying to deconvert outdated JSON
Dictionary<string, dynamic> objectMap = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(content);
Dictionary<string, PropertyInfo> propertyMap = new Dictionary<string, PropertyInfo>();
ExampleClass data = new ExampleClass();
foreach (PropertyInfo property in typeof(ExampleClass).GetProperties())
propertyMap.Add(property.Name, property);
foreach (KeyValuePair<string, dynamic> kvp in objectMap)
{
Type propertyType = propertyMap[kvp.Key].PropertyType;
if (kvp.Key != "CustomProperty")
{
if (kvp.Value is Array || kvp.Value is JArray)
{
Type recipientType = propertyType.GetElementType();
Type listType = typeof(List<>).MakeGenericType(recipientType);
System.Collections.IList itemList = (System.Collections.IList)Activator.CreateInstance(listType);
foreach (var item in (kvp.Value as JArray))
itemList.Add(Convert.ChangeType(item, recipientType));
Array itemArray = Array.CreateInstance(recipientType, itemList.Count);
itemList.CopyTo(itemArray, 0);
propertyMap[kvp.Key].SetValue(data, itemArray);
}
else if (Nullable.GetUnderlyingType(propertyType) != null)
{
propertyMap[kvp.Key].SetValue(data, Convert.ChangeType(kvp.Value, Nullable.GetUnderlyingType(propertyType)));
}
else
propertyMap[kvp.Key].SetValue(data, Convert.ChangeType(kvp.Value, propertyType));
}
else
{
if (kvp.Value is string)
{
propertyMap[kvp.Key].SetValue(data, new Dictionary<string, string[]>() { { "DEFAULT", (kvp.Value as string).Split('&') } });
}
else
propertyMap[kvp.Key].SetValue(data, kvp.Value);
}
}
return data;
}
}
With this semi-generic function I can replicate the normal DeserializeObject, and have it elaborate the value depending on its type when it reaches a specific property: not only that, but by having it attempt to use DeserializeObject first, it ensures the fastest alternative is used on properly formatted JSON. It's probably not the most elegant solution, but it works, so I'll take it.
Thank you all for your answers, they were a great help in reaching this solution! Hope this helps others with the same issue as well!
How come when I send a message to RabbitMQ through rebus that it is adding extra object data like below, primarily the $type.
{"$type":"ThreeSquared.VTGPAM.Objects.Wagon, ThreeSquared.VTGPAM.Objects","WagonId":"a98a06ab-33b9-4a11-9de2-df0b8787b713","WamosId":12324,"Description":"test","YearBuilt":1982,"Token":"3cce443c-249f-4fd2-9882-5830fb308b6b"}
We have a client that will just be using the Java RabbitMQ library with no rebus. This approach I believe we just send the JSON without the type declarations. This therefore doesn't work when I try and read in simple JSON object. How can we make it work so that it doesn't define the $type in the message?
It's simply because Rebus by default uses Newtonsoft JSON.NET with TypeNameHandling.All, which means that the $type field is included in every serialized object containing the full .NET type name of the type serialized.
The benefit is that you can serialize almost anything, even though it may contain instances referenced by (possibly abstract) supertypes, and even by interfaces.
E.g. this command message type
public class ProcessFile
{
public ProcessFile(string filePath, IEnumerable<IFileProcessingTask> tasks)
{
FilePath = filePath;
Tasks = tasks;
}
public string FilePath { get; }
public IReadOnlyCollection<IFileProcessingTask> Tasks { get; }
}
could contain arbitrary implementations of IFileProcessingTask, e.g. something like
public class GZipFileProcessingTask : IFileProcessingTask
{
// ...
}
as long as the recipient can find the type by looking it up via the value of the $type field.
If you want to process this type of message on another platform, you can simply make it ignore the $type field of every object. This may be easy/hard/impossible, depending on how flexible your JSON serializer is.
Another option is to simply replace Rebus' serializer with your own implementation by doing this
Configure.With(...)
.(...)
.Serialization(s => s.UseCustomJsonSerialization())
.Start();
where UseCustomJsonSerialization is an extension method that you implement like this:
public static class RebusConfigEx
{
public static void UseCustomJsonSerialization(this StandardConfigurer<ISerializer> configurer)
{
configurer.Register(c => new YourCustomJsonSerializer());
}
}
and then all there is left to do is to create the class YourCustomJsonSerializer as an implementation of ISerializer.
I am trying to deserialize JSON. My root object has a single property "en.pickthall". I am using dynamic type for reading my JSON. I thought I could just do away with "." in the property since its a local JSON file but then there must be some way to access such a property
var result = App_Code.FileIOHelper.ReadFromDefaultFile("ms-appx:///Assets/en.pickthall.json");
dynamic stuff = JsonConvert.DeserializeObject(result);
foreach(var x in stuff.(en.pickthall)) //Tried this intellisense didn't like it
{
}
You could create a root class to deserialize into and use JsonProperty
public class Root
{
// Use the proper type instead of object
[JsonProperty(PropertyName = "en.pickthall")]
public IEnumerable<object> EnPickthall { get; set; }
public Root() { }
}
Used as follows
Root stuff = JsonConvert.DeserializeObject<Root>(result);
foreach(var x in stuff.EnPickthall)
{
}
You could serialize not to dynamic but to JObject and then access your property via
JObject stuff = JsonConvert.DeserializeObject<JObject>(Jsonstring);
var x = stuff.Value<String>("my.property")
C# doesn't have any way of quoting identifiers. If it's not a valid identifier, your only option is reflection.
However, it's possible the object returned by your JSON deserializer changed the identifiers to make them useable in C# - you might want to enumerate all the properties to check if that is the case. A dynamic object with indexers might also be a solution (allowing e.g. stuff["en.pickthall"]).
Another alternative is to change the way the serializer maps properties. For example, Newtonsoft.Jsoft allows you to customize this using a IContractResolver. It's quite easy to replace the . for something more C#-sane in this way.
I know you said you were using a dynamic type for your JSON deserialization, but I just wanted to point out that there is a .NET RESTful client out there that supports this with static model definitions too. For you or for anyone else who happens upon this response when searching for an answer to their problems with dots in property names in C# REST calls.
As of the newly released RestSharp 106.1.0 (and I do mean this version because this support was just added), it can handle renaming properties with a dot in their name via the DeserializeAs attribute. An example being when I call the ElasticSearch API for a _cat call with the following model:
public class CatResponse
{
public string index { get; set; }
...
[DeserializeAs(Name = "docs.count")]
public string docscount { get; set; }
}
And actually get back the docs.count property deserialized into docscount now:
var resource = $"_cat/indices/{indexPattern}?format=json&pretty=true";
var request = new RestRequest(resource, Method.GET);
var response = client.Execute<List<CatResponse>>(request);
This support is out of the box and doesn't need to use the Newtonsoft.Json.JsonSerializer which I have also heard is a possible solution to this problem but which I couldn't get to work.
With a dynamic object and NewtonSoft.Json:
dynamic json = JValue.Parse(result);
int Opens = Convert.ToInt32(json.opens);
int Clicks = Convert.ToInt32(json.clicks);
string State = json.state;
I'm doing some work for a JIRA REST lib. It uses JSON for communication.
Part of the objects I receive in JSON format are know. The others can be of several different formats. So what I have done is created an object with properties that hide a dictionary
public IssueFields Fields { get; set; }
public IssueType IssueType
{
get { return Fields.IssueType; }
set { Fields.IssueType= value; }
}
IssueFields
{
private Dictionary<string, Field> _fields = new Dictionary<string, Field>();
public string IssueType
{
get { return GetFieldByName<IssueType>(IssueTypeFieldName); }
set { SetFieldByName(IssueTypeFieldName, value); }
}
public T GetFieldByName<T>(string fieldName) where T : class
{
return _fields.ContainsKey(fieldName) ? _fields[fieldName] as T: null;
}
public void SetFieldByName(string fieldName, Field field)
{
if (_fields.ContainsKey(fieldName))
{
_fields[fieldName] = field;
}
else
{
_fields.Add(fieldName, field);
}
}
So I have a bunch of classes like that. I can deserialize into them no problem since JavaScriptSerializer (or any other JSON deserializer) just takes the values and puts them into properties of the objects objects automatically. However there are a bunch of unknown fields all starting with "customField_XXXXX".
What I am currently doing is overriding the JavaScriptSerializer and manually putting EVERYTHING into place. Another idea I got from someone else's code was to re-serialize the dictionary inside the JavaScriptConverter override, then deserialize it into the issue, then put everything else in manually from the dictionary, but that adds a lot of overhead and certainly will raise more than a few eyebrows.
public class MyConverter : JavaScriptConverter
{
private static JavaScriptSerializer _javaScriptSerializer;
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
string json = _javaScriptSerializer.Serialize(dictionary);
Issue issue = _javaScriptSerializer.Deserialize<Issue>(json);
// Then add the rest of my objects manually
Is there any way to get the object back with whatever it could serialize AND the dictionary so I can fill in anything it couldn't on my own? I just haven't been able to find anything other than this method....
Thanks!
Json.Net has a built-in "Extension Data" feature that might solve your problem. After deserializing known properties into your class, it automatically handles adding any "extra" object properties to a dictionary. You can read about it on the author's blog. Also check out this answer for some example code.
I'm sure this question has been asked over and over again, but for some reason, I still can't manage to get this to work.
I want to deserialize a JSON object that contains a single member; a string array:
[{"idTercero":"cod_Tercero"}]
This is the class that I'm trying to deserialize into:
[DataContract]
public class rptaOk
{
[DataMember]
public string idTercero { get; set; }
public rptaOk() { }
public rptaOk(string idTercero)
{
this.idTercero = idTercero;
}
}
This is the method that I try to deserialize:
public T Deserialise<T>(string json)
{
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
T result = (T)deserializer.ReadObject(stream);
return result;
}
}
And so I try to fill the object:
rptaOk deserializedRpta = deserializarOk(rpta);
But for some reason, this returns ""
MessageBox.Show(deserializedRpta.idTercero);
Without any dependencies outside of the .net framework, you could do it this way
[DataContract(Name="rptaOk")]
public class RptaOk
{
[DataMember(Name="idTercero")]
public string IdTercero { get; set; }
}
[CollectionDataContract(Name="rptaOkList")]
public class RptaOkList : List<RptaOk>{}
var stream = new StreamReader(yourJsonObjectInStreamFormat);
var serializer = new DataContractSerializer(typeof(RptaOkList));
var result = (RptOkList) serializer.ReadObject(stream);
I don't know if your're wiling to change the library that you're using, but I use library "Newtonsoft.Json" to desserialize JSON objects, it's pretty easy to use
[HttpPost]
public void AddSell(string sellList)
{
var sellList = JsonConvert.DeserializeObject<List<Sell>>(sellListJson);
BD.SaveSellList(sellList);
}
As you can see you can deserialize a whole json object list to a List<> fo the type "Sell", an object that i've created... And, of course, you can do that to an array too. I don't know the correct syntax for this, but you can convert this list to an array afterwards
Hope this helps
I think you're making this a lot more difficult than it needs to be. Firstly, your sample json and the class you're trying to deserialize into do not have an array of strings. They have a single property of type string. Secondly, why are you using this class DataContractJsonSerializer? You're not doing anything with it that you can't get from a simple call to json.NET's generic deserialization method. I would remove all of your code except the class definition and replace it with this simple one liner;
rptaOk[] myInstances = JsonConvert.DeserializeObject<rptaOk>(jsonString);
Also, no matter what the structure of your json is, if you have a class to correctly model it that method will correctly deserialize it. If you want to enforce some kind of contract I recommend using json schemas which json.NET also supports. If you use a schema it enforces a rigid contract, if you attempt to deserialize into an object there is something of an implicit contract. I don't know every scenario which will cause it to throw, but if your json is too far from the class definition it will. If your class has properties that don't appear in the json I believe they will just get initialized with the default values for that type.
EDIT: I just noticed your json is actually an array of objects. So you simply need to make the lhs of that assignment an array or rptaOk objects, rather than a singleton.