mrb_value ccvaluemap_to_rubyval(mrb_state* mrb, const cocos2d::ValueMap& inValue) { mrb_value rhash = mrb_hash_new(mrb); for (auto iter = inValue.begin(); iter != inValue.end(); ++iter) { std::string key = iter->first; const Value& obj = iter->second; mrb_value hkey = mrb_str_new_cstr(mrb, key.c_str()); mrb_value hval = ccvalue_to_rubyval(mrb, obj); mrb_hash_set(mrb, rhash, hkey, hval); } return rhash; }
void Json_Parser::write_a_map(cocos2d::ValueMap &temp,rapidjson::Value &val2,rapidjson::Document::AllocatorType&allocator){ rapidjson::Value object2(rapidjson::kObjectType); auto it=temp.begin(); for (;it!=temp.end();it++) { rapidjson::Value v; rapidjson::Value v2; v.SetString(it->first.c_str(),allocator); if(it->second.getType()==Value::Type::DOUBLE){ v2.SetDouble(it->second.asDouble()); val2.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::STRING){ v2.SetString(it->second.asString().c_str(),allocator); val2.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::BOOLEAN){ v2.SetBool(it->second.asBool()); val2.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::INTEGER){ v2.SetInt(it->second.asInt()); val2.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::NONE){ v2.SetNull(); val2.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::MAP){ ValueMap chmap=it->second.asValueMap(); v2.SetObject(); write_a_map(chmap,v2,allocator); val2.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::VECTOR){ ValueVector chvec=it->second.asValueVector(); v2.SetArray(); write_a_array(chvec, v2, allocator); val2.AddMember(v,v2,allocator); } } }
void Resource::addResourceReference(const cocos2d::ValueMap& files) { if (files.size()<1) return; for (auto iter=files.begin(); iter!=files.end(); iter++) { auto fullpath=cocos2d::FileUtils::getInstance()->fullPathForFilename(iter->first); if (!fullpath.empty()) { auto find=_resourceReferences.find(fullpath); if (find!=_resourceReferences.end()) { find->second++; } else { _resourceReferences.insert(std::make_pair(fullpath, 1)); _resourceNotLoadded.push_back(fullpath); _resourceTypes.insert(std::make_pair(fullpath, iter->second.asString())); } } } }
static void dictionary_Value(CCDictionary * aDict, const cocos2d::ValueMap & value) { cocos2d::ValueMap::const_iterator beg = value.begin(); cocos2d::ValueMap::const_iterator end = value.end(); for (; beg != end; ++beg) { const std::string & key = (*beg).first; const cocos2d::Value & v = (*beg).second; if (v.getType() == Value::Type::MAP) { CCDictionary * d = new CCDictionary(); d->init(); dictionary_Value(d, v.asValueMap()); aDict->setObject(d, key); d->release(); } else if (v.getType() == Value::Type::VECTOR) { CCArray * a = new CCArray(); a->init(); array_Value(a, v.asValueVector()); aDict->setObject(a, key); a->release(); } else { CCString * str = new CCString(); if (v.getType() == Value::Type::DOUBLE) str->initWithFormat("%f", v.asDouble()); else str->initWithFormat("%s", v.asString().c_str()); aDict->setObject(str, key); str->release(); } } }
//写入文件系列 //将ValueMap写入一个一个Json文件 void Json_Parser::write_a_map(cocos2d::ValueMap &map_temp,const char*filepath){ rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType&allocator=doc.GetAllocator(); rapidjson::Value object(rapidjson::kObjectType); rapidjson::Value array(rapidjson::kArrayType); auto it=map_temp.begin(); for (;it!=map_temp.end();it++) { rapidjson::Value v; rapidjson::Value v2; v.SetString(it->first.c_str(),allocator); if(it->second.getType()==Value::Type::DOUBLE){ v2.SetDouble(it->second.asDouble()); object.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::STRING){ v2.SetString(it->second.asString().c_str(),allocator); object.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::BOOLEAN){ v2.SetBool(it->second.asBool()); object.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::INTEGER){ v2.SetInt(it->second.asInt()); object.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::NONE){ v2.SetNull(); object.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::MAP){ ValueMap chmap=it->second.asValueMap(); v2.SetObject(); write_a_map(chmap,v2,allocator); object.AddMember(v,v2,allocator); } else if(it->second.getType()==Value::Type::VECTOR){ ValueVector chvec=it->second.asValueVector(); v2.SetArray(); write_a_array(chvec, v2, allocator); object.AddMember(v,v2,allocator); } } array.PushBack(object,allocator); doc.AddMember("record",array,allocator); rapidjson::StringBuffer buffer; rapidjson:: Writer<rapidjson::StringBuffer> writer(buffer); doc.Accept(writer); auto out=buffer.GetString(); log("out:%s",out); FILE*file=fopen(filepath, "wb"); if (file) { fputs(buffer.GetString(),file); fclose(file); } }