Mercurial > code
view C++/modules/Json/Json.cpp @ 404:9ab878fb9fa2
Json: bring classes into json namespace
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 05 Oct 2015 14:39:57 +0200 |
parents | d5ec1174b707 |
children | f81478065901 |
line wrap: on
line source
/* * Json.cpp -- jansson C++11 wrapper * * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <stdexcept> #include "Json.h" namespace json { /* -------------------------------------------------------- * Object * -------------------------------------------------------- */ Object Value::toObject() const noexcept { json_incref(m_handle.get()); return Object(m_handle.get()); } Array Value::toArray() const noexcept { json_incref(m_handle.get()); return Array(m_handle.get()); } /* -------------------------------------------------------- * Array * -------------------------------------------------------- */ Value Array::at(int index) const { auto value = json_array_get(m_handle.get(), index); if (value == nullptr) throw Error("index out of bounds"); json_incref(value); return Value{value}; } Value Array::operator[](int index) const noexcept { auto value = json_array_get(m_handle.get(), index); if (value == nullptr) return Value(); json_incref(value); return Value(value); } Array::Ref Array::operator[](int index) noexcept { auto value = json_array_get(m_handle.get(), index); if (value == nullptr) value = json_null(); else json_incref(value); return Ref(value, *this, index); } /* -------------------------------------------------------- * Object * -------------------------------------------------------- */ Object::Ref Object::operator[](const std::string &name) { if (typeOf() != Type::Object) return Ref(Value(), *this, name); auto value = json_object_get(m_handle.get(), name.c_str()); json_incref(value); return Ref(value, *this, name); } Value Object::operator[](const std::string &name) const { if (typeOf() != Type::Object) return Value(); auto value = json_object_get(m_handle.get(), name.c_str()); if (value == nullptr) return Value(); json_incref(value); return Value(value); } /* -------------------------------------------------------- * Document * -------------------------------------------------------- */ Value Document::read(std::string content, int flags) const { json_error_t error; json_t *json = json_loads(content.c_str(), flags, &error); if (json == nullptr) throw Error(error); return Value(json); } Value Document::read(std::ifstream &stream, int flags) const { if (!stream.is_open()) throw Error("File not opened"); stream.seekg(0, stream.end); auto length = stream.tellg(); stream.seekg(0, stream.beg); std::string buffer; buffer.resize(length, ' '); stream.read(&buffer[0], length); stream.close(); return read(std::move(buffer), flags); } Document::Document(std::ifstream &stream, int flags) { m_value = read(stream, flags); } Document::Document(std::ifstream &&stream, int flags) { m_value = read(stream, flags); } Document::Document(std::string content, int flags) { m_value = read(std::move(content), flags); } } // !json