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.
Related
NodaTime - can I change default xml serialization for Instant?
Is there a way to change it so all instants would be serialized without milliseconds:
Instead of this:
<CreationDateTime>2021-06-27T09:52:18.1900643Z</CreationDateTime>
I'd want this:
<CreationDateTime>2021-06-27T09:52:18Z</CreationDateTime>
I'm wondering if there is a way to do that for all NodaTime.Instant properties, e.g. with some settings on the XmlSerializer we create?
No, I'm afraid not: the format is hard-coded, effectively. I would suggest either post-processing the XML, or potentially serializing using a separate string property which is purely for the sake of XML (so you'd mark it as obsolete, but serialize it in the way that you want, and it would proxy to an Instant property which is not obsolete but is ignored for XML serialization).
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 send json objects to DotNet Service in format
{"__type":"EntityItem#ru.test.com","name":"sample"}
And on Net service i got object EntityItem and all good.
But if __type will not first in list properties then it's get error parsing object. Next version JSON crashes
{"name":"sample","__type":"EntityItem#ru.test.com"}
Does exists solution how to fix it?
It's called "type hints". Here is link http://msdn.microsoft.com/en-us/library/bb412170.aspx
and qoute
Note that the type hint must appear first in the JSON representation. This is the only case where order of key/value pairs is important in JSON processing.
It's so sad.
I've encountered a problem where a small number of data objects stored using a BinaryFormatter are coming back with parameters missing (null/default).
I'd like to know if the missing items were saved as null, or if the objects that were serialized were changed from the versions in source control and then reverted before a code commit (eg int numDogs vs unsigned int dogCount).
The former would represent a serious bug in the data validation code ran before the serialization was done; while the latter is just junk data in a test DB and ignorable.
Since the BinaryFormatter is able to get everything else out when a member is changed, added, or removed I assume it's writing objects in a form similar to a key value store. Is there any way to get a human readable representation of it, without having to try and guess the exact details of the object that was serialized?
If you implement ISerializable on your objects, you can have a look at what's been serialized by trying to deserialize.
You will need to add a constructor with the same signature as ISerializable.GetObjectData - this is where deserialization occurs.
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.