// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
namespace LightJson.Serialization
{
using System;
///
/// The exception that is thrown when a JSON value cannot be serialized.
///
///
/// This exception is only intended to be thrown by LightJson.
///
internal sealed class JsonSerializationException : Exception
{
///
/// Initializes a new instance of the class.
///
public JsonSerializationException()
: base(GetDefaultMessage(ErrorType.Unknown))
{
}
///
/// Initializes a new instance of the class with the given error type.
///
/// The error type that describes the cause of the error.
public JsonSerializationException(ErrorType type)
: this(GetDefaultMessage(type), type)
{
}
///
/// Initializes a new instance of the class with the given message and
/// error type.
///
/// The message that describes the error.
/// The error type that describes the cause of the error.
public JsonSerializationException(string message, ErrorType type)
: base(message)
{
this.Type = type;
}
///
/// Enumerates the types of errors that can occur during serialization.
///
public enum ErrorType
{
///
/// Indicates that the cause of the error is unknown.
///
Unknown = 0,
///
/// Indicates that the writer encountered an invalid number value (NAN, infinity) during serialization.
///
InvalidNumber,
///
/// Indicates that the object been serialized contains an invalid JSON value type.
/// That is, a value type that is not null, boolean, number, string, object, or array.
///
InvalidValueType,
///
/// Indicates that the object been serialized contains a circular reference.
///
CircularReference,
}
///
/// Gets the type of error that caused the exception to be thrown.
///
///
/// The type of error that caused the exception to be thrown.
///
public ErrorType Type { get; }
private static string GetDefaultMessage(ErrorType type)
{
switch (type) {
case ErrorType.InvalidNumber:
return "The value been serialized contains an invalid number value (NAN, infinity).";
case ErrorType.InvalidValueType:
return "The value been serialized contains (or is) an invalid JSON type.";
case ErrorType.CircularReference:
return "The value been serialized contains circular references.";
default:
return "An error occurred during serialization.";
}
}
}
}