#pragma once
#ifndef JSON_H
#define JSON_H

#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>

namespace json {

class JSONError : public std::runtime_error {
    public:
        JSONError(std::string msg) : std::runtime_error(msg) { }
};

enum Type {
    Num,
    String,
    Object,
    Array,
    Bool,
    Null
};

class JSON {
    public:
        /**
         * Default constructor, is a JSON null value
         */
        JSON();

        JSON(const JSON& other);
        JSON(JSON&& other);
        JSON& operator=(const JSON& other);
        JSON& operator=(JSON&& other);

        ~JSON();

        operator std::string() const;
        operator double() const;
        operator bool() const;
        JSON& operator[] (int) const;
        JSON& operator[] (std::string) const;
        JSON& operator[] (const char*) const;
        JSON& operator[] (const JSON&) const;

        std::string to_string() const;
        double to_double() const;
        bool to_bool() const;

        std::size_t size() const;
        void push_back(const JSON& other);
        void push_front(const JSON& other);

        bool isNull() const;

        Type getType() const;

        static JSON num(double n);
        static JSON string(const std::string& s);
        static JSON object();
        static JSON array();
        static JSON boolean(bool b);
        static JSON null();

        friend std::ostream& operator<<(std::ostream&, const JSON&);

    private:
        Type type;

        //Pointers because of union restrictions
        union Value {
            double num;
            std::string* str;
            std::map<std::string, JSON>* obj;
            std::deque<JSON>* arr;
            bool b;
        } val;

        JSON(Type t);
        JSON(Type t, Value v);
};

std::ostream& operator<<(std::ostream& os, const JSON& j);

}

#endif