Getting large numbers from ofJSON

I am trying to retrieve large numbers from a JSON file, they are actually unix timestamps in milliseconds.
For example
This is too big for an int, I think the appropriate type is:
But how can I get the value from the JSON file like this, or actually in any format that does not truncate the value. At the moment when I try to get the value like this:

My .h file:

ofJSON myJSONData;
int  unixTime1YearAgo;

My .cpp file

unixTime1YearAgo = ofGetUnixTime() - 31536000; //31536000 is seconds per year

ofFile myJSONFile("myFile.json");

	if (myJSONFile.exists())
		myJSONFile >> myJSONData;

		for (int i = 0; i < myJSONData["blocks"].size(); i++)
			int tempStamp =ofToInt(myJSONData["blocks"][i]["timestampMs"]);

			cout << myJSONData["blocks"][i]["timestampMs"] << endl;

			if (tempStamp/1000  < unixTime1YearAgo) //I want to exit this loop when I get to the timestamps that are not in the range
				cout << "we got further than one year ago" << endl;
			cout<<"this time is within range of one year " + ofToString(tempStamp)<<endl;

This is the line that I need to change

int tempStamp =ofToInt(myJSONData["blocks"][i]["timestampMs"]);
I have tried this:
long int tempStamp =myJSONData["blocks"][i]["timestampMs"].get<long int>();

But this crashes and substituting long int with int64_t also does not work

I dont think this is a difficult thing to do, but I cannot see how to get this large value.

It wold be great if someone can help me out

If this doesn’t work:

int64_t t_signed = myJSONData["blocks"][i]["timestampMs"].get<int64_t>();
int64_t t_unsigned = myJSONData["blocks"][i]["timestampMs"].get<uint64_t>();

Then you need to look at your json and see if it is being stored as a string or a number. If it is being stored as a string, json will throw an exception. In that case you might need to:

int64_t t_signed = ofToInt64(myJSONData["blocks"][i]["timestampMs"].get<std::string>());
unsigned long long t_unsigned = std::stoull(myJSONData["blocks"][i]["timestampMs"].get<std::string>());

This worked perfectly, thanks.

1 Like