JSONParseUtils.cpp 10.3 KB
//
//  JSONParseUtils.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 19.05.2017.
//
//

#include <stdio.h>
#include "JSONParseUtils.h"

std::string JSONParseUtils::loadJSONFromFile(const std::string& jsonFilePath)
{
    std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(jsonFilePath);
    return cocos2d::FileUtils::getInstance()->getStringFromFile(fullPath);
}

bool JSONParseUtils::hasMemberBool(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && json[memberName].IsBool();
}

bool JSONParseUtils::hasMemberInt(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && json[memberName].IsInt();
}

bool JSONParseUtils::hasMemberFloat(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && (json[memberName].IsFloat() || json[memberName].IsInt());
}

bool JSONParseUtils::hasMemberString(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && json[memberName].IsString();
}

bool JSONParseUtils::hasMemberArray(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && json[memberName].IsArray();
}

bool JSONParseUtils::hasMemberObject(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && json[memberName].IsObject();
}

bool JSONParseUtils::hasMemberPoint(const Value& json, const char* memberName)
{
    return json.IsObject() && json.HasMember(memberName) && JSONParseUtils::isPoint(json[memberName]);
}

cocos2d::Point JSONParseUtils::getMemberPoint(const Value& json, const char* memberName)
{
    return JSONParseUtils::getPoint(json[memberName]);
}

bool JSONParseUtils::isPoint(const Value& json)
{
    return JSONParseUtils::hasMemberFloat(json, "x") && JSONParseUtils::hasMemberFloat(json, "y");
}

cocos2d::Point JSONParseUtils::getPoint(const Value& json)
{
    auto x = json["x"].GetFloat();
    auto y = json["y"].GetFloat();
    return cocos2d::Point(x,y);
}

bool JSONParseUtils::hasMemberSize(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && JSONParseUtils::isSize(json[memberName]);
}

cocos2d::Point JSONParseUtils::getMemberSize(const Value& json, const char* memberName)
{
    return JSONParseUtils::getSize(json[memberName]);
}

bool JSONParseUtils::isSize(const Value& json)
{
    return json.IsObject() && ((JSONParseUtils::hasMemberFloat(json, "w") && JSONParseUtils::hasMemberFloat(json, "h")) || (JSONParseUtils::hasMemberFloat(json, "width") && JSONParseUtils::hasMemberFloat(json, "height")));
}

cocos2d::Size JSONParseUtils::getSize(const Value& json)
{
    auto w = -1, h = -1;
    if(JSONParseUtils::hasMemberFloat(json, "w")){
        w = json["w"].GetFloat();
        h = json["h"].GetFloat();
    }
    else {
        w = json["width"].GetFloat();
        h = json["height"].GetFloat();
    }
    
    return cocos2d::Size(w,h);
}

bool JSONParseUtils::hasMemberRect(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && JSONParseUtils::isRect(json[memberName]);
}

cocos2d::Rect JSONParseUtils::getMemberRect(const Value& json, const char* memberName)
{
   return JSONParseUtils::getRect(json[memberName]);
}
    
bool JSONParseUtils::isRect(const Value& json)
{
    return json.IsObject() && JSONParseUtils::hasMemberFloat(json, "x") && JSONParseUtils::hasMemberFloat(json, "y") && JSONParseUtils::hasMemberFloat(json, "w") && JSONParseUtils::hasMemberFloat(json, "h");
}

cocos2d::Rect JSONParseUtils::getRect(const Value& json)
{
    auto x = json["x"].GetFloat();
    auto y = json["y"].GetFloat();
    auto w = json["w"].GetFloat();
    auto h = json["h"].GetFloat();
    return cocos2d::Rect(x,y,w,h);
}

bool JSONParseUtils::hasMemberColor3B(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && JSONParseUtils::isColor3B(json[memberName]);
}

cocos2d::Color3B JSONParseUtils::getMemberColor3B(const Value& json, const char* memberName)
{
    return JSONParseUtils::getColor3B(json[memberName]);
}

bool JSONParseUtils::isColor3B(const Value& json)
{
    return json.IsObject() && JSONParseUtils::hasMemberInt(json, "r") && JSONParseUtils::hasMemberInt(json, "g") && JSONParseUtils::hasMemberInt(json, "b");
}

cocos2d::Color3B JSONParseUtils::getColor3B(const Value& json)
{
    auto r = json["r"].GetInt();
    auto g = json["g"].GetInt();
    auto b = json["b"].GetInt();
    return cocos2d::Color3B(r,g,b);
}

bool JSONParseUtils::hasMemberColor4F(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && JSONParseUtils::isColor4F(json[memberName]);
}

cocos2d::Color4F JSONParseUtils::getMemberColor4F(const Value& json, const char* memberName)
{
    return JSONParseUtils::getColor4F(json[memberName]);
}

bool JSONParseUtils::isColor4F(const Value& json)
{
    return json.IsObject() && JSONParseUtils::hasMemberFloat(json, "r") && JSONParseUtils::hasMemberFloat(json, "g") && JSONParseUtils::hasMemberFloat(json, "b") && JSONParseUtils::hasMemberFloat(json, "a");
}

cocos2d::Color4F JSONParseUtils::getColor4F(const Value& json)
{
    auto r = json["r"].GetFloat();
    auto g = json["g"].GetFloat();
    auto b = json["b"].GetFloat();
    auto a = json["a"].GetFloat();
    return cocos2d::Color4F(r,g,b,a);
}

bool JSONParseUtils::checkMemberBool(const Value& json, const char* memberName, bool boolValue)
{
    return JSONParseUtils::hasMemberBool(json, memberName) && json[memberName].GetBool() == boolValue;
}

bool JSONParseUtils::checkMemberInt(const Value& json, const char* memberName, int intValue)
{
    return JSONParseUtils::hasMemberInt(json, memberName) && json[memberName].GetInt() == intValue;
}

bool JSONParseUtils::checkMemberFloat(const Value& json, const char* memberName, float floatValue)
{
    return JSONParseUtils::hasMemberInt(json, memberName) && json[memberName].GetFloat() == floatValue;
}

bool JSONParseUtils::checkMemberString(const Value& json, const char* memberName, std::string expectedValue)
{
    return JSONParseUtils::hasMemberString(json, memberName) && expectedValue == json[memberName].GetString();
}

std::vector<std::string> JSONParseUtils::parseStringArray(const Value& json)
{
    std::vector<std::string> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(value.IsString()){
                values.push_back(value.GetString());
            }
        }
    }
    else if(json.IsString()){
        values.push_back(json.GetString());
    }
    
    return values;
}

std::vector<float> JSONParseUtils::parseFloatArray(const Value& json)
{
    std::vector<float> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(value.IsFloat() || value.IsInt()){
                values.push_back(value.GetFloat());
            }
        }
    }
    else if(json.IsFloat() || json.IsInt()){
        values.push_back(json.GetFloat());
    }
    
    return values;
}

std::vector<int> JSONParseUtils::parseIntArray(const Value& json)
{
    std::vector<int> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(value.IsInt()){
                values.push_back(value.GetInt());
            }
        }
    }
    else if(json.IsInt()){
        values.push_back(json.GetInt());
    }
    
    return values;
}

std::vector<cocos2d::Point> JSONParseUtils::parsePointArray(const Value& json)
{
    std::vector<cocos2d::Point> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(JSONParseUtils::isPoint(value)){
                values.push_back(JSONParseUtils::getPoint(value));
            }
        }
    }
    else if(JSONParseUtils::isPoint(json)){
        values.push_back(JSONParseUtils::getPoint(json));
    }
    
    return values;
}

std::vector<cocos2d::Size> JSONParseUtils::parseSizeArray(const Value& json)
{
    std::vector<cocos2d::Size> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(JSONParseUtils::isSize(value)){
                values.push_back(JSONParseUtils::getSize(value));
            }
        }
    }
    else if(JSONParseUtils::isSize(json)){
        values.push_back(JSONParseUtils::getSize(json));
    }
    
    return values;
}

std::vector<cocos2d::Rect> JSONParseUtils::parseRectArray(const Value& json)
{
    std::vector<cocos2d::Rect> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(JSONParseUtils::isRect(value)){
                values.push_back(JSONParseUtils::getRect(value));
            }
        }
    }
    else if(JSONParseUtils::isRect(json)){
        values.push_back(JSONParseUtils::getRect(json));
    }
    
    return values;
}

std::vector<std::string> JSONParseUtils::parseMemberStringArray(const Value& json, const char* memberName)
{
    if(JSONParseUtils::hasMemberArray(json, memberName)){
        return JSONParseUtils::parseStringArray(json[memberName]);
    }
    
    return std::vector<std::string>();
}

std::vector<float> JSONParseUtils::parseMemberFloatArray(const Value& json, const char* memberName)
{
    if(JSONParseUtils::hasMemberArray(json, memberName)){
        return JSONParseUtils::parseFloatArray(json[memberName]);
    }
    
    return std::vector<float>();
}

std::vector<int> JSONParseUtils::parseMemberIntArray(const Value& json, const char* memberName)
{
    if(JSONParseUtils::hasMemberArray(json, memberName)){
        return JSONParseUtils::parseIntArray(json[memberName]);
    }
    
    return std::vector<int>();
}

std::vector<cocos2d::Point> JSONParseUtils::parseMemberPointArray(const Value& json, const char* memberName)
{
    if(JSONParseUtils::hasMemberArray(json, memberName)){
        return JSONParseUtils::parsePointArray(json[memberName]);
    }
    
    return std::vector<cocos2d::Point>();
}

//std::vector<rapidjson::Value*> JSONParseUtils::parseValueArray(const Value& json, rapidjson::CrtAllocator* allocator){
//    std::vector<rapidjson::Value*> vec;
//    auto array = json.GetArray();
//    for(int i = 0; i < array.Size(); ++i){
//        const rapidjson::Value* action = &array[i];
//        rapidjson::Value* actionCopy = new rapidjson::Value();
//        actionCopy->CopyFrom(action, allocator);
//        vec.push_back(actionCopy);
//    }
//    return vec;
//}