I'm using the JsonConvert.SerializeObject inside a custom ActionFilterAttribute.
Inside the OnActionExecuting I'm serializing the filterContext.ActionParameters for logging purpose.
The problem is that some of the actions are receiving files (System.Web.HttpPostedFileWrapper) via a <from/> with enctype="multipart/form-data" so i'm getting the error
Timeouts are not supported on this stream.
What i want to do is ignore these kind of object which can't get serialize and could through errors.
I found out that i can ignore class Properties with [JsonIgnore] or [ScriptIgnore] attributes, but I'm looking for a more general solution becuase i don't want to place these attributes all over the place.
I also tried to research the JsonSerializerSettings object but didn't find something relevant to my case.
You can simply filter these parameters. somethink like:
var ToBeSerialized = filterContext.ActionParameters.Where(a=>a.Value.GetType()!=typeof(TheTypeYouWantToAvoid));
string result = JsonConvert.SerializeObject(ToBeSerialized);
Related
I have a set of objects that contain fields & properties that need to be inspectable in the output of serialization but not read back in when deserialized.
This is purely for debugging/confirmation purposes. We are creating hundreds of files and I want to spot check that serialization is occurring correctly by adding supplementary information. I do not want this supplementary information to be read in during deserialization - it's impossible to do so in fact.
I also need to do this with equal facility across different serialization formats, so we can assess which one is working best. I have a generic serialization approach where the desired format is passed in as an argument, so don't want anything too messy or intricate for each different format.
I've hunted around and found various things on related topics - mostly to do with the opposite: not writing certain fields during serialization. What's out there seems to be quite complicated and at times hacky.
Is it possible to serialize an object differently to deserializing it using Json.Net?
JsonConvert .NET Serialize/Deserialize Read Only
Serialize Property, but Do Not Deserialize Property in Json.Net
Also it appears any approach is inconsistent between serialization formats. i.e. unlike the [*Ignore] attributes, there are no [*SerializeOnly] attributes (where * = JSON, XML, YAML).
Is there an easy way to do this across these serialization formats? Is there a single family of attributes that can help? Or is it idiosyncratic and hacky in each case?
I have tested and applied this only to XML serialization, but it works for me:
When I want a property to be serialized, but not read back, I just declare an empty setter.
public String VersionOfApplicationThatHasWrittenThisFile
{
get
{
return "1.0";
}
set
{
// Leave empty
}
}
I'm using Microsoft.Web.Redis.RedisOutputCacheProvider for output caching in Redis. The task is to retrieve manually value from the Redis database and deserialize value for further processing the original HTML.
I tried StackExchange.Redis.IDatabase - can get value by key with StringGet() method, but the problem is that it's serialized. Any thoughts regarding above ?
That's because RedisOutputCacheProvider by default, serializes in a binary format provided by the BinaryFormatter class.
So you need to deserialize it in the same way OR use a custom serialization method on RedisOutputCacheProvider.
Check its configuration wiki with the instructions to use a custom serializer.
Hi I'm quite new to C# and I'm trying to make a text editor that saves and loads Plaintext formats. I've used NewtonSoft.Json NuGet package, but I'm getting an error. I've stated a string called textToLoad, which is set to a JsonConvert.DeserializeObject. Only thing is, it says it can't convert an object to a string! I tried toString(); but it still had the same error.
It is kind of hard without the code. The process of serializing and deserializing is pretty straight forward using Json.Net. So this is an example from their documentation:
YourType yourObject= new YourType();
yourObject.Property="something";
string output = JsonConvert.SerializeObject(yourObject);
//For some reason you want this to be string, but is the type you serialized in the first place
YourType textToLoad= JsonConvert.DeserializeObject<YourType>(output);
This outlines the basic works of serializing and deserializing. But we don't really know the details of your implementation.
Hope it helps.
You can't deserialize into a string like that. At simplest form you started with JSON in the form of:
{ value: "someString" }
If you want something out of it, you must deserialize and then get the value from it.
dynamic foo = JsonConvert.DeserializeObject<dynamic>(theJson);
var textToLoad = foo.value.ToString();
You must deserialize to something in order to inspect and get properties from it.
[Edit] - Perhaps I'm not understanding. But if you share code, I'll update my answer.
Is there a way in MVC3 to set what properties the Json function outputs?
ie. properties on my model have an attribute that tells the Json function not to output them.
It looks like the ScriptIgnoreAttribute will do what you want. Just decorate whatever property you don't want serialized with it.
Use anonymous method for that:
so instead of
return Json(it);
do
return Json(new {
it.Name,
CreatedAt = it.CreatedAt.ToString("D")
// And so on...
});
this way you explicitly publish (map) set of attributes to the web which ensures that only allowed properties can be accessed from JSON.
If you don't want to Repeat Yourself, you can use JSON.NET serializer with which you can customise how objects are serialised. (So you can create custom HideAttribute and take that into account).
With JSON.NET you will also need to write Controller.Json method replacement (SmartJson or so). But it should not be an issue I suppose.
I have incoming messages that I need to try and parse in my own objects structure. SOme of these are well formed JSON obejcts and some are just nonsense.
I use JsonConvert.DeserializeObject<MyObject>(incmoingString); to do this. This however sometimes gives me a exception when the incoming is total garbage. Other times I get a non-complete object structure when the incoming string is kind of OK - and finally it sometimes work.
I've wrapped the conversion in a try/catch and than manually validate that I've gotten the properties I need to the deserialized result.
Is there a better way to do this?
Json.NET supports JSON Schema. You could create a schema with all the required properties marked and validate incoming JSON against it before deserializing.