예제 #1
0
int64_t GetJsonlonglong(const Json::Value& _jsValue)
{
	return (_jsValue.type() == Json::realValue||
		_jsValue.type() == Json::intValue||
		_jsValue.type() == Json::uintValue) ? static_cast<int64_t>(_jsValue.asDouble()):0;
}
예제 #2
0
파일: json.cpp 프로젝트: imerr/SFMLEngine
	double jsonNodeAs<double>(const Json::Value& node) {
		if (node.isNull()) {
			return 0;
		}
		return node.asDouble();
	}
예제 #3
0
double GetJsonDouble(const Json::Value& _jsValue)
{
	return (_jsValue.type() == Json::realValue||
		_jsValue.type() == Json::intValue||
		_jsValue.type() == Json::uintValue) ? _jsValue.asDouble():0;
}
예제 #4
0
void VerifyOptionf(Json::Value &Option, const float &Default, const float &Lower, const float &Upper)
{
  if(Option.asDouble() < Lower || Option.asDouble() > Upper)
    Option = Default;
}
예제 #5
0
파일: pb2json.cpp 프로젝트: shewang/PB2Json
    int ToPbSingle(const Json::Value &value, const FieldDescriptor *pFieldDescriptor, Message &message, map<string, string>& key_map)
    {
        if (!check_type(value.type(), pFieldDescriptor->cpp_type()))
        {
            return 1;
        }

        const Reflection *pReflection = message.GetReflection();
        EnumDescriptor      *pEnumDes = NULL;
        EnumValueDescriptor *pEnumValueDes = NULL;
        int ret = 0;
        switch(pFieldDescriptor->cpp_type())
        {
            case FieldDescriptor::CPPTYPE_INT32:
            {
                pReflection->SetInt32(&message, pFieldDescriptor, value.asInt());
                break;
            }
            case FieldDescriptor::CPPTYPE_UINT32:
            {
                pReflection->SetUInt32(&message, pFieldDescriptor, value.asUInt());
                break;
            }
            case FieldDescriptor::CPPTYPE_INT64:
            {
                pReflection->SetInt64(&message, pFieldDescriptor, value.asInt64());
                break;
            }
            case FieldDescriptor::CPPTYPE_UINT64:
            {
                pReflection->SetUInt64(&message, pFieldDescriptor, value.asUInt64());
                break;
            }
            case FieldDescriptor::CPPTYPE_STRING:
            {
                pReflection->SetString(&message, pFieldDescriptor, value.asString());
                break;
            }
            case FieldDescriptor::CPPTYPE_BOOL:
            {
                pReflection->SetBool(&message, pFieldDescriptor, value.asBool());
                break;
            }
            case FieldDescriptor::CPPTYPE_DOUBLE:
            {
                pReflection->SetDouble(&message, pFieldDescriptor, value.asDouble());
                break;
            }
            case FieldDescriptor::CPPTYPE_FLOAT:
            {
                pReflection->SetFloat(&message, pFieldDescriptor, value.asFloat());
                break;
            }
            case FieldDescriptor::CPPTYPE_ENUM:
            {
                if ((pEnumDes = (EnumDescriptor *)pFieldDescriptor->enum_type()) == NULL)
                {
                    return 1;
                }

                if ((pEnumValueDes = (EnumValueDescriptor *)pEnumDes->FindValueByNumber(value.asInt())) == NULL)
                {
                    return 1;
                }

                pReflection->SetEnum(&message, pFieldDescriptor, pEnumValueDes);
                break;
            }
            case FieldDescriptor::CPPTYPE_MESSAGE:
            {
                Message *pmessage = pReflection->MutableMessage(&message, pFieldDescriptor);
                if (key_map.size() == 0)
                {
                    ret = ToPb(*pmessage, value);
                }
                else
                {
                    ret = ToPbMap(*pmessage, value, key_map);
                }

                break;
            }
            default:
            {
                ret = 1;
                break;
            }
        }

        return ret;
    }
예제 #6
0
  void LuaContext::PushJson(const Json::Value& value)
  {
    if (value.isString())
    {
      const std::string s = value.asString();
      lua_pushlstring(lua_, s.c_str(), s.size());
    }
    else if (value.isDouble())
    {
      lua_pushnumber(lua_, value.asDouble());
    }
    else if (value.isInt())
    {
      lua_pushinteger(lua_, value.asInt());
    }
    else if (value.isUInt())
    {
      lua_pushinteger(lua_, value.asUInt());
    }
    else if (value.isBool())
    {
      lua_pushboolean(lua_, value.asBool());
    }
    else if (value.isNull())
    {
      lua_pushnil(lua_);
    }
    else if (value.isArray())
    {
      lua_newtable(lua_);

      // http://lua-users.org/wiki/SimpleLuaApiExample
      for (Json::Value::ArrayIndex i = 0; i < value.size(); i++)
      {
        // Push the table index (note the "+1" because of Lua conventions)
        lua_pushnumber(lua_, i + 1);

        // Push the value of the cell
        PushJson(value[i]);

        // Stores the pair in the table
        lua_rawset(lua_, -3);
      }
    }
    else if (value.isObject())
    {
      lua_newtable(lua_);

      Json::Value::Members members = value.getMemberNames();

      for (Json::Value::Members::const_iterator 
             it = members.begin(); it != members.end(); ++it)
      {
        // Push the index of the cell
        lua_pushlstring(lua_, it->c_str(), it->size());

        // Push the value of the cell
        PushJson(value[*it]);

        // Stores the pair in the table
        lua_rawset(lua_, -3);
      }
    }
    else
    {
      throw OrthancException(ErrorCode_JsonToLuaTable);
    }
  }
예제 #7
0
	void Deserialize_Double( const Json::Value& srcValue, F8 &dstValue )
	{
		CHK_VRET_IF_NOT( srcValue.isDouble() );
		dstValue = srcValue.asDouble();
	}
std::string GlobalizationNDK::numberToString(const std::string& args)
{
    if (args.empty()) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: no arguments provided!");
        return errorInJson(UNKNOWN_ERROR, "No arguments provided!");
    }

    Json::Reader reader;
    Json::Value root;
    bool parse = reader.parse(args, root);

    if (!parse) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: invalid json data: %s",
                args.c_str());
        return errorInJson(PARSING_ERROR, "Invalid json data!");
    }

    Json::Value nv = root["number"];
    if (nv.isNull()) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: no number provided!");
        return errorInJson(FORMATTING_ERROR, "No number provided!");
    }

    if (!nv.isNumeric()) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: invalid number type: %d!",
                nv.type());
        return errorInJson(FORMATTING_ERROR, "Invalid number type!");
    }

    // This is the default value when no options provided.
    ENumberType type = kNumberDecimal;

    Json::Value options = root["options"];
    std::string error;
    if (!handleNumberOptions(options, type, error))
        return errorInJson(PARSING_ERROR, error);

    UErrorCode status = U_ZERO_ERROR;
    NumberFormat* nf;
    switch (type) {
    case kNumberDecimal:
    default:
        nf = NumberFormat::createInstance(status);
        break;
    case kNumberCurrency:
        nf = NumberFormat::createCurrencyInstance(status);
        break;
    case kNumberPercent:
        nf = NumberFormat::createPercentInstance(status);
        break;
    }

    if (!nf) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::numberToString: failed to create NumberFormat instance for type %d: %d",
                status, type);
        return errorInJson(UNKNOWN_ERROR, "Failed to create NumberFormat instance!");
    }
    std::auto_ptr<NumberFormat> deleter(nf);

    UnicodeString result;
    nf->format(nv.asDouble(), result);
    std::string utf8;
    result.toUTF8String(utf8);

    return resultInJson(utf8);
}
예제 #9
0
static void
printValueTree(FILE* fout, Json::Value& value, const std::string& path = ".") {
  if (value.hasComment(Json::commentBefore)) {
    fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str());
  }

  switch (value.type()) {
  case Json::nullValue:
    fprintf(fout, "%s=null\n", path.c_str());
    break;
  case Json::intValue:
    fprintf(fout,
            "%s=%s\n",
            path.c_str(),
            Json::valueToString(value.asLargestInt()).c_str());
    break;
  case Json::uintValue:
    fprintf(fout,
            "%s=%s\n",
            path.c_str(),
            Json::valueToString(value.asLargestUInt()).c_str());
    break;
  case Json::realValue:
    fprintf(fout,
            "%s=%s\n",
            path.c_str(),
            normalizeFloatingPointStr(value.asDouble()).c_str());
    break;
  case Json::stringValue:
    fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
    break;
  case Json::booleanValue:
    fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
    break;
  case Json::arrayValue: {
    fprintf(fout, "%s=[]\n", path.c_str());
    int size = value.size();
    for (int index = 0; index < size; ++index) {
      static char buffer[16];
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
      sprintf_s(buffer, sizeof(buffer), "[%d]", index);
#else
      snprintf(buffer, sizeof(buffer), "[%d]", index);
#endif
      printValueTree(fout, value[index], path + buffer);
    }
  } break;
  case Json::objectValue: {
    fprintf(fout, "%s={}\n", path.c_str());
    Json::Value::Members members(value.getMemberNames());
    std::sort(members.begin(), members.end());
    std::string suffix = *(path.end() - 1) == '.' ? "" : ".";
    for (Json::Value::Members::iterator it = members.begin();
         it != members.end();
         ++it) {
      const std::string& name = *it;
      printValueTree(fout, value[name], path + suffix + name);
    }
  } break;
  default:
    break;
  }

  if (value.hasComment(Json::commentAfter)) {
    fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
  }
}
예제 #10
0
static bool SetSingleValueForMessage(const Reflection* reflection,
                                     Message* pb,
                                     const FieldDescriptor* field,
                                     const Json::Value& node) {
    switch (field->cpp_type()) {
    case FieldDescriptor::CPPTYPE_INT32:
        reflection->SetInt32(pb, field, node.asInt());
        break;
    case FieldDescriptor::CPPTYPE_INT64: {
        int64_t number = 0;
        if (!StringToInt64(node.asString(), &number)) {
            LOG(WARNING) << "Fail to convert to interger:" << node.asString();
            return false;
        }
        reflection->SetInt64(pb, field, number);
        break;
    }
    case FieldDescriptor::CPPTYPE_UINT32:
        reflection->SetUInt32(pb, field, node.asUInt());
        break;
    case FieldDescriptor::CPPTYPE_UINT64: {
        uint64_t number = 0;
        if (!StringToUint64(node.asString(), &number)) {
            LOG(WARNING) << "Fail to convert to interger:" << node.asString();
            return false;
        }
        reflection->SetUInt64(pb, field, number);
        break;
    }
    case FieldDescriptor::CPPTYPE_DOUBLE:
        reflection->SetDouble(pb, field, node.asDouble());
        break;
    case FieldDescriptor::CPPTYPE_FLOAT:
        reflection->SetFloat(pb, field, node.asDouble());
        break;
    case FieldDescriptor::CPPTYPE_BOOL:
        reflection->SetBool(pb, field, node.asBool());
        break;
    case FieldDescriptor::CPPTYPE_ENUM: {
        const EnumValueDescriptor* enum_value = reflection->GetEnum(*pb, field);
        const EnumDescriptor* enum_desc = enum_value->type();
        const EnumValueDescriptor* real_enum_value = enum_desc->FindValueByNumber(node.asInt());
        reflection->SetEnum(pb, field, real_enum_value);
        break;
    }
    case FieldDescriptor::CPPTYPE_STRING:
        reflection->SetString(pb, field, node.asString());
        break;
    case FieldDescriptor::CPPTYPE_MESSAGE: {
        Message* sub_pb = reflection->MutableMessage(pb, field);
        if (!ParseFromJsonValue(node, sub_pb)) {
            return false;
        }
        break;
    }
    default:
        CHECK(false) << "bad type:" << field->cpp_type();
        break;
    }
    return true;
}
예제 #11
0
static bool SetRepeatedValueForMessage(const Reflection* reflection,
                                       Message* pb,
                                       const FieldDescriptor* field,
                                       const Json::Value& sub_node) {
    switch (field->cpp_type()) {
    case FieldDescriptor::CPPTYPE_INT32:
        reflection->AddInt32(pb, field, sub_node.asInt());
        break;
    case FieldDescriptor::CPPTYPE_INT64: {
        int64_t number = 0;
        if (!StringToInt64(sub_node.asString(), &number)) {
            LOG(WARNING) << "Fail to convert to interger:" << sub_node.asString();
            return false;
        }
        reflection->AddInt64(pb, field, number);
        break;
    }
    case FieldDescriptor::CPPTYPE_UINT32:
        reflection->AddUInt32(pb, field, sub_node.asUInt());
        break;
    case FieldDescriptor::CPPTYPE_UINT64: {
        uint64_t number = 0;
        if (!StringToUint64(sub_node.asString(), &number)) {
            LOG(WARNING) << "Fail to convert to interger:" << sub_node.asString();
            return false;
        }
        reflection->AddUInt64(pb, field, number);
        break;
    }
    case FieldDescriptor::CPPTYPE_DOUBLE:
        reflection->AddDouble(pb, field, sub_node.asDouble());
        break;
    case FieldDescriptor::CPPTYPE_FLOAT:
        reflection->AddFloat(pb, field, sub_node.asDouble());
        break;
    case FieldDescriptor::CPPTYPE_BOOL:
        reflection->AddBool(pb, field, sub_node.asBool());
        break;
    case FieldDescriptor::CPPTYPE_ENUM: {
        const EnumValueDescriptor* enum_value = reflection->GetEnum(*pb, field);
        const EnumDescriptor* enum_desc = enum_value->type();
        const EnumValueDescriptor* real_enum_value = enum_desc->FindValueByNumber(sub_node.asInt());
        reflection->AddEnum(pb, field, real_enum_value);
        break;
    }
    case FieldDescriptor::CPPTYPE_STRING:
        reflection->AddString(pb, field, sub_node.asString());
        break;
    case FieldDescriptor::CPPTYPE_MESSAGE:
    {
        MessageFactory* factory = MessageFactory::generated_factory();
        Message* message = reflection->AddMessage(pb, field, factory);
        util::ProtoJsonFormat::ParseFromValue(sub_node, message);
        break;
    }
    default:
        LOG(FATAL) << "Bad type:" << field->cpp_type();
        break;
    }
    return true;
}
예제 #12
0
static void print_value_tree( FILE *fout, Json::Value &value, const std::string &path = "."  )
{
    switch (value.type())
    {
        case Json::nullValue:
            fprintf( fout, "%s=null\n", path.c_str()  );
            break;

        case Json::intValue:
            fprintf( fout, "%s=%d\n", path.c_str(), value.asInt()  );
            break;

        case Json::uintValue:
            fprintf( fout, "%s=%u\n", path.c_str(), value.asUInt()  );
            break;

        case Json::realValue:
            fprintf( fout, "%s=%.16g\n", path.c_str(), value.asDouble()  );
            break;

        case Json::stringValue:
            fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str()  );
            break;

        case Json::booleanValue:
            fprintf( fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false"  );
            break;

        case Json::arrayValue:
            {
                fprintf( fout, "%s=[] arrayValue \n", path.c_str()  );
                int size = value.size();
                for ( int index =0; index < size; ++index  )
                {
                    static char buffer[16];
                    sprintf( buffer, "[%d]", index  );
                    print_value_tree( fout, value[index], path + buffer);

                }

            }
            break;

        case Json::objectValue:
            {
                fprintf( fout, "%s={} objectValue \n", path.c_str()  );
                Json::Value::Members members( value.getMemberNames()  );
                std::sort( members.begin(), members.end()  );
                std::string suffix = *(path.end()-1) == '.' ? "" : ".";
                for ( Json::Value::Members::iterator it = members.begin(); it != members.end();++it)
                {
                    const std::string &name = *it;
                    print_value_tree( fout, value[name], path + suffix + name  );

                }

            }
            break;

        default:
            break;
    }
}
예제 #13
0
void OptionNumber::set(Json::Value &value)
{
	_value = value.asDouble();
}
예제 #14
0
double FileParser::parseDouble(const Json::Value& node, const string& nodeName) const {
  if (node == Json::nullValue) {
    throw RenderException("unable to parse double for \"" + nodeName + "\"");
  }
  return node.asDouble();
}
예제 #15
0
// Gets all timers and recordings
bool filmonAPIgetRecordingsTimers(bool completed) {
	bool res = filmonRequest("tv/api/dvr/list", sessionKeyParam);
	if (res == true) {
		Json::Value root;
		Json::Reader reader;
		reader.parse(response, root);

		// Usage
		Json::Value total = root["userStorage"]["total"];
		Json::Value used = root["userStorage"]["recorded"];
		storageTotal = (long long int) (total.asDouble() * FILMON_ONE_HOUR_RECORDING_SIZE); // bytes
		storageUsed = (long long int) (used.asDouble() * FILMON_ONE_HOUR_RECORDING_SIZE); // bytes
		XBMC->Log(LOG_DEBUG, "recordings total is %u", storageTotal);
		XBMC->Log(LOG_DEBUG, "recordings used is %u", storageUsed);

		bool timersCleared = false;
		bool recordingsCleared = false;

		Json::Value recordingsTimers = root["recordings"];
		for (unsigned int recordingId = 0;
				recordingId < recordingsTimers.size(); recordingId++) {
			std::string recTimId =
					recordingsTimers[recordingId]["id"].asString();
			std::string recTimTitle =
					recordingsTimers[recordingId]["title"].asString();
			unsigned int recTimStart = stringToInt(
					recordingsTimers[recordingId]["time_start"].asString());
			unsigned int recDuration = stringToInt(
					recordingsTimers[recordingId]["length"].asString());

			Json::Value status = recordingsTimers[recordingId]["status"];
			if (completed && status.asString().compare(std::string(RECORDED_STATUS)) == 0) {
				if (recordingsCleared == false) {
					recordings.clear();
					recordingsCleared = true;
				}
				FILMON_RECORDING recording;
				recording.strRecordingId = recTimId;
				recording.strTitle = recTimTitle;
				recording.strStreamURL =
						recordingsTimers[recordingId]["download_link"].asString();
				recording.strPlot =
						recordingsTimers[recordingId]["description"].asString();
				recording.recordingTime = recTimStart;
				recording.iDuration = recDuration;
				recording.strIconPath =	recordingsTimers[recordingId]["images"]["channel_logo"].asString();
				recording.strThumbnailPath = recordingsTimers[recordingId]["images"]["poster"].asString();
				recordings.push_back(recording);
				XBMC->Log(LOG_DEBUG, "found completed recording %s", recording.strTitle.c_str());
			} else if (status.asString().compare(std::string(TIMER_STATUS))
					== 0) {
				if (timersCleared == false) {
					timers.clear();
					timersCleared = true;
				}

				FILMON_TIMER timer;
				timer.iClientIndex = stringToInt(recTimId);
				timer.iClientChannelUid = stringToInt(
						recordingsTimers[recordingId]["channel_id"].asString());
				timer.startTime = recTimStart;
				timer.endTime = timer.startTime + recDuration;
				timer.strTitle = recTimTitle;
				timer.state = FILMON_TIMER_STATE_NEW;
				timer.strSummary =
						recordingsTimers[recordingId]["description"].asString();
				setTimerDefaults(&timer);
				time_t t = time(0);
				if (t >= timer.startTime && t <= timer.endTime) {
					XBMC->Log(LOG_DEBUG, "found active timer %s", timer.strTitle.c_str());
					timer.state = FILMON_TIMER_STATE_RECORDING;
				} else if (t < timer.startTime) {
					XBMC->Log(LOG_DEBUG, "found scheduled timer %s", timer.strTitle.c_str());
					timer.state = FILMON_TIMER_STATE_SCHEDULED;
				} else if (t > timer.endTime) {
					XBMC->Log(LOG_DEBUG, "found completed timer %s", timer.strTitle.c_str());
					timer.state = FILMON_TIMER_STATE_COMPLETED;
				}
				timers.push_back(timer);
			}
		}
		clearResponse();
	}
	return res;
}
static bool castToNumber(JSON::Value& value, double& result) { return value.asDouble(result); }