I am converting code that was written using NewtonSoft.JsonNet. This is actually a custom Json Media Type Formatter. I have to change it because Json.Net has proven that its performance is very poor under load. Many comparison on the Internet is also proving this.
Anyway, I have a base type called CatalogueItem. Three types are derived from this type and are called ContainerItem, SectionItem, and RefresherItem. Based on a property in the Json object which is called itemType we decide which sub-class must be instantiated.
var type = (string)jsonObject.Property("itemType");
switch (type)
{
case "Container":
return new ContainerItem();
case "Section":
return new SectionItem();
case "Refresher":
return new RefresherItem();
}
We used to do this with creating a custom CustomCreationConverter, and adding it to Serializer.Converters collection of Json.Net.Serializer. Trying to get rid of Json.Net, I am using ServiceStack.Text, but I don't know how can I control the type that is being generated using it. Can anyone please help me with this?
p.s. I found this post on StackOverflow in which similar issue has been answered. However, I get the json from a third-party web service so I cannot include type names in it. Plus, I cannot use the generic version of JsConfig because MediaTypeFormatter does not have any generic methods.
I recommend avoiding trying to coerce your JSON Serializer to your models and just use DTO's that map 1:1 to the wire format than use plain C# to map the typed DTO's to your desired domain models.
With that said, depending on what the JSON and DTO's look like you may be able to use one of:
JsConfig<CatalogueItem>.RawDeserializeFn
JsConfig<CatalogueItem>.DeSerializeFn
JsConfig<CatalogueItem>.OnDeserializedFn
Otherwise you can parse JSON dynamically using JsonObject, here's an example.
Related
Recently I need to parse JSON object like this:
{
"method":"someMehtod",
"content":{"oneObject":"can be a very complicated object"},
"somotherthing":"someOtherValue"
}
into C# with Newtonsoft JSON.NET.
I know it is very common to create a new class or classes for the purpose but this is highly unwelcome choice to my current situation since it is considered unnecessary to my superior (and I don't want there to be too many classes like this either). Is there anything that resembles JAVA's JSONObject class in C# world I can use to query multiple level json without making new class?
You can use JSON.Net's JObject. Here's an example:
var jsonString = "{\"method\":\"someMehtod\", \"content\":{\"oneObject\":\"can be a very complicated object\"}, \"somotherthing\":\"someOtherValue\"}";
var obj = JsonConvert.DeserializeObject<JObject>(jsonString);
Console.WriteLine(obj["content"]["oneObject"]); // will print "can be a very complicated object"
dotnet fiddle demo available here
Take a look at the JsonConvert.DeserializeAnonymousType method. Define an anonymous type inline to define the expected structure and pass it in as a parameter. No new classes required, but I suspect if your superior is against creating new classes (really??) then they'll be against anonymous types too...
https://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm
I am trying to serialize a JSON object to send it from the Controller to the View. Despite reading plenty of similar questions, I did not find a solution that works well.
In my case, I have a List<MyType> object, where is a pre-compiled class. But when I tried to serialize the data by means of:
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyType));
I got the following error:
Additional information: Type MyType cannot be
serialized. Consider marking it with the DataContractAttribute
attribute, and marking all of its members you want serialized with the
DataMemberAttribute attribute.
However, since it is a pre-compiled class, I cannot mark the members. How can I solve this?
The DataContractJsonSerializer uses opt-in semantics, which means that you must mark the class(es) to be serialized with a [DataContract] attribute, and mark all members of those classes that you want serialized with [DataMember]. If you cannot change the classes (because they are precompiled or you don't otherwise control the source code), then there's not much you can do with this serializer short of copying the data into another class that can be serialized. But, with ASP.NET MVC you shouldn't really be using DataContractJsonSerializer anyway-- this serializer was created for WCF and is not very flexible or user-friendly, IMO.
The ASP.NET MVC framework has a Json method built into the base controller, which uses the JavaScriptSerializer behind the scenes to serialize your model objects. This serializer does not require marking up classes with attributes, and because it is baked-in you don't have to put any special serialization code into your controller methods to use it. Just change your method to return JsonResult instead of ActionResult, then pass your object to the Json method as a last order of business.
For example:
[HttpGet]
public JsonResult GetItem(int id)
{
PrecompiledClass pc = RetrieveObjectFromDatabaseOrWhatever(id);
return Json(pc, JsonRequestBehavior.AllowGet);
}
The JavaScriptSerializer does have some limitations that I won't go into here, which you probably won't hit in most normal circumstances. But if you do find that the JavaScriptSerializer does not suit your needs, you can switch to a robust third-party serializer like Json.Net. See ASP.NET MVC and Json.NET and Using JSON.NET as the default JSON serializer in ASP.NET MVC - is it possible? to learn more about that option.
I would not recommend using a precompiled class as your view model. Separation of concerns and whatnot.
Create a new class, MyTypeViewModel, which will have only the properties that the UI needs to know about, and map the properties from MyType to MyTypeViewModel. Then return like this in your controller method if you're returning this result as part of an API call:
return Json(myTypeViewModel, JsonRequestBehavior.AllowGet);
or return View(myTypeViewModel) if you want to render an entire view.
I have reference to several classes defined in an external library that contain a TypeConverter attribute. Due to this attribute, Json.Net, and consequently Nest, is incorrectly serializing these objects. The correct way to serialize these objects is to ignore the TypeConverter attribute and make use of the object properties. In searching Stack Overflow I have found a method that successfully does this.
Unfortunately, when I apply this method to Nest in the following way, the resultant objects do not contain the necessary _type, _index and _id fields.
var settings = new ConnectionSettings(_Uri);
// Tell Json.Net to ignore custom TypeConverters
settings.SetJsonSerializerSettingsModifier(m => m.ContractResolver = new SerializableContractResolver());
var client = new ElasticClient(settings);
Since the resultant JSON does not contain the critical fields, all index requests fail. Is there a way to have Nest ignore the TypeConverter attributes while still ensuring the objects contain the necessary additional fields for Elasticsearch requests?
When you are passing a contractresolver that ignores TypeConverters you are applying a bazooka to kill a fly in my opinion.
We also rely heavily on typeconverters to write our objects correctly.
If the classes in your external library do not quite represent the way they should look in your elasticsearch index I'd recommend create a specific POCO for NEST that does. AutoMapper is a great way to convert instances of your external library to instances of indexable object.
#Martijn Laarman's suggestion is probably the correct solution. However, I decided to unblock myself with the following hack:
I continue to use the method defined here. I then correctly serialize the external object into JSON. I deserialize the object into a dynamic type. Finally I pass this object to Nest.
// Strip away TypeConverter attributes -- super hacky
var json = JsonConvert.SerializeObject(item);
payload = JsonConvert.DeserializeObject<dynamic>(json);
descriptor.Index<Object>(op => op.Index(index).Type(type).Id(id).Document(payload));
I'm wondering how to exclude/strip certain properties of given type(s) (or collections of those) from being serialized using Json.NET library?
I tried to write my own contract resolver (inheriting from DefaultContractResolver) with no luck.
I know that I could be done using DataAnnotations, decorating the excluded properties with ScriptIgnoreAttribute, but it's not applicable in my scenario. The objects serialized can be virtually anything, so I don't know which properties to exclude at design-time. I know only the types of properties that should not be serialized.
It looks like a rather simple task, but unfortunately I couldn't find a decent solution anywhere...
BTW - I'm not bound to Json.NET library - if it can easily be done with default/other .NET JSON serializers it'd be an equally good solution for me.
UPDATE
The properties has to be excluded before trying to serialize them. Why?
Basically, the types of objects I'm receiving and serializing can have dynamic properties of type inheriting from IDynamicMetaObjectProvider. I'm not going to describe all the details, but the DynamicMetaObject returned from GetMetaObject method of these objects doesn't have DynamicMetaObject.GetDynamicMemberNames method implemented (throws NotImplementedException...). Summarizing - the problem is those objects (I need to exclude) doesn't allow to enumerate their properties, what Json.NET serializer tries to do behind the scenes. I always end up with NotImplementedException being thrown.
I have tried both the WCF JSON serialization as well as the System.Web.Script.Serialization.JavaScriptSerializer. I have found if you want solid control of the serialization process and do not want to be bound by attributes and hacks to make things work, the JavaScriptSerializer is the way to go. It is included in the .NET stack and allows you to create and register JavaScriptConverter subclasses to perform custom serialization of types.
The only restriction I have found that may cause you a problem is that you cannot easily register a converter to convert all subclasses of Object (aka, one converter to rule them all). You really need to have knowledge of common base classes or preregister the set of types up front by scanning an assembly. However, property serialization is entirely left up to you, so you can decide using simple reflection which properties to serialize and how.
Plus, the default serialization is much much much better for JSON than the WCF approach. By default, all types are serializable without attributes, enums serialize by name, string-key dictionaries serialize as JSON objects, lists serialize as arrays, etc. But for obvious reasons, such as circular trees, even the default behavior needs assistance from time to time.
In my case, I was supporting a client-API that did not exactly match the server class structure, and we wanted a much simpler JSON syntax that was easy on the eyes, and the JavaScriptSerializer did the trick every time. Just let me know if you need some code samples to get started.
Create your own contract resolver, override the method that creates the properties for an object and then filter the results to only include those that you want.
Have you considered using the ShouldSerialize prefix property to exclude the property of your specific type at runtime?
public class Employee
{
public string Name { get; set; }
public Employee Manager { get; set; }
public bool ShouldSerializeManager()
{
return (Manager != this);
}
}
I am in the process of designing an itemization system for a game. I am creating a bunch of interfaces (IItem, IConsumable, IEquipable, IWeapon, etc...) to define what type of functionality is possible with items and then a number of classes (Weapon, Potion, etc...) the define the actual item types.
Now when saving out the player, the data is going to be stored as a JSON file (using the JSON.NET library) and will included the players inventory which will include these items. The issue is that when I try to deserialize the JSON file to an object when reading in the file, how will I be able to tell the JSON.NET library what type of class this is?
One thing I thought of and have not had time to try yet is that all my objects that get serialized into JSON have a DTO version of the object that is used in conjunction with the JSON.NET library. What I though about trying is maybe I could add in a property to the DTO called ClassType and then when reading the file into the application, I would first read the object in as an anonymous type. Then based on the ClassType property, I would convert it to the proper type. The 2 issues I have with this is that 1. it seems like a very ugly solution and 2. I am not even sure if that is possible.
(Copied from this question)
In cases here I have not had control over the incoming JSON (and so cannot ensure that it includes a $type property) I have written a custom converter that just allows you to explicitly specify the concrete type:
public class Model
{
[JsonConverter(typeof(ConcreteTypeConverter<Something>))]
public ISomething TheThing { get; set; }
}
This just uses the default serializer implementation from Json.Net whilst explicitly specifying the concrete type.
The source code and an overview are available on this blog post.
You should know the concrete type when you are serializing. So you can use TypeNameHandling of JSON.NET.
http://james.newtonking.com/archive/2010/08/13/json-net-3-5-release-8-3-5-final.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+jamesnewtonking+%28James+Newton-King%29