3
\$\begingroup\$

I wrote this class to one-line all my JSON serialization, and I'm curious of any input on it.

It's only responsible for serializing/deserializing any type to/from JSON.

The comments and code are pretty self explanatory, but if you want any extra explanation of it please feel free to comment.

Not a lot here, so comment on what you can.

/// <summary>
/// Provides methods for Serialization and Deserialization of JSON/JavaScript Object Notation documents.
/// </summary>
public class JsonSerialization
{
    /// <summary>
    /// Serializes an object to a JSON/JavaScript Object Notation string.
    /// </summary>
    /// <typeparam name="T">The type of the object to serialize.</typeparam>
    /// <param name="value">The object to serialize.</param>
    /// <param name="serializedJson">Filled with a string that is the JsonSerialized object.</param>
    /// <param name="throwErrors">If true, will throw errors. Otherwise, returns false on failures.</param>
    /// <returns>A boolean value indicating success.</returns>
    public static bool Serialize<T>(T value, ref string serializedJson, bool throwErrors = false)
    {
        if (value == null)
        {
            return false;
        }

#if DEBUG
#warning When in DEBUG Mode JavaScript Serialization Errors will be thrown regardless of throwErrors parameter.
            throwErrors = true;
#endif

        try
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            serializedJson = jss.Serialize(value);
            return true;
        }
        catch
        {
            if (throwErrors)
                throw;

            return false;
        }
    }

    /// <summary>
    /// Deserializes a JSON/JavaScript Object Notation string to an object.
    /// </summary>
    /// <typeparam name="T">The type of the object to serialize.</typeparam>
    /// <param name="value">The JSON string representing the serialized object.</param>
    /// <param name="deserializedObject">Filled with the object that is the JsonSerialized string.</param>
    /// <param name="throwErrors">If true, will throw errors. Otherwise, returns false on failures.</param>
    /// <returns>A boolean value indicating success.</returns>
    public static bool Deserialize<T>(string value, ref T deserializedObject, bool throwErrors = false)
    {
        if (value == null)
        {
            return false;
        }

#if DEBUG
#warning When in DEBUG Mode JavaScript Deserialization Errors will be thrown regardless of throwErrors parameter.
            throwErrors = true;
#endif

        try
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            deserializedObject = jss.Deserialize<T>(value);
            return true;
        }
        catch
        {
            if (throwErrors)
                throw;

            return false;
        }
    }
}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Style

  • The overall reading of these methods is pretty good and the code is well structured.

  • The documentation is good to read and to understand.

  • Your style is a little bit inconsistent. It's about the using of braces {} for single statement if's . One time at the guard clause you use braces and later on for the if(throwErrors) you don't use them.

    Speaking about throwErrors, the method won't ever throw errors but one Exception so a better name could be either shouldThrowException or throwsException.


Speaking about Serialize() method

I don't like ref parameters that much and I try to avoid them if there is a better/different way to solve this.

For the Serialize() method the usage of an ref parameter could be avoided by passing a StringBuilder instead of a string as argument.

You for sure would need to check if the passed in StringBuilder is not null but you could just pass it afterwards to this overload of the Serialize() method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.