patterncsharpMinor
NewtonSoft Json.Net serialiser
Viewed 0 times
newtonsoftserialisernetjson
Problem
I'm just starting to develop more in C# after being mainly a VB.NET developer and was looking for someone to critique my implementation of a NewtonSoft Json.Net serialiser.
Can you provide some feedback on the following points:
Interface
Implementation
```
using System;
using System.Threading.Tasks;
using Helper.Core.Compression;
using Helper.Core.Logging;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
namespace Helper.Core.Serialisation
{
Can you provide some feedback on the following points:
- Is this a good way to build the class (using Unity)?
- Is it acceptable to be throwing an exception from the constructor?
- Is the Async/Await implementation correct?
Interface
using System.Threading.Tasks;
namespace Helper.Core.Serialisation
{
public interface ISerialiser
{
///
/// Serialise the passed in object with the Json.Net serialiser
///
/// Generic type of the serialised object
/// The object to be serialised
/// A serialised Json string
Task SerialiseAsync(T serialseObject);
///
/// Serialise the passed in object with the Json.Net serialiser and compress the string using the IStreamCompression implementation
///
/// Generic type of the serialised object
/// The object to be serialised
/// A compressed byte array of the serialised object
Task SerailseAndCompressAsync(T serialseObject);
///
/// Deserialise the Json string into the generic object
///
/// Generic type of the serialised object
/// The object to be serialised
/// A deserialsied object of type T
Task DeserialiseAsync(string serialsedString);
///
/// Uncompress and deserialise the Json string into the generic object
///
/// Generic type of the serialised object
/// The object to be serialised
/// An uncompressed & deserialsied object of type T
Task DeserialseAndUnCompressAsync(byte[] serialed);
}
}Implementation
```
using System;
using System.Threading.Tasks;
using Helper.Core.Compression;
using Helper.Core.Logging;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
namespace Helper.Core.Serialisation
{
Solution
Code looks good, I like this IoC style.
3 points to your consideration:
(Oops didn't address your actual questions)
3 points to your consideration:
- You should
catchanAggregateExceptionoverawait.
- I wouldn't bother passing a
loggerto aserializer- that's none of his business. Let the serializerthrowif he's not happy.
- Fix some typo in names and messages ("Deserialse" and so).
- I somewhat doubt the whole concept of async serialization. I take serialized data to be an object snapshot in a known 'time point'. But if it's useful for you go for it.
(Oops didn't address your actual questions)
- Yes I think it's great.
- Sure. Lacking meaningful 'default object', you don't have many alternatives.
- This is probably the main issue here, and the hardest to answer. I have some doubts about returning a non-cancellable
Task. I suspect if the object to be serialized has changed completely, the user may want to cancel the serialization.
Context
StackExchange Code Review Q#18401, answer score: 7
Revisions (0)
No revisions yet.