AniJSONParseUtils.cpp 10.6 KB
//
//  AniJSONParseUtils.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 19.05.2017.
//
//

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

cocos2d::Size AniJSONParseUtils::getSize(const Value& json)
{
    auto w = -1, h = -1;
    if(AniJSONParseUtils::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 AniJSONParseUtils::hasMemberRect(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && AniJSONParseUtils::isRect(json[memberName]);
}

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

cocos2d::Rect AniJSONParseUtils::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 AniJSONParseUtils::hasMemberColor3B(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && AniJSONParseUtils::isColor3B(json[memberName]);
}

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

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

cocos2d::Color3B AniJSONParseUtils::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 AniJSONParseUtils::hasMemberColor4F(const Value& json, const char* memberName)
{
    return json.HasMember(memberName) && AniJSONParseUtils::isColor4F(json[memberName]);
}

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

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

cocos2d::Color4F AniJSONParseUtils::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 AniJSONParseUtils::checkMemberBool(const Value& json, const char* memberName, bool boolValue)
{
    return AniJSONParseUtils::hasMemberBool(json, memberName) && json[memberName].GetBool() == boolValue;
}

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

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

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

std::vector<std::string> AniJSONParseUtils::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> AniJSONParseUtils::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> AniJSONParseUtils::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> AniJSONParseUtils::parsePointArray(const Value& json)
{
    std::vector<cocos2d::Point> values;
    
    if(json.IsArray()){
        for(const auto& value : json.GetArray()){
            if(AniJSONParseUtils::isPoint(value)){
                values.push_back(AniJSONParseUtils::getPoint(value));
            }
        }
    }
    else if(AniJSONParseUtils::isPoint(json)){
        values.push_back(AniJSONParseUtils::getPoint(json));
    }
    
    return values;
}

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

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

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

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

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

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

//std::vector<rapidjson::Value*> AniJSONParseUtils::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;
//}