手打范德萨发斯蒂芬

#pragma once

#include <string>
#include <vector>
#include <map>
using namespace std;

namespace yazi {
    namespace json {
        class Json {
        public:
            enum Type
            {
                json_null = 0,
                json_bool,
                json_int,
                json_double,
                json_string,
                json_array,
                json_object
            };
            Json();
            Json(bool value);
            Json(int value);
            Json(double value);
            Json(const char* value);
            Json(const string& value);
            Json(Type type);
            Json(const Json& other);

            operator bool();
            operator int();
            operator double();
            operator string();

            Json& operator[](int index);
            void append(const Json& other);
            
            Json& operator[](const char* key);
            Json & operator[](const string* key);

            string str() const;

            void operator = (const Json& other);

        private:
            union Value
            {
                bool m_bool;
                int m_int;
                double m_double;
                std::string* m_string;
                std::vector<Json>* m_array;
                std::map<string, Json> *m_object;
            };

            Type m_type;
            Value m_value;
        };
    }
}

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <sstream>
#include "json.h"

using namespace std;
using namespace yazi::json;

Json::Json() :m_type(json_null)
{

}
Json::Json(bool value) :m_type(json_bool)
{
    m_value.m_bool = value;
}
Json::Json(int value) :m_type(json_int)
{
    m_value.m_int = value;
}
Json::Json(double value) :m_type(json_double)
{
    m_value.m_double = value;
}
Json::Json(const char * value) :m_type(json_string)
{
    m_value.m_string = new string(value);
}
Json::Json(const string &value) :m_type(json_string)
{
    m_value.m_string = new string(value);
}
Json::Json(Type type) :m_type(type)
{
    switch (m_type)
    {
    case json_null:
        break;
    case json_bool:
        m_value.m_bool = false;
        break;
    case json_int:
        m_value.m_int = 0;
        break;
    case json_double:
        m_value.m_double = 0.0;
        break;
    case json_string:
        m_value.m_string = new string("");
        break;
    case json_array:
        m_value.m_array = new std::vector<Json>();
        break;
    case json_object:
        m_value.m_object = new std::map<string, Json>();
        break;
    default:
        break;
    }
}

Json::Json(const Json& other)
{
    m_type = other.m_type;
    switch (m_type)
    {
    case json_null:
        break;
    case json_bool:
        m_value.m_bool = other.m_value.m_bool;
        break;
    case json_int:
        m_value.m_int == other.m_value.m_int;
        break;
    case json_double:
        m_value.m_double = other.m_value.m_double;
        break;
    case json_string:
        m_value.m_string = other.m_value.m_string;
        break;
    case json_array:
        m_value.m_array = other.m_value.m_array;
        break;
    case json_object:
        m_value.m_object = other.m_value.m_object;
        break;
    default:
        break;
    }
}


Json::operator bool()
{
    if (m_type != json_bool)
    {
        throw new logic_error("type error");
    }

    return m_value.m_bool;
}

Json::operator int()
{
    if (m_type != json_int)
    {
        throw new logic_error("type error");
    }
    return m_value.m_int;
}

Json::operator double()
{
    if (m_type != json_double)
    {
        throw new logic_error("type error");
    }
    return m_value.m_double;
}

Json::operator string()
{
    if (m_type != json_string)
    {
        throw new logic_error("type error");
    }
    return *(m_value.m_string);
}


Json& Json::operator[](int index)
{
    if (m_type != json_array)
    {
        m_type = json_array;
        m_value.m_array = new vector<Json>();
    }
    if (index < 0) {
        throw new logic_error("array[] index <0");
    }
    int size = m_value.m_array->size();
    if (index >= size)
    {
        for (int i = size; i < index; i++)
        {
            (m_value.m_array)->push_back(Json());
        }
    }
    return m_value.m_array->at(index);
}
void Json::append(const Json& other)
{
    if (m_type != json_array)
    {
        m_type = json_array;
        m_value.m_array = new vector<Json>();
    }
    (m_value.m_array)->push_back(other);
}


string Json::str() const
{
    stringstream ss;
    switch (m_type)
    {
    case yazi::json::Json::json_null:
        ss << "null";
        break;
    case yazi::json::Json::json_bool:
        if (m_value.m_bool)
        {
            ss << "true";
        }
        else
        {
            ss << "false";
        }
        break;
    case yazi::json::Json::json_int:
        ss << m_value.m_int;
        break;
    case yazi::json::Json::json_double:
        ss << m_value.m_double;
        break;
    case yazi::json::Json::json_string:
        ss << '\"' << *(m_value.m_string)<<'\"';
        break;
    case yazi::json::Json::json_array:
    {
        ss << '[';
        for (auto it = (m_value.m_array)->begin(); it != (m_value.m_array)->end(); it++)
        {
            if (it != m_value.m_array->begin())
            {
                ss << ',';
            }
            ss << it->str();
        }
        ss << ']';
    }
        break;
    case yazi::json::Json::json_object:
        ss << '{';
        for (auto it = (m_value.m_object)->begin(); it != (m_value.m_object)->end(); it++)
        {
            if (it != m_value.m_object->begin())
            {
                ss << ',';
            }
            ss << '\"'<<it->first<<'\"'<<':'<<it->second.str();
        }
        ss << '}';
        break;
    default:
        break;
    }
    return ss.str();
}

Json& Json::operator[](const char* key)
{
    string name(key);
    return (*(this))[name];
}

Json& Json::operator[](const string* key)
{
    return (*(m_value.m_object))[key];
}

void Json::operator = (const Json& other)
{
    m_type = other.m_type;
    switch (m_type)
    {
    case json_null:
        break;
    case json_bool:
        m_value.m_bool = other.m_value.m_bool;
        break;
    case json_int:
        m_value.m_int == other.m_value.m_int;
        break;
    case json_double:
        m_value.m_double = other.m_value.m_double;
        break;
    case json_string:
        m_value.m_string = other.m_value.m_string;
        break;
    case json_array:
        m_value.m_array = other.m_value.m_array;
        break;
    case json_object:
        m_value.m_object = other.m_value.m_object;
        break;
    default:
        break;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值