view C++/modules/Json/Json.cpp @ 475:b681299e6987

Json: removal of json::Object, json::Array, json::Document
author David Demelier <markand@malikania.fr>
date Mon, 09 Nov 2015 16:52:38 +0100
parents 902b034df6e3
children c7825b8c6145
line wrap: on
line source

/*
 * Json.cpp -- C++14 JSON manipulation using jansson parser
 *
 * 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 "Json.h"

#include <jansson.h>

namespace json {

namespace {

void readObject(Value &parent, json_t *object);
void readArray(Value &parent, json_t *array);

Value readValue(json_t *v)
{
	if (json_is_null(v)) {
		return Value{nullptr};
	}
	if (json_is_string(v)) {
		return Value{json_string_value(v)};
	}
	if (json_is_real(v)) {
		return Value{json_number_value(v)};
	}
	if (json_is_integer(v)) {
		return Value{static_cast<int>(json_integer_value(v))};
	}
	if (json_is_boolean(v)) {
		return Value{json_boolean_value(v)};
	}
	if (json_is_object(v)) {
		Value object{Type::Object};

		readObject(object, v);

		return object;
	}
	if (json_is_array(v)) {
		Value array{Type::Array};

		readArray(array, v);

		return array;
	}

	return Value{};
}

void readObject(Value &parent, json_t *object)
{
	const char *key;
	json_t *value;

	json_object_foreach(object, key, value) {
		parent.insert(key, readValue(value));
	}
}

void readArray(Value &parent, json_t *array)
{
	size_t index;
	json_t *value;

	json_array_foreach(array, index, value) {
		parent.append(readValue(value));
	}
}

template <typename Func, typename... Args>
Value convert(Func fn, Args&&... args)
{
	json_error_t error;
	json_t *json = fn(std::forward<Args>(args)..., &error);

	if (json == nullptr) {
		throw Error{error.text, error.source, error.line, error.column, error.position};
	}

	Value value;

	if (json_is_object(json)) {
		value = Value{Type::Object};
		readObject(value, json);
	} else {
		value = Value{Type::Array};
		readArray(value, json);
	}

	json_decref(json);

	return value;
}

} // !namespace

void Value::copy(const Value &other)
{
	switch (other.m_type) {
	case Type::String:
		new (&m_string) std::string();
		m_string = std::move(other.m_string);
		break;
	case Type::Real:
		m_number = other.m_number;
		break;
	case Type::Int:
		m_integer = other.m_integer;
		break;
	case Type::Boolean:
		m_boolean = other.m_boolean;
		break;
	case Type::Object:
		new (&m_object) std::map<std::string, Value>();
		m_object = std::move(other.m_object);
		break;
	case Type::Array:
		new (&m_array) std::deque<Value>();
		m_array = std::move(other.m_array);
		break;
	default:
		break;
	}

	m_type = other.m_type;
}

void Value::move(Value &&other)
{
	switch (other.m_type) {
	case Type::String:
		new (&m_string) std::string(std::move(other.m_string));
		break;
	case Type::Real:
		m_number = other.m_number;
		break;
	case Type::Int:
		m_integer = other.m_integer;
		break;
	case Type::Boolean:
		m_boolean = other.m_boolean;
		break;
	case Type::Object:
		new (&m_object) std::map<std::string, Value>(std::move(other.m_object));
		break;
	case Type::Array:
		new (&m_array) std::deque<Value>(std::move(other.m_array));
		break;
	default:
		break;
	}

	m_type = other.m_type;
}

Value::Value(Type type)
	: m_type{type}
{
	switch (type) {
	case Type::String:
		new (&m_string) std::string();
		break;
	case Type::Array:
		new (&m_array) std::deque<Value>();
		break;
	case Type::Object:
		new (&m_object) std::map<std::string, Value>();
		break;
	case Type::Real:
		m_number = 0;
		break;
	case Type::Int:
		m_integer = 0;
		break;
	case Type::Boolean:
		m_boolean = false;
		break;
	default:
		break;
	}
}

Value::~Value()
{
	switch (m_type) {
	case Type::String:
		m_string.~basic_string();
		break;
	case Type::Object:
		m_object.~map<std::string, Value>();
		break;
	case Type::Array:
		m_array.~deque<Value>();
		break;
	default:
		break;
	}
}

bool Value::toBool() const noexcept
{
	if (m_type != Type::Boolean) {
		return false;
	}

	return m_boolean;
}

double Value::toReal() const noexcept
{
	if (m_type != Type::Real) {
		return 0;
	}

	return m_number;
}

int Value::toInt() const noexcept
{
	if (m_type != Type::Int) {
		return 0;
	}

	return m_integer;
}

std::string Value::toString() const noexcept
{
	if (m_type != Type::String) {
		return "";
	}

	return m_string;
}

Value::Value(const Buffer &buffer)
{
	*this = convert(json_loads, buffer.text.c_str(), 0);
}

Value::Value(const File &file)
{
	*this = convert(json_load_file, file.path.c_str(), 0);
}

std::string escape(const std::string &value)
{
	std::string result;

	for (auto it = value.begin(); it != value.end(); ++it) {
		switch (*it) {
		case '\\':
			result += "\\\\";
			break;
		case '/':
			result += "\\/";
			break;
		case '"':
			result += "\\\"";
			break;
		case '\b':
			result += "\\b";
			break;
		case '\f':
			result += "\\f";
			break;
		case '\n':
			result += "\\n";
			break;
		case '\r':
			result += "\\r";
			break;
		case '\t':
			result += "\\t";
			break;
		default:
			result += *it;
			break;
		}
	}

	return result;
}

} // !json