Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
a.out
test-core
test-core-int64
test-no-except
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ includedir=$(prefix)/include

check: test

test: test-core test-core-int64
test: test-core test-core-int64 test-no-except
./test-core
./test-core-int64
./test-no-except

test-core: picojson.h test.cc picotest/picotest.c picotest/picotest.h
$(CXX) -Wall test.cc picotest/picotest.c -o $@

test-core-int64: picojson.h test.cc picotest/picotest.c picotest/picotest.h
$(CXX) -Wall -DPICOJSON_USE_INT64 test.cc picotest/picotest.c -o $@

test-no-except: picojson.h test.cc picotest/picotest.c picotest/picotest.h
$(CXX) -Wall -DPICOJSON_NO_EXCEPTIONS test.cc picotest/picotest.c -o $@

clean:
rm -f test-core test-core-int64
rm -f test-core test-core-int64 test-no-except

install:
install -d $(DESTDIR)$(includedir)
Expand Down
39 changes: 37 additions & 2 deletions picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
#include <iterator>
#include <limits>
#include <map>
#if (!defined(PICOJSON_NO_EXCEPTIONS))
#include <stdexcept>
#endif // PICOJSON_NO_EXCEPTIONS)
#include <string>
#include <vector>
#include <utility>
Expand Down Expand Up @@ -83,12 +85,18 @@ extern "C" {
#endif

#ifndef PICOJSON_ASSERT
#if (!defined(PICOJSON_NO_EXCEPTIONS))
#define PICOJSON_ASSERT(e) \
do { \
if (!(e)) \
throw std::runtime_error(#e); \
} while (0)
#endif
#else
#define PICOJSON_ASSERT(e) \
if (!e) \
std::cerr << "picosjon runtime error\n";
#endif // PICOJSON_NO_EXCEPTIONS
#endif // PICOJSON_ASSERT

#ifdef _MSC_VER
#define SNPRINTF _snprintf_s
Expand Down Expand Up @@ -159,11 +167,20 @@ class value {
~value();
value(const value &x);
value &operator=(const value &x);

#if PICOJSON_USE_RVALUE_REFERENCE
#if (!defined(PICOJSON_NO_EXCEPTIONS))
value(value &&x) throw();
value &operator=(value &&x) throw();
#else
value(value &&x) value &operator=(value &&x)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line needs to be fixed to:

  value(value &&x);
  value &operator=(value &&x);

#endif // PICOJSON_NO_EXCEPTIONS)
#endif
#if (!defined(PICOJSON_NO_EXCEPTIONS))
void swap(value &x) throw();
#else
void swap(value &x);
#endif // PICOJSON_NO_EXCEPTIONS)
template <typename T> bool is() const;
template <typename T> const T &get() const;
template <typename T> T &get();
Expand Down Expand Up @@ -237,7 +254,9 @@ inline value::value(double n) : type_(number_type), u_() {
isnan(n) || isinf(n)
#endif
) {
#if !defined(PICOJSON_NO_EXCEPTIONS)
throw std::overflow_error("");
#endif // PICOJSON_NO_EXCEPTIONS)
}
u_.number_ = n;
}
Expand Down Expand Up @@ -318,7 +337,7 @@ inline value &value::operator=(const value &x) {
}
return *this;
}

#if (!defined(PICOJSON_NO_EXCEPTIONS))
#if PICOJSON_USE_RVALUE_REFERENCE
inline value::value(value &&x) throw() : type_(null_type), u_() {
swap(x);
Expand All @@ -332,6 +351,22 @@ inline void value::swap(value &x) throw() {
std::swap(type_, x.type_);
std::swap(u_, x.u_);
}
#else // PICOJSON_NO_EXCEPTIONS
#if PICOJSON_USE_RVALUE_REFERENCE
inline value::value(value &&x) : type_(null_type), u_() {
swap(x);
}
inline value &value::operator=(value &&x) {
swap(x);
return *this;
}
#endif
inline void value::swap(value &x) {
std::swap(type_, x.type_);
std::swap(u_, x.u_);
}

#endif // PICOJSON_NO_EXCEPTIONS

#define IS(ctype, jtype) \
template <> inline bool value::is<ctype>() const { \
Expand Down
2 changes: 2 additions & 0 deletions test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ int main(void)
_ok(v.serialize(true) == "{\n \"a\": 1,\n \"b\": [\n 2,\n {\n \"b1\": \"abc\"\n }\n ],\n \"c\": {},\n \"d\": []\n}\n", "prettifying output");
}

#if !defined(PICOJSON_NO_EXCEPTIONS)
try {
picojson::value v(std::numeric_limits<double>::quiet_NaN());
_ok(false, "should not accept NaN");
Expand All @@ -287,6 +288,7 @@ int main(void)
} catch (std::runtime_error e) {
_ok(true, "get<wrong_type>() should raise an error");
}
#endif

#ifdef PICOJSON_USE_INT64
{
Expand Down