Argo  1.0
A C++ library for handling JSON.
json_exception.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Andrew Haisley
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
24 
25 #include <string.h>
26 #include <ostream>
27 
28 #include "common.hpp"
29 #include "json_exception.hpp"
30 
31 using namespace std;
32 using namespace NAMESPACE;
33 
34 json_exception::json_exception(exception_type et) noexcept : exception(), m_type(et)
35 {
36  switch (et)
37  {
38  case invalid_json_type_e:
39  strncpy(m_message, "internal error, invalid json instance type", max_message_length);
40  break;
41  case not_number_e:
42  strncpy(m_message, "instance is not a number", max_message_length);
43  break;
44  case cant_compare_raw_e:
45  strncpy(m_message, "raw values can't be compared", max_message_length);
46  break;
47  case cant_cast_raw_e:
48  strncpy(m_message, "raw values can't be cast", max_message_length);
49  break;
50  case pointer_not_matched_e:
51  strncpy(m_message, "pointer doesn't match a location in the instance", max_message_length);
52  break;
53  case pointer_token_type_invalid_e:
54  strncpy(m_message, "pointer token type is invalid", max_message_length);
55  break;
56  default:
57  strncpy(m_message, "generic", max_message_length);
58  break;
59  }
60 }
61 
62 json_exception::json_exception(exception_type et, const char *json_type_name) noexcept : exception(), m_type(et)
63 {
64  switch (et)
65  {
66  case not_an_array_e:
67  snprintf(m_message, max_message_length, "instance type is %s not array", json_type_name);
68  break;
69  case not_an_object_e:
70  snprintf(m_message, max_message_length, "instance type is %s not object", json_type_name);
71  break;
72  case not_number_int_or_boolean_e:
73  snprintf(m_message, max_message_length, "instance type is %s not integer number or boolean", json_type_name);
74  break;
75  case not_string_e:
76  snprintf(m_message, max_message_length, "instance type is %s not string", json_type_name);
77  break;
78  case not_number_or_string_e:
79  snprintf(m_message, max_message_length, "attempt to set a raw value of an instance of type %s (only works for number and string)", json_type_name);
80  break;
81  default:
82  strncpy(m_message, "generic", max_message_length);
83  break;
84  }
85 }
86 
87 json_exception::json_exception(
88  exception_type et,
89  const char *first_json_type_name,
90  const char *second_json_type_name) noexcept : exception(), m_type(et)
91 {
92  if (et == types_not_comparable_e)
93  {
94  snprintf(m_message, max_message_length, "attempt to compare %s with %s", first_json_type_name, second_json_type_name);
95  }
96  else
97  {
98  strncpy(m_message, "generic", max_message_length);
99  }
100 }
101 
102 const char *json_exception::what() const noexcept
103 {
104  return m_message;
105 }
106 
107 json_exception::exception_type json_exception::get_type() const noexcept
108 {
109  return m_type;
110 }
111 
112 ostream &NAMESPACE::operator<<(ostream& stream, const NAMESPACE::json_exception &e)
113 {
114  return stream << e.what();
115 }
The json_exception class.
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Definition: common.hpp:29
STL namespace.
Common defs needed everywhere and, as far as is possible, platform specific changes.