Beispiel #1
0
SEXP create(const json::Array& value, Protect* pProtect)
{
   // create the list
   SEXP listSEXP;
   pProtect->add(listSEXP = Rf_allocVector(VECSXP, value.size()));
   
   // add each array element to it
   for (json::Array::size_type i=0; i<value.size(); i++)
   {
      SEXP valueSEXP = create(value[i], pProtect);
      SET_VECTOR_ELT(listSEXP, i,  valueSEXP);
   }
   return listSEXP;
}
Beispiel #2
0
	//------------------------------------------------------------------------------------------------------------------
	bool Serializer::push(const Json::Array& _array, ostream& _oStream, size_t _tab) {
		_oStream << "[\n"; // Open braces
		// Push elements
		for(size_t i = 0; i < _array.size(); ++i) {
			if(!push(*_array[i], _oStream, _tab+1))
				return false; // Error processing element
			if(i != _array.size()-1) // All elements but the last one
				_oStream << ',';
			_oStream << '\n';
		}
		// Close braces
		tabify(_oStream, _tab);
		_oStream << ']';
		return true;
	}
Beispiel #3
0
inline core::Error readParam(const json::Array& params, 
                             unsigned int index, 
                             json::Value* pValue)
{
   if (index >= params.size())
      return core::Error(errc::ParamMissing, ERROR_LOCATION);
   
   *pValue = params[index] ;
   return Success();
}
Error setChunkDefs(const std::string& docPath, const std::string& docId,
                   std::time_t docTime, const json::Array& newDefs)
{
   // create JSON object wrapping 
   json::Object chunkDefs;
   chunkDefs[kChunkDefs] = newDefs;
   chunkDefs[kChunkDocWriteTime] = static_cast<boost::int64_t>(docTime);

   // ensure we have a place to write the sidecar file
   FilePath defFile = chunkDefinitionsPath(docPath, docId, 
         notebookCtxId());

   // if there are no old chunk definitions and we aren't adding any new ones,
   // no work to do
   if (!defFile.exists() && newDefs.size() < 1) 
      return Success();

   // we're going to write something; make sure the parent folder exists
   Error error = defFile.parent().ensureDirectory();
   if (error)
      return error;

   // get the old set of chunk IDs so we can clean up any not in the new set 
   // of chunks
   std::vector<std::string> chunkIds;
   json::Value oldDefs;
   std::string oldContent;
   error = getChunkDefs(docPath, docId, notebookCtxId(), NULL, 
         &oldDefs);
   if (error)
      LOG_ERROR(error);
   else if (oldDefs.type() == json::ArrayType)
   {
      if (oldDefs.get_array() == newDefs) 
      {
         // definitions not changing; no work to do
         return Success();
      }
      cleanChunks(chunkCacheFolder(docPath, docId),
                  oldDefs.get_array(), newDefs);
   }

   std::ostringstream oss;
   json::write(chunkDefs, oss);

   error = writeStringToFile(defFile, oss.str());
   if (error)
   {
      LOG_ERROR(error);
      return error;
   }
   
   return Success();
}
Beispiel #5
0
core::Error readParam(const json::Array& params, unsigned int index, T* pValue)
{
   if (index >= params.size())
      return core::Error(errc::ParamMissing, ERROR_LOCATION);

   if (!isType<T>(params[index]))
      return core::Error(errc::ParamTypeMismatch, ERROR_LOCATION) ;

   *pValue = params[index].get_value<T>();

   return Success() ;
}
Beispiel #6
0
std::unique_ptr<OptionValue> ConfigVisitor::ReadArray(json::Array const& src, std::string const& array_type, void (OptionValueType::*)(const std::vector<ValueType>&)) {
	std::vector<ValueType> arr;
	arr.reserve(src.size());

	for (json::Object const& obj : src) {
		if (obj.size() != 1) {
			Error<OptionJsonValueArray>("Invalid array member");
			return 0;
		}
		if (obj.begin()->first != array_type) {
			Error<OptionJsonValueArray>("Attempt to insert value into array of wrong type");
			return 0;
		}

		arr.push_back(ValueType(obj.begin()->second));
	}

	return util::make_unique<OptionValueType>(name, arr);
}
Beispiel #7
0
void Scene::parseObjects(const json::Array &arr) {
  for (int i = 0; i < arr.size(); i++) {
    json::Object o = arr[i].get_obj();
    string type = get_str(o, "type");

    // create an object based on the object string
    if (type == "plane") {
      int idx = getMaterialIdx(get_str(o, "material"));
      Vector n = get_vector(get_value(o, "normal").get_array());
      float dist = get_float(o, "distance");
      m_shapes.push_back(new Plane(idx, n, dist));      
    }
    if (type == "sphere") {
      Vector v = get_vector(get_value(o, "center").get_array());
      float rad = get_float(o, "radius");
      int idx = getMaterialIdx(get_str(o, "material"));

      m_shapes.push_back(new Sphere(idx, v, rad));
    }
  }
}
Beispiel #8
0
bool Item::doProcessSearchResult(const QByteArray &response, int &newPagesCount)
{
  QByteArray resp = response;
  QBuffer buffer(&resp);

  buffer.open(QIODevice::ReadOnly);

  try
  {
    JSON::Reader reader(&buffer);

    JSON::Object *rootObject, *object;
    JSON::Array *array;
    JSON::Value *value;

    rootObject = dynamic_cast<JSON::Object *> (reader.element());

    if (!rootObject)
    {
      qDebug() << metaObject()->className() << ": cannot find root object";
      return false;
    }

    if (( (value = dynamic_cast<JSON::Value *> (rootObject->value("responseDetails"))) ) &&
        !value->toString().compare("out of range start"))
    {
      newPagesCount = 1;
      return false;
    }

    rootObject = dynamic_cast<JSON::Object *> (rootObject->value("responseData"));

    if (!rootObject)
    {
      qDebug() << metaObject()->className() << ": cannot find responseData object";
      return false;
    }

    array = dynamic_cast<JSON::Array *> (rootObject->value("results"));

    if (!array || !array->size())
    {
      qDebug() << metaObject()->className() << ": cannot find photos array";
      return false;
    }

    object = dynamic_cast<JSON::Object *> (array->at(0));

    if (!object)
    {
      qDebug() << metaObject()->className() << ": cannot find object inside array";
      return false;
    }

    value = dynamic_cast<JSON::Value *> (object->value("imageId"));
    if (value)
      _photoId = value->toString();
    else
      qDebug() << metaObject()->className() << ": cannot find imageId";

    value = dynamic_cast<JSON::Value *> (object->value("url"));
    if (value)
      _photoUrl = value->toString();
    else
    {
      value = dynamic_cast<JSON::Value *> (object->value("unescapedUrl"));
      if (value)
        _photoUrl = value->toString();
      else
        qDebug() << metaObject()->className() << ": cannot find url/unescapedUrl";
    }

    value = dynamic_cast<JSON::Value *> (object->value("originalContextUrl"));
    if (value)
      _sourceUrl = value->toString();
    else
      qDebug() << metaObject()->className() << ": cannot find originalContextUrl";

    value = dynamic_cast<JSON::Value *> (object->value("title"));
    if (value)
      photoTitle = value->toString();
    else
    {
      value = dynamic_cast<JSON::Value *> (object->value("titleNoFormatting"));
      if (value)
        photoTitle = value->toString();
      else
        qDebug() << metaObject()->className() << ": cannot find title/titleNoFormatting";
    }

    value = dynamic_cast<JSON::Value *> (object->value("width"));
    if (value)
      photoSize.setWidth(value->toInt());
    else
      qDebug() << metaObject()->className() << ": cannot find width";

    value = dynamic_cast<JSON::Value *> (object->value("height"));
    if (value)
      photoSize.setHeight(value->toInt());
    else
      qDebug() << metaObject()->className() << ": cannot find height";

    value = dynamic_cast<JSON::Value *> (object->value("content"));
    if (value)
      photoDescription = value->toString();
    else
    {
      value = dynamic_cast<JSON::Value *> (object->value("contentNoFormatting"));
      if (value)
        photoDescription = value->toString();
      else
        qDebug() << metaObject()->className() << ": cannot find content/contentNoFormatting";
    }

    object = dynamic_cast<JSON::Object *> (rootObject->value("cursor"));
    value = dynamic_cast<JSON::Value *> (object->value("estimatedResultCount"));
    newPagesCount = value->toInt();
  }
  catch (const QString &message)
  {
    qDebug() << message;
  }

  return (!_photoId.isEmpty() && !_lastPhotoIds.contains(_photoId));
}
Beispiel #9
0
static Vector get_vector(const json::Array &a) {
  assert(a.size() == 3);

  return Vector(a[0].get_real(), a[1].get_real(), a[2].get_real());
}
Beispiel #10
0
static void foreach_exif_entry( ExifEntry * entry , void * _closure )
{
	if ( ! entry )
	{
		return;
	}

	//.........................................................................
	// Bail out of types we don't handle

	switch( entry->format )
	{
	case EXIF_FORMAT_UNDEFINED:
	case EXIF_FORMAT_FLOAT:
	case EXIF_FORMAT_DOUBLE:
		return;
	default:
		break;
	}

	//.........................................................................

	unsigned char component_size = exif_format_get_size( entry->format );

	ExifIfd ifd = exif_content_get_ifd( entry->parent );

	const char * tag_name = exif_tag_get_name_in_ifd( entry->tag , ifd );

	if ( ! tag_name || ! entry->data || ! entry->size || ! component_size || ! entry->components )
	{
		return;
	}

	//.........................................................................
	// Add a prefix based on the IFD

	String name( tag_name );

	switch( ifd )
	{
	case EXIF_IFD_0:
		name = "IMAGE/" + name;
		break;
	case EXIF_IFD_1:
		name = "THUMBNAIL/" + name;
		break;
	case EXIF_IFD_EXIF:
		name = "EXIF/" + name;
		break;
	case EXIF_IFD_GPS:
		name = "GPS/" + name;
		break;
	case EXIF_IFD_INTEROPERABILITY:
		name = "INTEROP/" + name;
		break;
	default:
		return;
	}

	ExifClosure * closure = ( ExifClosure * ) _closure;

	JSON::Object * tags = closure->tags;

	//.........................................................................
	// ASCII ones are easy

	if ( entry->format == EXIF_FORMAT_ASCII )
	{
		(*tags)[ name ] = String( ( const char * ) entry->data , entry->size );
		return;
	}

	//.........................................................................

	if ( ( entry->components * component_size ) != entry->size )
	{
		return;
	}

	ExifByteOrder byte_order = exif_data_get_byte_order( closure->exif_data );

	const unsigned char * data = entry->data;

	JSON::Array array;

	for ( unsigned long i = 0; i < entry->components; ++i )
	{
		switch( entry->format )
		{
		case EXIF_FORMAT_BYTE:
			array.append( JSON::Value( int( * data ) ) );
			break;

		case EXIF_FORMAT_SHORT:
			array.append( JSON::Value( int( exif_get_short( data , byte_order ) ) ) );
			break;

		case EXIF_FORMAT_LONG:
			array.append( JSON::Value( int( exif_get_long( data , byte_order ) ) ) );
			break;

		case EXIF_FORMAT_SBYTE:
			array.append( JSON::Value( int( * ( ( const char * ) data ) ) ) );
			break;

		case EXIF_FORMAT_SSHORT:
			array.append( JSON::Value( exif_get_sshort( data , byte_order ) ) );
			break;

		case EXIF_FORMAT_SLONG:
			array.append( JSON::Value( exif_get_slong( data , byte_order ) ) );
			break;

		// TODO: I don't like representing a rational number as a string with a slash,

		case EXIF_FORMAT_SRATIONAL:
		{
			ExifSRational r = exif_get_srational( data , byte_order );
			array.append( Util::format("%ld/%ld" , r.numerator , r.denominator ) );
			break;
		}

		case EXIF_FORMAT_RATIONAL:
		{
			ExifRational r = exif_get_rational( data , byte_order );
			array.append( Util::format("%lu/%lu" , r.numerator , r.denominator ) );
			break;
		}
		default:
			break;
		}

		data += component_size;
	}

	if ( array.size() == 1 )
	{
		(*tags)[ name ] = array[ 0 ];
	}
	else if ( array.size() > 1 )
	{
		(*tags)[ name ] = array;
	}
}