// 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 message cannot be parsed.
///
///
/// This exception is only intended to be thrown by LightJson.
///
internal sealed class JsonParseException : Exception
{
///
/// Initializes a new instance of the class.
///
public JsonParseException()
: base(GetDefaultMessage(ErrorType.Unknown))
{
}
///
/// Initializes a new instance of the class with the given error type and position.
///
/// The error type that describes the cause of the error.
/// The position in the text where the error occurred.
public JsonParseException(ErrorType type, TextPosition position)
: this(GetDefaultMessage(type), type, position)
{
}
///
/// Initializes a new instance of the class with the given message, error type, and position.
///
/// The message that describes the error.
/// The error type that describes the cause of the error.
/// The position in the text where the error occurred.
public JsonParseException(string message, ErrorType type, TextPosition position)
: base(message)
{
this.Type = type;
this.Position = position;
}
///
/// Enumerates the types of errors that can occur when parsing a JSON message.
///
public enum ErrorType : int
{
///
/// Indicates that the cause of the error is unknown.
///
Unknown = 0,
///
/// Indicates that the text ended before the message could be parsed.
///
IncompleteMessage,
///
/// Indicates that a JsonObject contains more than one key with the same name.
///
DuplicateObjectKeys,
///
/// Indicates that the parser encountered and invalid or unexpected character.
///
InvalidOrUnexpectedCharacter,
}
///
/// Gets the text position where the error occurred.
///
/// The text position where the error occurred.
public TextPosition Position { get; private set; }
///
/// 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 set; }
private static string GetDefaultMessage(ErrorType type)
{
switch (type) {
case ErrorType.IncompleteMessage:
return "The string ended before a value could be parsed.";
case ErrorType.InvalidOrUnexpectedCharacter:
return "The parser encountered an invalid or unexpected character.";
case ErrorType.DuplicateObjectKeys:
return "The parser encountered a JsonObject with duplicate keys.";
default:
return "An error occurred while parsing the JSON message.";
}
}
}
}