Commit a4a051a2 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

fix fromJSON<JSONObject>

parent 0ff32680
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
build
build-dbg
.DS_Store
+13 −17
Original line number Diff line number Diff line
# This file is part of the "FnordMetric" project
#   Copyright (c) 2014 Paul Asmuth, Google Inc.
#
# Licensed under the MIT license (see LICENSE).

all: build
all: build test

build:
	(cd fnordmetric-core && make build)
	mkdir -p build
	(cd build && cmake -GNinja -DCMAKE_BUILD_TYPE=Release .. && ninja)

install:
	(cd fnordmetric-core && make install)
build-dbg:
	mkdir -p build-dbg
	(cd build-dbg && cmake -GNinja -DCMAKE_BUILD_TYPE=Debug .. && ninja)

test:
	(cd fnordmetric-core && make test)
test: build
	@find build/tests -name "test-*" -exec ./{} \;

clean:
	(cd fnordmetric-core && make clean)
test-dbg: build-dbg
	@find build-dbg/tests -name "test-*" -exec ./{} \;

doc:
	find doc/examples -name "*.sql" | while read file; do PATH=./build:$$PATH fnordmetric -f svg -o $${file/.sql/.svg}.html $$file; done
	(cd doc/web && rake build)
clean:
	rm -rf build build-dbg

.PHONY: all test clean doc build
.PHONY: clean build build-dbg test
+7 −2
Original line number Diff line number Diff line
@@ -57,6 +57,11 @@ JSONObject parseJSON(JSONInputStream* json) {
      case JSON_ARRAY_END:
      case JSON_OBJECT_END: {
        obj.emplace_back(token);

        if (stack.empty()) {
          RAISE(kParseError, "unbalanced braces");
        }

        obj[stack.top()].size = obj.size() - stack.top();
        stack.pop();
        break;
@@ -222,11 +227,11 @@ template <>
JSONObject fromJSONImpl(
    std::vector<JSONToken>::const_iterator begin,
    std::vector<JSONToken>::const_iterator end) {
  if (begin == end) {
  if (begin + begin->size > end) {
    RAISE(kIndexError);
  }

  return JSONObject(begin, end);
  return JSONObject(begin, begin + begin->size);
}

} // namespace json
+25 −0
Original line number Diff line number Diff line
@@ -234,6 +234,31 @@ TEST_CASE(JSONTest, TestToFromJSON, [] () {
  EXPECT_EQ(m1.b, m2.b);
});

struct TestStruct {
  fnord::String str;
  fnord::json::JSONObject obj;

  template <typename T>
  static void reflect(T* meta) {
    meta->prop(&TestStruct::str, 1, "str", false);
    meta->prop(&TestStruct::obj, 2, "obj", false);
  };
};

TEST_CASE(JSONTest, TestJSONShouldNotSegfaultOnInvalidInput, [] () {
  EXPECT_EXCEPTION("unbalanced braces", [] {
    auto orig_str = "{ \"str\": \"fnord\", \"obj\": { \"a\": 1, \"b\": 2 ] } }";
    auto obj = fnord::json::fromJSON<TestStruct>(orig_str);
  });
});

TEST_CASE(JSONTest, TestJSONReEncodingViaReflectionWithObject, [] () {
  auto orig_str = "{\"str\":\"fnord\",\"obj\":{\"a\":\"1\",\"b\":\"2\"}}";
  auto obj = fnord::json::fromJSON<TestStruct>(orig_str);
  auto reenc_str = fnord::json::toJSONString(obj);
  EXPECT_EQ(orig_str, reenc_str);
});

/*
TEST_CASE(JSONTest, TestParseJSON, [] () {
  auto json = fnord::json::parseJSON(R"(