Пример #1
0
TYPED_TEST(DocumentMove, MoveAssignment) {
    typedef TypeParam Allocator;
    typedef GenericDocument<UTF8<>, Allocator> Document;
    Allocator allocator;

    Document a(&allocator);
    a.Parse("[\"one\", \"two\", \"three\"]");
    EXPECT_FALSE(a.HasParseError());
    EXPECT_TRUE(a.IsArray());
    EXPECT_EQ(3u, a.Size());
    EXPECT_EQ(&a.GetAllocator(), &allocator);

    // Document b; b = a; // does not compile (!is_copy_assignable)
    Document b;
    b = std::move(a);
    EXPECT_TRUE(a.IsNull());
    EXPECT_TRUE(b.IsArray());
    EXPECT_EQ(3u, b.Size());
    EXPECT_EQ(&a.GetAllocator(), (void*)0);
    EXPECT_EQ(&b.GetAllocator(), &allocator);

    b.Parse("{\"Foo\": \"Bar\", \"Baz\": 42}");
    EXPECT_FALSE(b.HasParseError());
    EXPECT_TRUE(b.IsObject());
    EXPECT_EQ(2u, b.MemberCount());

    // Document c; c = a; // does not compile (see static_assert)
    Document c;
    c = std::move(b);
    EXPECT_TRUE(b.IsNull());
    EXPECT_TRUE(c.IsObject());
    EXPECT_EQ(2u, c.MemberCount());
    EXPECT_EQ(&b.GetAllocator(), (void*)0);
    EXPECT_EQ(&c.GetAllocator(), &allocator);
}
/* ****************************************************************************
*
* parseAttributeValue - 
*/
std::string parseAttributeValue(ConnectionInfo* ciP, ContextAttribute* caP)
{
  Document    document;
  OrionError  oe;

  document.Parse(ciP->payload);

  if (document.HasParseError())
  {
    alarmMgr.badInput(clientIp, "JSON parse error");
    oe.reasonPhrase = ERROR_STRING_PARSERROR;
    oe.details      = "Errors found in incoming JSON buffer";
    ciP->httpStatusCode = SccBadRequest;;
    return oe.render(ciP, "");
  }


  if (!document.IsObject() && !document.IsArray())
  {
    alarmMgr.badInput(clientIp, "JSON parse error");
    oe.fill(SccBadRequest, "Neither JSON Object nor JSON Array for attribute::value");
    ciP->httpStatusCode = SccBadRequest;;

    return oe.render(ciP, "");
  }

  caP->valueType  = (document.IsObject())? orion::ValueTypeObject : orion::ValueTypeVector;
  parseContextAttributeCompoundValueStandAlone(document, caP, caP->valueType);

  return "OK";
}
void SistemaTransportePublico::carregaContornos(int linha, string arquivo) {

	// valida parametros
	if ((arquivo.size() == 0) || (linha < 0))
	{
		log->LOG("parametros invalidos.");
		return;
	}

	// verifica se arquivo existe
	std::ifstream t (arquivo.c_str());
	if (!t)
	{
		log->LOG("arquivo nao existe");
		return;
	}

	// transforma arquivo de ifstream para string
	std::string str;
	t.seekg(0, std::ios::end);
	str.reserve(t.tellg());
	t.seekg(0, std::ios::beg);

	str.assign((std::istreambuf_iterator<char>(t)),
			std::istreambuf_iterator<char>());

	if (str.length() == 0)
	{
		log->LOG("arquivo vazio");
		return;
	}
	// parse de string para JSON
	Document d;
	d.Parse(str.c_str());
	assert(d.IsArray());
	for (SizeType i = 0; i < d.Size(); i++)
	{
		assert(d[i].IsObject());
		assert(d[i].HasMember(JSON_PONTO_LATITUDE));
		assert(d[i][JSON_PONTO_LATITUDE].IsString());
		assert(d[i].HasMember(JSON_PONTO_LONGITUDE));
		assert(d[i][JSON_PONTO_LONGITUDE].IsString());

		string lat = d[i][JSON_PONTO_LATITUDE].GetString();
		lat[3]='.';
		string lon = d[i][JSON_PONTO_LONGITUDE].GetString();
		lon[3]='.';

		Coordenada c (lat, lon);

		//log->debug("coord: " + lat + ", " + lon );
		insereContorno (linha, c);
	}
	log->LOG("contornos inseridos: " + convert::intToString(d.Size()));
}
    virtual bool ParseString(const char* json, std::string& s) const {
        Document doc;
#ifdef TEST_INSITU
        RapidjsonParseResult pr(json, strlen(json));
        doc.ParseInsitu<TEST_PARSE_FLAG>(pr.buffer);
#else
        doc.Parse<TEST_PARSE_FLAG>(json);
#endif
        if (!doc.HasParseError() && doc.IsArray() && doc.Size() == 1 && doc[0].IsString()) {
            s = std::string(doc[0].GetString(), doc[0].GetStringLength());
            return true;
        }
        return false;
    }
    virtual bool ParseDouble(const char* json, double* d) const {
        Document doc;
#ifdef TEST_INSITU
        RapidjsonParseResult pr(json, strlen(json));
        doc.ParseInsitu<TEST_PARSE_FLAG>(pr.buffer);
#else
        doc.Parse<TEST_PARSE_FLAG>(json);
#endif
        if (!doc.HasParseError() && doc.IsArray() && doc.Size() == 1 && doc[0].IsNumber()) {
            *d = doc[0].GetDouble();
            return true;
        }
        return false;
    }
void SistemaTransportePublico::carregaLinhas(string arquivo) {

	// valida parametro
	if (arquivo.size() == 0)
		return;

	log->LOG("carregando arquivo de linhas: " + arquivo);

	// verifica se arquivo existe
	std::ifstream t (arquivo.c_str());
	if (!t)
	{
		log->LOG("arquivo nao existe");
		return;
	}

	// transforma arquivo de ifstream para string
	std::string str;
	t.seekg(0, std::ios::end);
	str.reserve(t.tellg());
	t.seekg(0, std::ios::beg);

	str.assign((std::istreambuf_iterator<char>(t)),
	            std::istreambuf_iterator<char>());

	// parse de string para JSON
	Document d;
	d.Parse(str.c_str());
	assert(d.IsArray());
	cout << d.Size() << endl;
	for (SizeType i = 0; i < d.Size(); i++)
	{
		assert(d[i].IsObject());
		assert(d[i].HasMember(JSON_LINHA_CODIGO));
		assert(d[i][JSON_LINHA_CODIGO].IsString());
		assert(d[i].HasMember(JSON_LINHA_NOME));
		assert(d[i][JSON_LINHA_NOME].IsString());
		Linha *l = new Linha (d[i][JSON_LINHA_CODIGO].GetString(),
				d[i][JSON_LINHA_NOME].GetString());

		log->LOG("cod: " + string(d[i][JSON_LINHA_CODIGO].GetString()) + " nome: " +
				string(d[i][JSON_LINHA_NOME].GetString()));

		insereLinha (l);
	}
	log->LOG("linhas inseridas: "  + convert::intToString(d.Size()));
}
Пример #7
0
    void Roi::loadTags(string host) {
        char url[host.length() + 17 + 10];
        sprintf(url, "http://%s/roi/%u/tags", host.c_str(), id);
        const char* tagJSON = curlGet(url).c_str();

        // Parse metadata
        Document document;
        if(document.Parse<0>(tagJSON).HasParseError() || !document.IsArray()) {
            // Failed to parse
            fprintf(stderr, "Error Parsing metadata JSON");
            return;
        }

        for(SizeType i = 0; i < document.Size(); i++) {
            tags.push_back(document[i].GetUint());
        }
    }
Пример #8
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);
}
Пример #9
0
std::vector<name_t> nameArrayFromData(const char *jsonData, size_t len) {

    std::vector<char> buffer(len + 1);

    std::memcpy(&buffer[0], jsonData, len);

    Document doc;

    doc.ParseInsitu(&buffer[0]);

    assert(doc.IsArray());

    std::vector<name_t> nameArray;
    nameArray.reserve(doc.Size());

    for( auto array_item = doc.Begin(); array_item != doc.End(); array_item++  ) {

        name_t instance = name_t(*array_item);
        nameArray.push_back(instance);
    }

    return nameArray;
}
Пример #10
0
    void LabeledImage::loadRois(string host) {
        // URL: host/image/id/rois
        rois.clear();

        char url[host.length() + 19 + 10];
        sprintf(url, "http://%s/image/%u/rois", host.c_str(), id);
        const char* roiJSON = curlGet(url).c_str();

        // Parse ROIs
        Document document;
        if(document.Parse<0>(roiJSON).HasParseError() || !document.IsArray()) {
            fprintf(stderr, "Error Parsing ROI JSON");
            return;
        }

        for(uint32_t i = 0; i < document.Size(); i++) {
            // Build an ROI
            Roi *roi = new Roi();
            roi->loadFromDocument(document[i]);
            rois.push_back(roi);
        }


    }
void SistemaTransportePublico::carregaRotas(string linha, string arquivo) {

	// valida parametros
	if ((arquivo.size() == 0) || (linha.size() == 0))
	{
		log->LOG("parametros vazios.");
		return;
	}

	log->LOG("carregando arquivo de pontos: " + arquivo +
			" da linha " + linha);

	// verifica se arquivo existe
	std::ifstream t (arquivo.c_str());
	if (!t)
	{
		log->LOG("arquivo nao existe");
		return;
	}

	// transforma arquivo de ifstream para string
	std::string str;
	t.seekg(0, std::ios::end);
	str.reserve(t.tellg());
	t.seekg(0, std::ios::beg);

	str.assign((std::istreambuf_iterator<char>(t)),
			std::istreambuf_iterator<char>());

	if (str.length() == 0)
	{
		log->LOG("arquivo vazio");
		return;
	}
	// parse de string para JSON
	Document d;
	d.Parse(str.c_str());
	assert(d.IsArray());
	cout << d.Size() << endl;
	for (SizeType i = 0; i < d.Size(); i++)
	{
		assert(d[i].IsObject());
		assert(d[i].HasMember(JSON_PONTO_NOME));
		assert(d[i][JSON_PONTO_NOME].IsString());
		assert(d[i].HasMember(JSON_PONTO_LATITUDE));
		assert(d[i][JSON_PONTO_LATITUDE].IsString());
		assert(d[i].HasMember(JSON_PONTO_LONGITUDE));
		assert(d[i][JSON_PONTO_LONGITUDE].IsString());
		assert(d[i].HasMember(JSON_PONTO_TIPO));
		assert(d[i][JSON_PONTO_TIPO].IsString());
		assert(d[i].HasMember(JSON_PONTO_NUMERO));
		assert(d[i][JSON_PONTO_NUMERO].IsString());
		assert(d[i].HasMember(JSON_PONTO_SENTIDO));
		assert(d[i][JSON_PONTO_SENTIDO].IsString());
		assert(d[i].HasMember(JSON_PONTO_SEQUENCIA));
		assert(d[i][JSON_PONTO_SEQUENCIA].IsString());

		string lat = d[i][JSON_PONTO_LATITUDE].GetString();
		lat[3]='.';
		string lon = d[i][JSON_PONTO_LONGITUDE].GetString();
		lon[3]='.';

		Coordenada c (lat, lon);

		PontoLinha *p = new PontoLinha(
				d[i][JSON_PONTO_NOME].GetString(),
				d[i][JSON_PONTO_NUMERO].GetString(),
				d[i][JSON_PONTO_TIPO].GetString(),
				d[i][JSON_PONTO_SENTIDO].GetString(),
				0,
				0,
				c
		);

		cout << "ponto: " << d[i][JSON_PONTO_NOME].GetString()
									<< " lat " << lat
									<< " lon " << lon
									<< endl;

		inserePontoLinha (linha, p);
	}
	log->LOG("pontos inseridos:"  + convert::intToString(d.Size()));
}
void SistemaTransportePublico::carregaArquivoVeiculos(string arquivo) {

	// verifica se arquivo existe
	std::ifstream t (arquivo.c_str());
	if (!t)
	{
		log->LOG("arquivo nao existe");
		return;
	}

	int ind = arquivo.find("veiculoslinhas-");
	if (ind < 0)
	{
		log->LOG("arquivo invalido de posicoes");
		return;
	}

	// extrai data e hora
	ind = arquivo.find("--")+2;
	string data = arquivo.substr(ind, 8);
//	ind = arquivo.find(".")-6;
//	string hora = arquivo.substr(ind, 6);

	// transforma arquivo de ifstream para string
	std::string str;
	t.seekg(0, std::ios::end);
	str.reserve(t.tellg());
	t.seekg(0, std::ios::beg);

	str.assign((std::istreambuf_iterator<char>(t)),
	            std::istreambuf_iterator<char>());

	// parse de string para JSON
	Document d;
	d.Parse(str.c_str());
	assert(d.IsArray());
	cout << d.Size() << endl;
	for (SizeType i = 0; i < d.Size(); i++)
	{
		assert(d[i].IsObject());
		assert(d[i].HasMember(JSON_VEICULO_LINHA));
		assert(d[i][JSON_VEICULO_LINHA].IsString());
		assert(d[i].HasMember(JSON_VEICULO_PREFIXO));
		assert(d[i][JSON_VEICULO_PREFIXO].IsString());
		assert(d[i].HasMember(JSON_VEICULO_LAT));
		assert(d[i][JSON_VEICULO_LAT].IsString());
		assert(d[i].HasMember(JSON_VEICULO_LONG));
		assert(d[i][JSON_VEICULO_LONG].IsString());
		assert(d[i].HasMember(JSON_VEICULO_HORA));
		assert(d[i][JSON_VEICULO_HORA].IsString());
//		assert(d[i].HasMember(JSON_VEICULO_ADAP));
//		assert(d[i][JSON_VEICULO_ADAP].IsString());

		string lat = d[i][JSON_PONTO_LATITUDE].GetString();
		lat[3]='.';
		string lon = d[i][JSON_PONTO_LONGITUDE].GetString();
		lon[3]='.';

		Coordenada c (lat, lon);

		LocalVeiculo *l = new LocalVeiculo (
				d[i][JSON_VEICULO_PREFIXO].GetString(),
				data,
				d[i][JSON_VEICULO_HORA].GetString(),
				c
				);

		inserePosicaoVeiculo (d[i][JSON_VEICULO_LINHA].GetString(),
				l);
	}
	log->LOG("posicoes de veiculos inseridas: "  + convert::intToString(d.Size()));
}