TYPED_TEST(DocumentMove, MoveAssignmentParseError) {
    typedef TypeParam Allocator;
    typedef GenericDocument<UTF8<>, Allocator> Document;

    ParseResult noError;
    Document a;
    a.Parse("{ 4 = 4]");
    ParseResult error(a.GetParseError(), a.GetErrorOffset());
    EXPECT_TRUE(a.HasParseError());
    EXPECT_NE(error.Code(), noError.Code());
    EXPECT_NE(error.Offset(), noError.Offset());

    Document b;
    b = std::move(a);
    EXPECT_FALSE(a.HasParseError());
    EXPECT_TRUE(b.HasParseError());
    EXPECT_EQ(a.GetParseError(), noError.Code());
    EXPECT_EQ(b.GetParseError(), error.Code());
    EXPECT_EQ(a.GetErrorOffset(), noError.Offset());
    EXPECT_EQ(b.GetErrorOffset(), error.Offset());

    Document c;
    c = std::move(b);
    EXPECT_FALSE(b.HasParseError());
    EXPECT_TRUE(c.HasParseError());
    EXPECT_EQ(b.GetParseError(), noError.Code());
    EXPECT_EQ(c.GetParseError(), error.Code());
    EXPECT_EQ(b.GetErrorOffset(), noError.Offset());
    EXPECT_EQ(c.GetErrorOffset(), error.Offset());
}
示例#2
0
int main() {
    Document d;

    {
        AsyncDocumentParser<> parser(d);

        const char json1[] = " { \"hello\" : \"world\", \"t\" : tr";
        //const char json1[] = " { \"hello\" : \"world\", \"t\" : trX"; // For test parsing error
        const char json2[] = "ue, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.14";
        const char json3[] = "16, \"a\":[1, 2, 3, 4] } ";

        parser.ParsePart(json1, sizeof(json1) - 1);
        parser.ParsePart(json2, sizeof(json2) - 1);
        parser.ParsePart(json3, sizeof(json3) - 1);
    }

    if (d.HasParseError()) {
        std::cout << "Error at offset " << d.GetErrorOffset() << ": " << GetParseError_En(d.GetParseError()) << std::endl;
        return EXIT_FAILURE;
    }
    
    // Stringify the JSON to cout
    OStreamWrapper os(std::cout);
    Writer<OStreamWrapper> writer(os);
    d.Accept(writer);
    std::cout << std::endl;

    return EXIT_SUCCESS;
}
示例#3
0
TEST(Document, UnchangedOnParseError) {
    Document doc;
    doc.SetArray().PushBack(0, doc.GetAllocator());

    ParseResult err = doc.Parse("{]");
    EXPECT_TRUE(doc.HasParseError());
    EXPECT_EQ(err.Code(), doc.GetParseError());
    EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
    EXPECT_TRUE(doc.IsArray());
    EXPECT_EQ(doc.Size(), 1u);

    err = doc.Parse("{}");
    EXPECT_FALSE(doc.HasParseError());
    EXPECT_FALSE(err.IsError());
    EXPECT_EQ(err.Code(), doc.GetParseError());
    EXPECT_EQ(err.Offset(), doc.GetErrorOffset());
    EXPECT_TRUE(doc.IsObject());
    EXPECT_EQ(doc.MemberCount(), 0u);
}
示例#4
0
bool Export(const char* data, const char* outFile)
{
	Document document;
	document.Parse(data);

	if (document.HasParseError()) {
		ParseErrorCode err = document.GetParseError();
		printf("pares error! %d %d", err, document.GetErrorOffset());
		return false;
	}

	if (!document.IsObject())
	{
		printf("error, not a object");
		return false;
	}

	InitializeSdkObjects();

	const Value& skel = document["Skeleton"];
	if (!skel.IsNull())
	{
		ExportBones(skel);
	}

	const Value& mesh = document["Mesh"];
	if (!mesh.IsNull())
	{
		for (uint32_t i = 0; i < mesh.Size(); i++)
		{
			ExportFbxMesh(mesh[i]);
		}
	}

	const Value& anim = document["Animation"];
	if (!anim.IsNull())
	{
		for (uint32_t i = 0; i < anim.Size(); i++)
		{
			ExportAnimation(anim[i]);
		}
	}
	
	SaveFbxFile(outFile);

	return true;
}
Sprites parseSprite(const std::string& image, const std::string& json) {
    using namespace rapidjson;

    Sprites sprites;

    // Parse the sprite image.
    const util::Image raster(image);
    if (!raster) {
        Log::Warning(Event::Sprite, "Could not parse sprite image");
        return sprites;
    }

    Document doc;
    doc.Parse<0>(json.c_str());

    if (doc.HasParseError()) {
        Log::Warning(Event::Sprite, std::string{ "Failed to parse JSON: " } + doc.GetParseError() +
                                        " at offset " + std::to_string(doc.GetErrorOffset()));
        return sprites;
    } else if (!doc.IsObject()) {
        Log::Warning(Event::Sprite, "Sprite JSON root must be an object");
        return sprites;
    } else {
        for (Value::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr) {
            const std::string name = { itr->name.GetString(), itr->name.GetStringLength() };
            const Value& value = itr->value;

            if (value.IsObject()) {
                const uint16_t x = getUInt16(value, "x", 0);
                const uint16_t y = getUInt16(value, "y", 0);
                const uint16_t width = getUInt16(value, "width", 0);
                const uint16_t height = getUInt16(value, "height", 0);
                const double pixelRatio = getDouble(value, "pixelRatio", 1);
                const bool sdf = getBoolean(value, "sdf", false);

                auto sprite = createSpriteImage(raster, x, y, width, height, pixelRatio, sdf);
                if (sprite) {
                    sprites.emplace(name, sprite);
                }
            }
        }
    }

    return sprites;
}
GameState* JsonEncodeDecode::decode(const char *buffer)
{
	Document doc;

	// Parse and catch errors
	if (doc.Parse(buffer).HasParseError())
	{
		printf("\n----------\nError (offset %u): %s\n----------\n",
				(unsigned)doc.GetErrorOffset(),
				GetParseError_En(doc.GetParseError()));
		return NULL;
	}

	// Create new GameState
	GameState* state = new GameState();

	// Iterate through JSON document
	for (Value::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr)
	{
		/*
		 * GAME OVER STATE
		 */
		if (strcmp(itr->name.GetString(), "GameOver") == 0)
		{
			int gameOver = itr->value.GetInt();
			state->setGameOver(gameOver);
		}

		/*
		 * TIME TO RESTART
		 */
		if (strcmp(itr->name.GetString(), "SecToRestart") == 0)
		{
			int secToRestart = itr->value.GetInt();
			state->setSecToRestart(secToRestart);
		}

		/*
		 * VEHICLES
		 */
		if (strcmp(itr->name.GetString(), "Vehicles") == 0)
		{
			// If Vehicles is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Vehicles array is not array\n----------\n");
				return NULL;
			}

			const Value& vehicleArray = itr->value;
			for (SizeType i = 0; i < vehicleArray.Size(); i++)
			{
				if (decodeVehicle(state, vehicleArray[i]) == -1)
				{
					printf("\n----------\nError decoding vehicles\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * BULLETS
		 */
		if (strcmp(itr->name.GetString(), "Bullets") == 0)
		{
			// If Bullets is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Bullets array is not array\n----------\n");
				return NULL;
			}

			const Value& bulletArray = itr->value;
			for (SizeType i = 0; i < bulletArray.Size(); i++)
			{
				if (decodeBullet(state, bulletArray[i]) == -1)
				{
					printf("\n----------\nError decoding bullets\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * BASES
		 */
		if (strcmp(itr->name.GetString(), "Bases") == 0)
		{
			// If Bases is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Bases array is not array\n----------\n");
				return NULL;
			}

			const Value& basesArray = itr->value;
			for (SizeType i = 0; i < basesArray.Size(); i++)
			{
				if (decodeBase(state, basesArray[i]) == -1)
				{
					printf("\n----------\nError decoding bases\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * SHIELDS
		 */
		if (strcmp(itr->name.GetString(), "Shields") == 0)
		{
			// If Shields is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Shields array is not array\n----------\n");
				return NULL;
			}

			const Value& shieldsArray = itr->value;
			for (SizeType i = 0; i < shieldsArray.Size(); i++)
			{
				if (decodeShield(state, shieldsArray[i]) == -1)
				{
					printf("\n----------\nError decoding shields\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * SHIELD GENERATORS
		 */
		if (strcmp(itr->name.GetString(), "ShieldGenerators") == 0)
		{
			// If ShieldGenerators is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Shield gens array is not array\n----------\n");
				return NULL;
			}

			const Value& shieldGenArray = itr->value;
			for (SizeType i = 0; i < shieldGenArray.Size(); i++)
			{
				if (decodeShieldGen(state, shieldGenArray[i]) == -1)
				{
					printf("\n----------\nError decoding shield gens\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * POWERUPS
		 */
		if (strcmp(itr->name.GetString(), "PowerUps") == 0)
		{
			// If Powerups is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Powerups array is not array\n----------\n");
				return NULL;
			}

			const Value& powerupArray = itr->value;
			for (SizeType i = 0; i < powerupArray.Size(); i++)
			{
				if (decodePowerup(state, powerupArray[i]) == -1)
				{
					printf("\n----------\nError decoding powerups\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * ROCKETS
		 */
		if (strcmp(itr->name.GetString(), "Rockets") == 0)
		{
			// If Rockets is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: Rockets array is not array\n----------\n");
				return NULL;
			}

			const Value& rocketArray = itr->value;
			for (SizeType i = 0; i < rocketArray.Size(); i++)
			{
				if (decodeRocket(state, rocketArray[i]) == -1)
				{
					printf("\n----------\nError decoding rockets\n----------\n");
					return NULL;
				}
			}
		}

		/*
		 * GRAVITY WELLS
		 */
		if (strcmp(itr->name.GetString(), "GravityWells") == 0)
		{
			// If GravityWells is not an array, something went wrong
			if (!itr->value.IsArray())
			{
				printf("\n----------\nError: GravityWells array is not array\n----------\n");
				return NULL;
			}

			const Value& gravWellArray = itr->value;
			for (SizeType i = 0; i < gravWellArray.Size(); i++)
			{
				if (decodeGravWell(state, gravWellArray[i]) == -1)
				{
					printf("\n----------\nError decoding gravity wells\n----------\n");
					return NULL;
				}
			}
		}
	}

	return state;
}
示例#7
0
 EReturn Initialiser::initialiseProblemJSON(PlanningProblem_ptr problem,
     const std::string& constraints)
 {
   {
     Document document;
     if (!document.Parse<0>(constraints.c_str()).HasParseError())
     {
       if (ok(problem->reinitialise(document, problem)))
       {
         // Everythinh is fine
       }
       else
       {
         INDICATE_FAILURE
         ;
         return FAILURE;
       }
     }
     else
     {
       ERROR(
           "Can't parse constraints from JSON string!\n"<<document.GetParseError() <<"\n"<<constraints.substr(document.GetErrorOffset(),50));
       return FAILURE;
     }
   }
   return SUCCESS;
 }
示例#8
0
bool GateConfig::load(const char* jsonConfig)
{
	char *json = filetool::get_whole_file_buf(jsonConfig);
	if (NULL == json) {
		return false;
	}

	Document doc;

	if (doc.ParseInsitu(json).HasParseError()) {
		LOG_ERROR << "parse config<" << jsonConfig << "> failed, error code = " << doc.GetParseError() << ", error offset = " << doc.GetErrorOffset()
		          << ", err text = " << json[doc.GetErrorOffset()];

		delete[] json;
		return false;
	}

	if (!doc.IsObject()) {
		LOG_ERROR;
		return false;
	}

	{
		// ¶ÁÈ¡ÄÚÍøÍøÂçÅäÖÃ
		const Value& wan = doc["wan"];
		const Value& wanListen = wan["listen"];

		m_wanListen.ip = wanListen["ip"].GetString();
		m_wanListen.port = wanListen["port"].GetInt();

		m_wanThreadNum = wan["threads"].GetInt();
	}

	{
		// ¶ÁÈ¡ÍâÍøÍøÂçÅäÖÃ
		const Value& lan = doc["lan"];
		const Value& lanListens = lan["listen"];
		const Value& lanConnects = lan["listen"];

		IpPort ipport;
		for (SizeType i = 0; i < lanListens.Size(); i++) {
			const Value &listen = lanListens[i];

			ipport.ip = listen["ip"].GetString();
			ipport.port = listen["port"].GetInt();

			m_lanListens.push_back(ipport);
		}

		for (SizeType i = 0; i < lanConnects.Size(); i++) {
			const Value &connect = lanConnects[i];

			ipport.ip = connect["ip"].GetString();
			ipport.port = connect["port"].GetInt();

			m_lanConnects.push_back(ipport);
		}

		m_lanThreadNum = lan["threads"].GetInt();
	}

	delete[] json;
	return true;
}